# Question on the multiple loop structure in my playbook

**URL:** <https://forum.ansible.com/t/question-on-the-multiple-loop-structure-in-my-playbook/38367>\
**Category:** Ansible Project\
**Created:** [August 24, 2023, 10:51am UTC](https://forum.ansible.com/t/question-on-the-multiple-loop-structure-in-my-playbook/38367 "2023-08-24T10:51:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [August 24, 2023, 10:51am UTC](https://forum.ansible.com/t/question-on-the-multiple-loop-structure-in-my-playbook/38367/1 "2023-08-24T10:51:58Z")

</div>

Hi All ,

I trying to modify the sshd\_config parameter using the lineinfile ansible module . I would like to delegate this to multiple host . As we have used the loop twice its unable to resolve the loop structure.

vars:  
ssh\_delegate\_hosts:

- “192.50.26.248”

- “192.50.27.248”

- name: Change configuration in sshd\_config

ansible.builtin.lineinfile:  
path: /etc/ssh/sshd\_config  
regexp: “{{ item.regexp }}”  
line: “{{ item.line }}”  
loop:

- { regexp: ‘^AllowTcpForwarding’, line: ‘AllowTcpForwarding yes’ }
- { regexp: ‘^AllowAgentForwarding’, line: ‘AllowAgentForwarding yes’ }  
loop: “{{ ssh\_delegate\_hosts }}”  
delegate\_to: “{{ item }}”

Need your valuable feedback of how to loop both the lines and loop the hosts .

Thanks and Regards,  
Deepak Kumar

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [August 24, 2023, 1:16pm UTC](https://forum.ansible.com/t/question-on-the-multiple-loop-structure-in-my-playbook/38367/2 "2023-08-24T13:16:24Z")

</div>

You haven’t described the actual overarching thing you’re trying to do with this, sometimes describing the larger task can be useful in terms of providing better guidance.

But reading between the lines, is there any reason you can’t place your intended delegation hosts into an inventory group and address them directly? For example if they’re getting special treatment as intended jumphosts or similar. Group them accordingly and run the bits of automation that’s unique to that intended system role against those group members.

I’d also tend to prefer a Jinja template over lineinfile for config if possible especially if you need to accommodate different conditional scenarios. It’s potentially a little more effort to start, but will save you pain in the long run.

That said, I think you can probably achieve what you’re trying to do with a product filter? (There’s probably easier ways to get the outcome you want too.)

[https://www.packetswitch.co.uk/how-to-use-ansible-loops-with-examples/](https://www.packetswitch.co.uk/how-to-use-ansible-loops-with-examples/)  
[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_loops.html#iterating-over-nested-lists](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#iterating-over-nested-lists)

Here’s a simplified example, you’d need to modify slightly to pull out the specific elements:

- name: debug vars  
hosts: localhost  
gather\_facts: no

vars:  
delegates:

- host1
- host2  
regexes:
- regex: AllowTcpForwarding  
line: AllowTcpForwarding yes
- regex: AllowAgentForwarding  
line: AllowAgentForwarding yes

tasks:

- name: Do some stuff  
debug:  
msg: “Doing {{ item.1 }} on {{ item.0 }}”  
delegate\_to: “{{ item.0 }}”  
loop: “{{ delegates | product(regexes) | list }}”

Which would do:

(ansible)$ ansible-playbook test/debug.yml

PLAY [debug vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Do some stuff] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost → host1] =\> (item=[‘host1’, {‘regex’: ‘AllowTcpForwarding’, ‘line’: ‘AllowTcpForwarding yes’}]) =\> {  
“msg”: “Doing {‘regex’: ‘AllowTcpForwarding’, ‘line’: ‘AllowTcpForwarding yes’} on host1”  
}  
ok: [localhost → host1] =\> (item=[‘host1’, {‘regex’: ‘AllowAgentForwarding’, ‘line’: ‘AllowAgentForwarding yes’}]) =\> {  
“msg”: “Doing {‘regex’: ‘AllowAgentForwarding’, ‘line’: ‘AllowAgentForwarding yes’} on host1”  
}  
ok: [localhost → host2] =\> (item=[‘host2’, {‘regex’: ‘AllowTcpForwarding’, ‘line’: ‘AllowTcpForwarding yes’}]) =\> {  
“msg”: “Doing {‘regex’: ‘AllowTcpForwarding’, ‘line’: ‘AllowTcpForwarding yes’} on host2”  
}  
ok: [localhost → host2] =\> (item=[‘host2’, {‘regex’: ‘AllowAgentForwarding’, ‘line’: ‘AllowAgentForwarding yes’}]) =\> {  
“msg”: “Doing {‘regex’: ‘AllowAgentForwarding’, ‘line’: ‘AllowAgentForwarding yes’} on host2”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [August 25, 2023, 10:57am UTC](https://forum.ansible.com/t/question-on-the-multiple-loop-structure-in-my-playbook/38367/3 "2023-08-25T10:57:46Z")

</div>

Thank you very much you have in detail describe to handle multiple loops by using the product and list in the ansible playbook . Basically this was a admin script out of box requirements as we are not include in our repository so that the reason we could not leverage the out environmental inventory.

Once again Thank you so much … I appreciate the help .

Thanks  
Deepak Kumar
