Cmag
Cmag

Reputation: 15770

ansible playbook when: condition ec2 tag is set

Folks, I'd like to run a simple command, lets say setting a hostname, if an ec2 tag is set on an instance. Consider the following:

- name: Gather facts
  action: ec2_facts

- name: Get instance ec2 facts
  action: ec2_facts
  register: ec2_facts

- name: Get resource tags from ec2 facts
  sudo: false
  local_action: ec2_tag resource={{ec2_facts.ansible_facts.ansible_ec2_instance_id}} region={{ec2_facts.ansible_facts.ansible_ec2_placement_region}} state=list
  register: ec2_tags

- debug: msg="{{ ec2_tags.tags }}"

Output:

TASK: [foo | debug msg="{{ ec2_tags.tags }}"] *****************************
ok: [10.1.15.119] => {
"msg": "{'role': 'foo', 'Name': 'YAY', 'service': 'web', 'env': 'dev'}"
}

So now, I'd like to do the following:

Set a hostname, if the ec2_tags.tags.Name exists. How do i use this in a playbook ? This seems to fail:

- name: friendly hostname
  hostname: name={{ec2_tags.tags.Name}}
  when: {{ec2_tags.tags.Name}}

Thanks!

Upvotes: 2

Views: 3374

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146084

when: ec2_tags.tags.Name is defined should do it

Upvotes: 3

Related Questions