Trouble with using variables for ec2 tags

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

This is definitely the same YAML parsing error, but because it’s happening in the complex args it doesn’t seem to be triggering the same error detection/handling code. Could you please open a github issue for this as well?

Thanks!

I've filed it as 2 separate issues since they could be related but
different (one completing but with the wrong results and the other
failing with the traceback):

https://github.com/ansible/ansible/issues/7833
https://github.com/ansible/ansible/issues/7835

But I'm still a bit confused about the why the first attempt I
mentioned is failing. It's not throwing the traceback, it's just
giving weird results. Is this still a YAML error? Can I write my YAML
in a way that doesn't trigger this problem? Or is there a limit on
which values can be templated?

Thanks,

Already got this on github, yes, but the problem is the ‘environment’ variable in Ansible is reserved.

The “environment” keyword stores variables here.

We may consider changing this to ansible_environment as this occasionally happens.