pass ec2 tag to a task conditional

1,666 views
Skip to first unread message

Paul Stivers

unread,
Apr 11, 2016, 5:57:10 PM4/11/16
to Ansible Project

Is there a way to pass the value of an ec2 tag to a conditional in an ansible task? Boto is installed and used to determine which tagged ec2 servers the playbook is run against.

 

For example:

 

ec2 server with tag name "Application" and value "my_app"

 

ansible command to run a playbook

 

 

$ AWS_PROFILE=paul-dev ansible-playbook playbooks/paul_test.yml -i inventories/ec2.py -l 'tag_Environment_dev:&tag_Application_my_app' -e "app=my_app" -u ubuntu -s

 

works for the simple playbook containing this task because of the extra variable "app=my_app" on the commandline.

 

---

 

 
- hosts: all

 

    tasks
:

     
- name: tree package is present

        apt
: name=tree state=present

       
when: app  == 'my_app'


 

If I want to avoid including an extra variable on the command line, is there a way to access the ec2 tag name and value in a task conditional? The tag name and value are referenced in the command to run the playbook, "&tag_Application_my_app", but I haven't found a way to reference it in a task.

 

I get the following error when running the playbook with the following attempts at conditionals on the ec2 tag

 

---

 

 
- hosts: all

 

    tasks
:

     
- name: tree package is present

        apt
: name=tree state=present

       
when: tag_Application == 'my_app'

       
# when: Application = 'my_app'

       
# when: tag_Application_my_app


 

fatal: [10.5.81.130] => ERROR! error while evaluating conditional: tag_Application_cis_acord_1203

 

Stephen Granger

unread,
Apr 12, 2016, 5:58:39 PM4/12/16
to ansible...@googlegroups.com
http://docs.ansible.com/ansible/ec2_tag_module.html

You will need to "list" the tags first of the instance to see what tags are set.

Something like

when: ec2_tags.tags.Application == 'my_app'

should give you what you want.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2a6129a9-760b-4500-83d1-b187b2aa3ebb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve

Paul Stivers

unread,
Apr 21, 2016, 6:11:54 PM4/21/16
to Ansible Project
Good clue, Steve. Thanks.

Got me thinking, I'm using the EC2 external inventory script, so from the variable list here http://docs.ansible.com/ansible/intro_dynamic_inventory.html#example-aws-ec2-external-inventory-script

This worked

when: ec2_tag_Application == 'my_app'


On Tuesday, April 12, 2016 at 2:58:39 PM UTC-7, Stephen Granger wrote:
http://docs.ansible.com/ansible/ec2_tag_module.html

You will need to "list" the tags first of the instance to see what tags are set.

Something like

when: ec2_tags.tags.Application == 'my_app'

should give you what you want.

--
Steve
Reply all
Reply to author
Forward
0 new messages