I'm trying to create a VPC with certain tags which are derived from
variables. I feel like I can't find the right YAML/Jinja/whatever
syntax to make it work though.
At first I tried setting them in the resource_tags directly when using
the ec2_vpc module like so:
- name: Create VPC for new environment
local_action:
module: ec2_vpc
state: present
cidr_block: 10.0.0.0/16
resource_tags: { "Name": "marketplace-{{ environment }}" }
region: "{{ aws_region }}"
dns_hostnames: true
dns_support: true
internet_gateway: true
validate_certs: true
instance_tenancy: default
wait: true
subnets:
route_tables:
register: new_vpc
While this executes without errors it results in a VPC with the Name
tag of "marketplace-{}" which is obviously not what I want.
And if my Name tag starts out with a "{" like "{{ environment
}}-marketplace" then I get this error:
failed: [localhost] => {"failed": true, "parsed": false}
invalid output was: Traceback (most recent call last):
File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2_vpc",
line 2001, in <module>
main()
File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2_vpc",
line 599, in main
(vpc_dict, new_vpc_id, subnets_changed, changed) =
create_vpc(module, vpc_conn)
File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2_vpc",
line 278, in create_vpc
previous_vpc = find_vpc(module, vpc_conn, id, cidr_block)
File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2_vpc",
line 240, in find_vpc
if resource_tags and
set(resource_tags.items()).issubset(set(vpc_tags.items())):
TypeError: unhashable type: 'dict'
So I though, maybe ec2_vpc does run the Jinja templating on the values
in the resource_tags dictionary. So I then tried to create the VPC
with a dummy name tag, save it's vpc_id and then use ec2_tag to add
the tags in a separate task:
- name: Tag the new VPC
local_action: ec2_tag resource={{ new_vpc.vpc_id }} region={{
aws_region }} state=present
args:
tags:
Name: "marketplace-{{ environment }}"
But this results in the same thing (vpc with a Name tag of
"marketplace-{}" instead of something like "marketplace-branch-XYZ".
Am I confused about the YAML/Jinja syntax to accomplish what I want or
is using variables in EC2 tags not supported? And if it's not
supported would pull requests to add it be something folks would like?
Thanks