# can we use loop modules twice in ansible playbook.

**URL:** <https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [April 25, 2017, 6:55pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312 "2017-04-25T18:55:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![balubhai555](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@balubhai555](https://forum.ansible.com/u/balubhai555)\
**Post date:** [April 25, 2017, 6:55pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/1 "2017-04-25T18:55:27Z")

</div>

Hello team,

I want to

1. Create AWS ec2 instance using one " **with\_items: ec2"**

2. After that I want to insert the IP address of those instances into **/etc/ansible/hosts** file. For that I am using **local\_action: lineinfile .** But after that I want to use again with\_items: “{{ec2.instances}}”

At that time it shows error, That Item is undefined.

Are we use those with\_items twice in a playbook. Any example please suggest me.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![jtanner](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jtanner](https://forum.ansible.com/u/jtanner)\
**Post date:** [April 26, 2017, 1:17pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/2 "2017-04-26T13:17:26Z")

</div>

A variable does not disappear after being used in an item loop, so I suspect you aren’t referring to the variable correctly in the second task. Please post an example playbook and the full -vvvv output with the error.

---

<div class="post-metadata">

**Author:** ![balubhai555](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@balubhai555](https://forum.ansible.com/u/balubhai555)\
**Post date:** [April 26, 2017, 1:54pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/3 "2017-04-26T13:54:07Z")

</div>

> > [![](https://lh3.googleusercontent.com/-cvX4IudbEQo/WQCllh2EK6I/AAAAAAAAAH0/m-RoG7m1Un4ut28Ffw5xlYjCW-sZOyYcgCLcB/s320/Screenshot%2B%25281104%2529.png)](https://lh3.googleusercontent.com/-cvX4IudbEQo/WQCllh2EK6I/AAAAAAAAAH0/m-RoG7m1Un4ut28Ffw5xlYjCW-sZOyYcgCLcB/s1600/Screenshot%2B%25281104%2529.png)
> > 
> > tasks:
> > 
> > - name: Launch EC2 instance  
> > ec2:  
> > key\_name: “{{keypair}}”  
> > group: default  
> > count: “{{count}}”  
> > instance\_type: “{{instance\_type}}”  
> > instance\_tags: ‘{“Name”: “{{item.names}}”,“Type”:“{{tag\_Type}}”,“Environment”:“{{tag\_Environment}}”}’  
> > image: “{{image}}”  
> > wait: true  
> > region: “{{region}}”
> > 
> > aws\_access\_key: \*\*\*\*\*  
> > aws\_secret\_key: \*\*\*\*\*  
> > register: ec2  
> > with\_items:
> > 
> > - names: Ansible-1
> > 
> > - names: Ansible-2
> > 
> > - name: One Add the newly created EC2 instance(s) to the local host group (located inside the directory)  
> > local\_action: lineinfile dest=“/etc/hosts” regexp={{ item.public\_ip }} insertafter=“[launched]” line=“{{ item.public\_ip }} ansible\_ssh\_private\_key\_file=~/.ssh/{{ keypair }}.pem”
> > 
> > with\_items: “{{ec2.instances}}”

> > - name: wait for ssh to come up  
> > wait\_for:  
> > host: “{{ ansible\_host }}”  
> > port: 22  
> > delay: 60  
> > timeout: 320  
> > state: started
> > 
> > with\_items: “{{ec2.instances}}”
> > 
> > - name: Print all ec2 variables  
> > debug: var=ec2

[![](https://lh3.googleusercontent.com/-dbp1hcpEmz0/WQCmehD9_oI/AAAAAAAAAIA/yGn1basj9D4I0UgOYEvpI4MbezIJUgusgCLcB/s320/Screenshot%2B%25281104%2529.png)](https://lh3.googleusercontent.com/-dbp1hcpEmz0/WQCmehD9_oI/AAAAAAAAAIA/yGn1basj9D4I0UgOYEvpI4MbezIJUgusgCLcB/s1600/Screenshot%2B%25281104%2529.png)

---

<div class="post-metadata">

**Author:** ![groupfiles14](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@groupfiles14](https://forum.ansible.com/u/groupfiles14)\
**Post date:** [April 26, 2017, 4:15pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/4 "2017-04-26T16:15:51Z")

</div>

if you debug that you will get the real result name, it may be something like ec2.results.instances or something like that. can you post the debug output of ec2 variable?

---

<div class="post-metadata">

**Author:** ![balubhai555](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@balubhai555](https://forum.ansible.com/u/balubhai555)\
**Post date:** [April 26, 2017, 9:11pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/5 "2017-04-26T21:11:08Z")

</div>

Thanks for your reply,

Will you please explain more. Thank you.

---

<div class="post-metadata">

**Author:** ![groupfiles14](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@groupfiles14](https://forum.ansible.com/u/groupfiles14)\
**Post date:** [April 27, 2017, 3:08am UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/6 "2017-04-27T03:08:42Z")

</div>

tasks:

- name: Launch EC2 instance  
ec2:  
key\_name: “{{keypair}}”  
group: default  
count: “{{count}}”  
instance\_type: “{{instance\_type}}”  
instance\_tags: ‘{“Name”: “{{item.names}}”,“Type”:“{{tag\_Type}}”,“Environment”:“{{tag\_Environment}}”}’  
image: “{{image}}”  
wait: true  
region: “{{region}}”

aws\_access\_key: \*\*\*\*\*  
aws\_secret\_key: \*\*\*\*\*  
register: ec2  
with\_items:

- names: Ansible-1
- names: Ansible-2  
**debug: var=ec2**

add the above bold line in your playbook, it will throw an output , paste that here

---

<div class="post-metadata">

**Author:** ![balubhai555](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@balubhai555](https://forum.ansible.com/u/balubhai555)\
**Post date:** [April 27, 2017, 1:45pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/7 "2017-04-27T13:45:42Z")

</div>

Thanks for your quick reply. Already entered those bold lines.

Got the same issue.

---

<div class="post-metadata">

**Author:** ![groupfiles14](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@groupfiles14](https://forum.ansible.com/u/groupfiles14)\
**Post date:** [April 27, 2017, 3:54pm UTC](https://forum.ansible.com/t/can-we-use-loop-modules-twice-in-ansible-playbook/24312/8 "2017-04-27T15:54:00Z")

</div>

can you please show the output of it…i dont see that in the screenshot
