1

I have two variable definition files as follows

vars.yml

aws_common_tags:
    Project: "{{ project_name }}"
    Application: "{{ application_name }}"
        Region: "{{ aws_region }}"
        Ansible: "Yes"

instance.yml

ec2_instance_tags:
    environment_name: dev

Now in task.yml I am trying to create aws ec2 instance with all tags but its not working.

ec2:
    key_name: "{{ keypair_name }}"
    instance_type: "{{ ec2_instance_type }}"
    instance_tags: "{{ aws_common_tags + ec2_instance_tags }}"

We are getting the following error:

"tags": {
        "Ansible": "Yes",
        "Application": "webserver",
        "Project": "document_upload",
        "Region": "us-west-2"
    },

fatal: [localhost]: FAILED! => {
    "msg": "Unexpected templating type error occurred on ({{ aws_common_tags + ec2_instance_tags }}): unsupported operand type(s) for +: 'dict' and 'dict'" }

Thanks

4wk_
  • 310
  • 3
  • 15
Geo
  • 575
  • 3
  • 9
  • 23

1 Answers1

2

It's possible combine dictionaries. For example

ec2:
    key_name: "{{ keypair_name }}"
    instance_type: "{{ ec2_instance_type }}"
    instance_tags: "{{ aws_common_tags|combine(ec2_instance_tags) }}"

The concatenation "+" would work with lists or strings that are treated as lists.

Vladimir Botka
  • 5,138
  • 8
  • 20