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?