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://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 On Thu, 24 Aug 2023 at 11:52, Deepak B K <[email protected]> wrote: > 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 > > -- > 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 view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/5cd5230e-f36e-4990-b0ae-2820be5e2d0dn%40googlegroups.com > <https://groups.google.com/d/msgid/ansible-project/5cd5230e-f36e-4990-b0ae-2820be5e2d0dn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAKtKohR7T%3DvYOwPptmUgjKdUHMuSqZe04XbHA0A-z6PhrJuZug%40mail.gmail.com.
