how to get name tag from ec2_facts

1,415 views
Skip to first unread message

Brian Schott

unread,
Sep 19, 2013, 9:25:56 AM9/19/13
to ansible...@googlegroups.com

I have a playbook where I'm launching some instances, adding them to a host group, doing package installs, then I want to bundle each instance with a unique image name.  My first thought was to use an instance tag, but can't seem to find how to read tags on a running instance.  Should I use set_fact when I create the instance instead?  Still getting my head around variable scoping.

Thanks!
Brian

roles/ec2_bundle_image/main.yaml:

- name: Fetch the ec2_facts for the server
  action: ec2_facts

- name: Calculate the image name
  action: set_fact ec2_image_name={{ NAME_TAG_GOES_HERE }}_{{ ansible_ec2_profile }}_{{ ansible_date_time[date] }}

- name: Bundle the instance into an image
  local_action: ec2_ami aws_access_key={{ aws_access_key }} aws_secret_key={{ aws_secret_key }}
                instance_id={{ ansible_ec2_instance_id }} wait=yes
                 name={{ ec2_image_name }}


Michael DeHaan

unread,
Sep 19, 2013, 9:28:42 PM9/19/13
to ansible...@googlegroups.com
Seems like the ec2_facts module should return what you want since it hits the ec2 metadata server.

Shouldn't need to use set_fact here and I don't see how that would possibly help since that sets variables, it doesn't look them up.




--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Brian Schott

unread,
Sep 19, 2013, 11:06:36 PM9/19/13
to ansible...@googlegroups.com, ansible...@googlegroups.com

Unfortunately, the ec2 tags are not available inside the instance data.
http://stackoverflow.com/questions/3883315/query-ec2-tags-from-within-instance

I didn't realize I could add arbitrary variables during add_host, so I think I can just set ec2_image_name to what I want in the add_host action when I start the instances.

Otherwise, I don't see a way to list the tags associated with an instance.

Sent from Mailbox for iPad


You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/ci30yycTbi8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

benno joy

unread,
Sep 19, 2013, 11:30:58 PM9/19/13
to ansible...@googlegroups.com
Hi Brian,


Maybe you can set the user data while instance provisioning

user_data={{ foo }}

and use the ansible fact "ansible_ec2_user_data" for image provisioning.

Regards,
Benno

Michael DeHaan

unread,
Sep 21, 2013, 9:03:35 AM9/21/13
to ansible...@googlegroups.com
It would be nice if the facts module could be extended to provide tag info so nodes could be self selecting.

In any case, the ec2 dynamic inventory script does return tag info.


Reply all
Reply to author
Forward
0 new messages