Hi Stuart
This works fine for me with Ansible 1.8 onwards:
- name: Create an AMI from the provisioned instance
ec2_ami: region=ap-southeast-1 instance_id={{ item.id }}
name=baseami-{{ timestamp.stdout }} wait=yes wait_timeout=600
with_items:
- "{{ ec2.instances }}"
register: instance
- name: Delete the instance now that the AMI is created
ec2:
region: ap-southeast-1
instance_ids: "{{ ec2.instance_ids }}"
state: 'absent'
On Tue, Mar 31, 2015 at 4:29 PM, Stuart Davidson <[email protected]
> wrote:
> Hey Navid, you couldn't post an example could you?
>
> I seem to be running into the same problem as you, but 1.8.2 nor the
> brackets are fixing the problem.
>
> On Friday, 12 December 2014 01:21:42 UTC, Navid Paya wrote:
>>
>> Thanks, James. Upgrading Ansible from 1.8.1 to 1.8.2 and adding the
>> braces totally did the job.
>>
>> On Thu, Dec 11, 2014 at 11:30 PM, James Cammarata <[email protected]>
>> wrote:
>>>
>>> Hi Navid, what version of Ansible are you running? You might try using
>>> jinja2 braces around the ec2.instances variable in the with_items list, as
>>> there was a bug recently about variables like that not expanding properly
>>> (which should be fixed in 1.8.2).
>>>
>>> On Thu, Dec 11, 2014 at 8:36 AM, Navid Paya <[email protected]> wrote:
>>>
>>>> Hi all
>>>> I'm trying to make some kind of automation to create an AMI out of an
>>>> EC2 instance. So I have this chunk that creates the instance:
>>>>
>>>> - name: Launch an Ubuntu 14.04 EC2 instance
>>>> hosts: localhost
>>>> connection: local
>>>> gather_facts: no
>>>> tasks:
>>>> - name: Find the latest Ubuntu AMI
>>>> ec2_ami_search: distro=ubuntu release=trusty region=ap-southeast-1
>>>> store=ebs-ssd virt=hvm
>>>> register: ubuntu_image
>>>> - name: Start the new EC2 instance
>>>> ec2:
>>>> image: "{{ ubuntu_image.ami }}"
>>>> region: ap-southeast-1
>>>> zone: ap-southeast-1b
>>>> instance_type: t2.small
>>>> vpc_subnet_id: xxxxxxxxxxxxxxxx
>>>> group_id: ['sg-xxxxxxxx', 'sg-xxxxxxx']
>>>> key_name: random_key
>>>> wait: yes
>>>> wait_timeout: 500
>>>> register: ec2
>>>> - name: Add the new instance to host group
>>>> add_host: hostname={{ item.private_ip }} groupname=launched
>>>> with_items: ec2.instances
>>>> - name: Wait for SSH to come up on the new instance
>>>> wait_for: host={{ item.private_ip }} port=22 delay=60 timeout=320
>>>> state=started
>>>> with_items: ec2.instances
>>>>
>>>> Then I try to use the instance and generate an AMI out of that:
>>>>
>>>> - name: Create an AMI from the provisioned instance
>>>> hosts: localhost
>>>> connection: local
>>>> gather_facts: no
>>>> tasks:
>>>> - ec2_ami: region=ap-southeast-1 instance_id={{ item.id }} wait=no
>>>> name=basebox-2000000
>>>> with_items:
>>>> - ec2.instances
>>>>
>>>> - name: Delete the instance now that the AMI is created
>>>> hosts: localhost
>>>> connection: local
>>>> gather_facts: no
>>>> tasks:
>>>> - ec2:
>>>> region: ap-southeast-1
>>>> instance_ids: "{{ item.id }}"
>>>> state: 'absent'
>>>> with_items:
>>>> - ec2.instances
>>>>
>>>> But I get errors like this:
>>>>
>>>>
>>>> TASK: [ec2_ami region=ap-southeast-1 instance_id={{ item.id }} wait=no
>>>> name=basebox-2000000] ***
>>>> fatal: [localhost] => One or more undefined variables: 'str object' has
>>>> no attribute 'id'
>>>>
>>>> I guess I'm missing something with iterating over a hash but can't
>>>> figure what. Any ideas?
>>>>
>>>> --
>>>> 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 [email protected].
>>>> To post to this group, send email to [email protected].
>>>> To view this discussion on the web visit https://groups.google.com/d/
>>>> msgid/ansible-project/cc359c02-afd3-4c71-931d-
>>>> 7c5db8fde6a6%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/ansible-project/cc359c02-afd3-4c71-931d-7c5db8fde6a6%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>>> 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/VkA8f77Pn9E/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to
>>> [email protected].
>>> To post to this group, send email to [email protected].
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/ansible-project/CAMFyvFip1qw%2B_bFSB2YrOqHoMYPwEL%2B4%
>>> 2Bgckj18Y_CnWkE3J5g%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/ansible-project/CAMFyvFip1qw%2B_bFSB2YrOqHoMYPwEL%2B4%2Bgckj18Y_CnWkE3J5g%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> 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/VkA8f77Pn9E/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/910c13a9-bb28-4ba7-8ebb-4a3638bc457c%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/910c13a9-bb28-4ba7-8ebb-4a3638bc457c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAFsFwzu3oDuq-cpJwVOA9%2BC4VQzW0Ag2%3DPXJFf0yu9xZc1fvYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.