Cloudish123
Cloudish123

Reputation: 879

How do I reference tags in a when statement in Ansible

Given this task, particularly in the when statement, what is the way to reference the following tag:

 "tags": {
    "Name": "volume1"
  },

Task:

  - name: Attach correct ebs volume to correct instance using tags
    local_action:
        module: ec2_vol
        region: '{{ region }}'  
        instance: "{{ item.instance_ids[0] }}"
        id: vol-1111111111111111
        name: ebs-volume1
        device_name: /dev/sdf
    with_items: '{{ ec2.results }}' 
    when:
    - item.instance_ids[0] is defined
    - item.tagged_instances.tags.Name == "volume1"

I get an error back saying that there is no attribute "tags".

I can't seem to reference it correctly in a debug statement either.

I am thinking it could be something like:

item.tagged_instances.tags == "Name": "volume1"

Should I have curly brackets??

Thanks!

Nick

Upvotes: 2

Views: 358

Answers (1)

Cloudish123
Cloudish123

Reputation: 879

This is the answer for those that need it:

when: item.tagged_instances[0].tags.Name == "volume1"

This ensures that the task only works on the instances that have a tag of volume1. Pretty neat. In this case the key was Name and the value was volume1.

Thanks!

Upvotes: 1

Related Questions