# Issues generating dynamic host in Ansible loop.

**URL:** <https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345>\
**Category:** Ansible Project\
**Created:** [September 24, 2019, 6:44am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345 "2019-09-24T06:44:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 24, 2019, 6:44am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345/1 "2019-09-24T06:44:58Z")

</div>

I have an ansible play that fetches multiline data from the database and registers it a variable “command\_result”

command\_result variable has has data like below.

`  
host6,host5\targ3  
host4,host3\targ1

host1,host2,host5\targ2  
…  
…

`

I need to loop through each line of command\_result. Pick the host list for example host6,host5 and execute shell script using shell module by passing respective argument string i.e arg3.

Likewise, the second shell script should run on host4 & host3 and pass arg1 to the shell script.

The final shell script hould run on host1, host2 & host5 and pass arg2 to the shell script.

`

- name: Add hosts  
add\_host:  
name: “{{ item.split(‘\t’)[0] }}”  
file\_dets: “{{ item.split(‘\t’)[1] }}”

ansible\_host: localhost

ansible\_connection: local  
groups: dest\_nodes  
with\_items: “{{ command\_result.stdout\_lines }}”

- hosts: dest\_nodes  
gather\_facts: false  
tasks:
- debug:  
msg: Run the shell script with the arguments `{{ file_dets }}` here"  
`

This works fine when there is a single host in item.split(‘\t’)[0]

However, I do not know the approach when there are multiple hosts like host6,host5 for {{ item.split(‘\t’)[0] }}

---

<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:** [September 24, 2019, 6:58am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345/2 "2019-09-24T06:58:56Z")

</div>

Instead of using add\_host, use include\_tasks to include a second task file. In there, iterate over a “split by comma” list and adds hosts.

This gives you the second level of iteration.

Dick

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 24, 2019, 8:20am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345/3 "2019-09-24T08:20:42Z")

</div>

@Dick; I need more help please.

This is what I did but it is still failing.

`- name: Add hosts`  
`include_tasks: "{{ playbook_dir }}/gethosts.yml"`  
`dest_ip: "{{ item.split('\t')[0] }}"`  
`groups: dest_nodes`  
`file_dets: "{{ item.split('\t')[1] }}"`  
`ansible_host: localhost`  
`ansible_connection: local`  
`with_items: "{{ command_result.stdout_lines }}"`

And below is my get\_hosts.yml file

`add_host:`  
`name: {{ item }}`  
`with_items: "{{ dest_ip.split(',') }}"`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 24, 2019, 8:31am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345/4 "2019-09-24T08:31:05Z")

</div>

Given the input is a list 'db\_lines' normalise the data first. For example  
the loop below

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;separator: '\t'  
&nbsp;&nbsp;&nbsp;&nbsp;db\_lines:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'host6,host5\targ3'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'host4,host3\targ1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'host1,host2,host5\targ2'  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_data: "{{ my\_data|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[{'host': item.split(separator)[0].split(','),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'arg': item.split(separator)[1].split(',')}] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ db\_lines }}"

gives

&nbsp;&nbsp;my\_data:  
&nbsp;&nbsp;- arg:  
&nbsp;&nbsp;&nbsp;&nbsp;- arg3  
&nbsp;&nbsp;&nbsp;&nbsp;host:  
&nbsp;&nbsp;&nbsp;&nbsp;- host6  
&nbsp;&nbsp;&nbsp;&nbsp;- host5  
&nbsp;&nbsp;- arg:  
&nbsp;&nbsp;&nbsp;&nbsp;- arg1  
&nbsp;&nbsp;&nbsp;&nbsp;host:  
&nbsp;&nbsp;&nbsp;&nbsp;- host4  
&nbsp;&nbsp;&nbsp;&nbsp;- host3  
&nbsp;&nbsp;- arg:  
&nbsp;&nbsp;&nbsp;&nbsp;- arg2  
&nbsp;&nbsp;&nbsp;&nbsp;host:  
&nbsp;&nbsp;&nbsp;&nbsp;- host1  
&nbsp;&nbsp;&nbsp;&nbsp;- host2  
&nbsp;&nbsp;&nbsp;&nbsp;- host5

It should be trivial to use it in the loops.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 24, 2019, 9:21am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345/5 "2019-09-24T09:21:40Z")

</div>

The input data is not a fixed list but can be any number of lines returned by the database and registered by variable “command\_result” as below":

`

- name: “Play 1”  
hosts: localhost  
tasks:
- name: “Search DB”  
command: \> mysql --user=root --password=p@ssword deployment  
–host=localhost -Ns -e “SELECT dest\_ip,file\_dets FROM deploy\_dets WHERE num LIKE ‘{{ CR\_Number }}’”  
register: command\_result  
`

Kindly suggest.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 24, 2019, 10:02am UTC](https://forum.ansible.com/t/issues-generating-dynamic-host-in-ansible-loop/31345/6 "2019-09-24T10:02:14Z")

</div>

Can you suggest what is the issue with this code ?

`- name: Add hosts`  
`include_tasks: "{{ playbook_dir }}/gethosts.yml"`  
`dest_ip: "{{ item.split('\t')[0] }}"`  
`groups: dest_nodes`  
`file_dets: "{{ item.split('\t')[1] }}"`  
`ansible_host: localhost`  
`ansible_connection: local`  
`with_items: "{{ command_result.stdout_lines }}"`

And below is my get\_hosts.yml file

`add_host:`  
`name: "{{ item }}"`  
`with_items: "{{ dest_ip.split(',') }}"`
