# loop control for with\_together

**URL:** <https://forum.ansible.com/t/loop-control-for-with-together/32910>\
**Category:** Ansible Project\
**Created:** [May 12, 2020, 9:42am UTC](https://forum.ansible.com/t/loop-control-for-with-together/32910 "2020-05-12T09:42:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [May 12, 2020, 9:42am UTC](https://forum.ansible.com/t/loop-control-for-with-together/32910/1 "2020-05-12T09:42:13Z")

</div>

Hi team,  
could you please help me how to give two loop\_vars for corresponding list for with\_together module

my requirement is ,i am calling a role and where looping with\_togetjher

- include\_role:  
name: validatetcp\_port  
when: item.0 == ‘tcp’  
with\_together:
- “{{ proto}}”
- “{{ svc\_port}}”  
loop\_control:  
loop\_var: svc\_port

this is not working .my inner yml is only utilizing svc\_port and for when condition I need item of proto list. in my inner yml already a loop so without loop control it is not working. in current loop\_var for inner yml its passing whole list of svc\_port as a single item.  
so please suggest me how i can apply loop\_vars

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 12, 2020, 10:12am UTC](https://forum.ansible.com/t/loop-control-for-with-together/32910/2 "2020-05-12T10:12:08Z")

</div>

You declare ‘loop\_var: svc\_port’ but that var already exists as one of the two variables of with\_together. That won’t work, you need to pick some other name.

Also, loop\_var in the context of with\_together will be a list of two vars (values from proto and svc\_port).

---

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [May 12, 2020, 3:57pm UTC](https://forum.ansible.com/t/loop-control-for-with-together/32910/3 "2020-05-12T15:57:08Z")

</div>

I tried with the other loop\_var name but didnt worked. my issues is for inner yml i wanted only port list items pass ,when using a loop\_var it is passing both list items . Please suggets how i can achieve this.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 12, 2020, 4:16pm UTC](https://forum.ansible.com/t/loop-control-for-with-together/32910/4 "2020-05-12T16:16:28Z")

</div>

Send the inner yml

---

<div class="post-metadata">

**Author:** ![Diyawary](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Diyawary](https://forum.ansible.com/u/Diyawary)\
**Post date:** [May 13, 2020, 9:10am UTC](https://forum.ansible.com/t/loop-control-for-with-together/32910/5 "2020-05-13T09:10:22Z")

</div>

Hi ,

Please find below my inner yml

- name: Checking TCP Service objects  
uri:  
url: “https://{{hostname}}/web\_api/show-objects”  
validate\_certs: False  
method: POST  
headers:  
x-chkp-sid: “{{ login.json.sid }}”  
body:  
filter: “{{item}}”  
type: “service-tcp”  
body\_format: json  
register: check\_host\_result

- name: Checking if TCP Service Object already exists  
set\_fact:  
item\_tcptype\_exists: true  
svc\_name: “{{ item[‘name’] }}”  
with\_items: “{{ check\_host\_result.json.objects }}”  
when:  
#- item[‘port’] == “{{svc\_port}}”

- item[‘port’] == “{{port}}”

- item[‘type’] is defined

- item[‘type’] == “service-tcp”

- debug:  
msg: “Service Object exists with name [{{svc\_name}}]”

and also other api call on the output of above set\_facts in this inner yml
