Execute task when: "'Key' not in object"

11 views
Skip to first unread message

Rob Wilkerson

unread,
Oct 30, 2015, 6:07:53 PM10/30/15
to Ansible Project
I have a playbook that's creating _n_ EC2 instances using exact_count. I'd like to tag those instances with a semi-meaningful Name value. I can get a list of all of my tagged instances from the ec2 module return value using {{ servers.tagged_instances }}, but...

I only want to apply a Name tag to those that don't already have one.

- name: Instances | Tag each instance with a name
  ec2_tag:
    aws_access_key: "{{ awscli.access_key }}"
    aws_secret_key: "{{ awscli.secret_key }}"
    region: "{{ aws.region }}"
    resource: "{{ item.1.id }}"
    state: present
    tags:
      Name: "{{ env_short }}-{{ 'ws%02d' | format(item.0) }}"
  with_indexed_items: webservers.tagged_instances
  when: "'Name' not in item.tags.keys()"  # Untagged instances only

I've tried several variations of the `when` stanza, but nothing's worked. Likewise, I haven't found any documentation with an example like this. Is it possible?
Reply all
Reply to author
Forward
0 new messages