I am dealing with nested loops inorder to build dynamic host using add_host.
Outer Loop: with_items: “{{ command_result.stdout_lines }}” // gets me the list of users
Inner Loop: with_items: “{{ dest_ip.split(‘,’) }}” // gets me the list of IP addresses seperated by comma (,)
The below code works fine.
`
- name: Add hosts
include_tasks: “{{ playbook_dir }}/gethosts.yml”
vars:
dest_ip: “{{ item.split(‘\t’)[0] }}”
file_dets: “{{ item.split(‘\t’)[1] }}”
USER: “anystring”
USER: “{% if item.split(‘\t’)[3] == ‘FrontEnd’ %}user1{% elif item.split(‘\t’)[3] == ‘BackEnd’ %}user2{% else %}{% endif %}”
ansible_host: localhost
install_dir_pass: “anystring”
install_dir: “{{ item.split(‘\t’)[2] }}”
with_items: “{{ command_result.stdout_lines }}”
`
Below is my include_task gethost.yml file:
`