how to get name tag from ec2_facts

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 }}

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.

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.

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

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.