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.
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?
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.
Can you please share the output of:
Thanks!
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”
}
]
}
}
In that case, looks like you want:
instance.results[0].image_id
Thank you very much, that did it.
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’, ‘#}’])
Take out the var=
{{ instance.results[0].image_id }}
Sorry, I should have got that one on my own. Thank you again for your help.
Meant to add, Ansible is a huge boon to our infrastructure efforts. Thanks so much to the Ansible team for creating this tool.
Saying this collectively for hundreds of people here who have helped with things, and the rest of the team - you are welcome!!!