Getting the ID of an AMI created with Ansible

380 views
Skip to first unread message

Peter Palmieri

unread,
Sep 11, 2014, 3:00:51 PM9/11/14
to ansible...@googlegroups.com

I'm building an EC2 instance with Ansible, then creating an AMI from the instance. I'm sure I'm missing something here, but how do I get the ID of the newly created AMI? I've tried:

tasks:
- name: create an ami in us-east-1
  ec2_ami: wait=yes
           aws_access_key={{ ec2_access_key }}
           aws_secret_key={{ ec2_secret_key }}
           instance_id={{ item }}
           region={{ region1 }}
           name=data-mgmt-qa-006
  with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id']
  register: ec2_ami_info

- debug: var=item
  with_items: ec2_ami_info.image_id 

and:

tasks:
- name: create an ami in us-east-1
  ec2_ami: wait=yes
           aws_access_key={{ ec2_access_key }}
           aws_secret_key={{ ec2_secret_key }}
           instance_id={{ item }}
           region={{ region1 }}
           name=data-mgmt-qa-006
  with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id']
  register: instance

- debug: var=item
  with_items: instance.image_id

The latter 'register' is copied from the docs, but I'm not able to get the right with_items obviously.

The AMI is being created fine. Any suggestions would be much appreciated. 

James Cammarata

unread,
Sep 11, 2014, 9:52:52 PM9/11/14
to ansible...@googlegroups.com
You should be able to just do:

- debug: var=instance.image_id

Which should print out the image's ID. If not, could you share what it does show?

--
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/c0d46c8d-1d93-494c-a016-877f5923fe90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Palmieri

unread,
Sep 12, 2014, 2:43:30 PM9/12/14
to ansible...@googlegroups.com
Thanks for helping, it's much appreciated. 

I now have:

  tasks:
    - name: create an ami in us-east-1
      ec2_ami: wait=yes
               aws_access_key={{ ec2_access_key }}
               aws_secret_key={{ ec2_secret_key }}
               instance_id={{ item }}
               region={{ region1 }}
               name=some-name
      with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id']
      register: instance

    - debug: var=instance.image_id

Which returns:

TASK: [create an ami in us-east-1] ******************************************** 
changed: [54.198.217.217] => (item=i-7c77c197)

TASK: [debug var=instance.image_id] ******************************************* 
ok: [54.198.217.217] => {
    "instance.image_id": "{{ instance.image_id }}"
}

instead of the actual image id. 

Michael DeHaan

unread,
Sep 12, 2014, 3:22:44 PM9/12/14
to ansible...@googlegroups.com
Can you please share the output of:

- debug: var=instance

Thanks!



Peter Palmieri

unread,
Sep 12, 2014, 3:54:55 PM9/12/14
to ansible...@googlegroups.com
That shows:

TASK: [debug var=instance] **************************************************** 
ok: [54.90.128.104] => {
    "instance": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "changed": true, 
                "image_id": "ami-be14b9d6", 
                "invocation": {
                    "module_args": "wait=yes aws_access_key=**** aws_secret_key=**** instance_id=i-393284d2 region=us-east-1 name=blah", 
                    "module_name": "ec2_ami"
                }, 
                "item": "i-393284d2", 
                "msg": "AMI creation operation complete", 
                "state": "available"
            }
        ]
    }
}

Michael DeHaan

unread,
Sep 12, 2014, 4:04:53 PM9/12/14
to ansible...@googlegroups.com
In that case, looks like you want:

instance.results[0].image_id



Peter Palmieri

unread,
Sep 12, 2014, 4:21:41 PM9/12/14
to ansible...@googlegroups.com
Thank you very much, that did it. 

Peter Palmieri

unread,
Sep 12, 2014, 5:36:20 PM9/12/14
to ansible...@googlegroups.com
How can I reference this in an ini file module? I'm trying to write the value of the ami id to a file, to be consumed by another process. This didn't work:

- ini_file: dest=ami.ini section=qa option=ami value={{ var=instance.results[0].image_id }}

I got:

TASK: [ini_file dest=ami.ini section=qa option=ami value={{ var=instance.results[0].image_id }}] *** 
failed: [54.166.40.120] => {"failed": true}
msg: this module requires key=value arguments (['dest=ami.ini', 'section=qa', 'option=ami', 'value={#', 'var=instance.results[0].image_id', '#}'])

Michael DeHaan

unread,
Sep 12, 2014, 5:52:36 PM9/12/14
to ansible...@googlegroups.com
Take out the var=

{{ instance.results[0].image_id }}



Peter Palmieri

unread,
Sep 12, 2014, 6:10:56 PM9/12/14
to ansible...@googlegroups.com
Sorry, I should have got that one on my own. Thank you again for your help. 

Peter Palmieri

unread,
Sep 12, 2014, 6:13:05 PM9/12/14
to ansible...@googlegroups.com
Meant to add, Ansible is a huge boon to our infrastructure efforts. Thanks so much to the Ansible team for creating this tool. 

Michael DeHaan

unread,
Sep 12, 2014, 7:45:42 PM9/12/14
to ansible...@googlegroups.com
Saying this collectively for hundreds of people here who have helped with things, and the rest of the team - you are welcome!!!


--
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.
Reply all
Reply to author
Forward
0 new messages