# Running ansible script on same host multiple times?

**URL:** <https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985>\
**Category:** Ansible Project\
**Created:** [November 10, 2017, 6:42am UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985 "2017-11-10T06:42:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SHUBHAM\_JAIN1](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@SHUBHAM\_JAIN1](https://forum.ansible.com/u/SHUBHAM_JAIN1)\
**Post date:** [November 10, 2017, 6:42am UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/1 "2017-11-10T06:42:33Z")

</div>

For example

[brokers]  
ansible-slave node\_id=1 kafka\_port=9092  
ansible-slave node\_id=2 kafka\_port=9093  
ansible-slave node\_id=3 kafka\_port=9094

I started playbook but it is running only once not three times.

How to achieve this thing using ansible?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 10, 2017, 9:48am UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/2 "2017-11-10T09:48:51Z")

</div>

The name must be uniqe, if the appear more than once only the last one count.

To archive this you can do this.

[brokers]  
ansible-slave node\_id=1 kafka\_port=9092  
ansible-slave-v1 node\_id=2 kafka\_port=9093 ansible\_host=ansible-slave  
ansible-slave-v2 node\_id=3 kafka\_port=9094 ansible\_host=ansible-slave

But remember, if you are using inventory\_hostname it will be the name in the inventory and not ansible-slave for the two last ones.

---

<div class="post-metadata">

**Author:** ![SHUBHAM\_JAIN1](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@SHUBHAM\_JAIN1](https://forum.ansible.com/u/SHUBHAM_JAIN1)\
**Post date:** [November 11, 2017, 7:42am UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/3 "2017-11-11T07:42:12Z")

</div>

Thank you so much Kai for your solution. it will definitely help me.  
I am using inventory\_hostname so name will be different according to your solution but i wanted same name.  
I want to automate multiple kafka brokers on single node. that’s why i am looking for the solution.  
As in server.properties file name will be different so it won’t work.

i tried below thing  
[brokers]  
ansible-slave node\_id=1 kafka\_port=[9092,9093,9094]  
ansible-slave-1 node\_id=2 kafka\_port=[9092,9093,9094]

But i am not able to iterate through kafka\_port list with **with\_items** looping construct.  
One more thing i need to do i.e. broker.id where i need to start from value 1 and end to a number which is total number of brokers. How can i achive this? Is there any notion of global variable which i can update and use further.  
For example in above case you can see i want to have three brokers on ansible-slave node on ports 9092,9093,9094 and three brokers on ansible-slave-1 node port 9092,9093,9094 so total brokers count 6 so i want to assign broker.id 1 to 3 in the ansible-slave host and 4 to 6 in ansible-slave-1 host.

Thanks in advance  
Shubham Jain

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 12, 2017, 2:39pm UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/4 "2017-11-12T14:39:17Z")

</div>

> Thank you so much Kai for your solution. it will definitely help me.  
> I am using inventory\_hostname so name will be different according to your  
> solution but i wanted same name.  
> I want to automate multiple kafka brokers on single node. that's why i am  
> looking for the solution.  
> As in server.properties file name will be different so it won't work.
> 
> i tried below thing  
> [brokers]  
> ansible-slave node\_id=1 kafka\_port=[9092,9093,9094]  
> ansible-slave-1 node\_id=2 kafka\_port=[9092,9093,9094]
> 
> But i am not able to iterate through kafka\_port list with \*with\_items\*  
> looping construct.

I don't think you can specify a list in the inventory, you would need to use host\_vars/ folder for that.  
Or you can do this "kafka\_port=9092,9093,9094" and the with\_items will be

with\_items: "{{ kafka\_port.split(',') }}"

> One more thing i need to do i.e. broker.id where i need to start from value  
> 1 and end to a number which is total number of brokers. How can i achive  
> this? Is there any notion of global variable which i can update and use  
> further.  
> For example in above case you can see i want to have three brokers on  
> ansible-slave node on ports 9092,9093,9094 and three brokers on  
> ansible-slave-1 node port 9092,9093,9094 so total brokers count 6 so i want  
> to assign broker.id 1 to 3 in the ansible-slave host and 4 to 6 in  
> ansible-slave-1 host.

When you are using the with\_items above or something simular you could calculate it, the item is the kafka\_port

{{ item - 9091 + (node\_id - 1) \* 3 }}

---

<div class="post-metadata">

**Author:** ![SHUBHAM\_JAIN1](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@SHUBHAM\_JAIN1](https://forum.ansible.com/u/SHUBHAM_JAIN1)\
**Post date:** [November 13, 2017, 5:40pm UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/5 "2017-11-13T17:40:50Z")

</div>

**Thank you so much kai. your solution made my day and it is totally great and worked out finally.**

One more Question - Can we use dictionary in inventory hosts section like below and access it in task using **with\_dict** and **item.key** and **item.value** as i have tried this but didn’t get success  
[brokers]  
ansible-slave node\_id=1 kafka\_dict={1:9092,2:9093,3:9094}  
ansible-slave-1 node\_id=2 kafka\_dict={1:9092,2:9093,3:9094,4:9095}

Then i declared them in variables section then i got success  
[brokers]  
ansible-slave  
ansible-slave-1

[brokers:vars]  
kafka\_dict={1:9092,2:9093,3:9094}

I was able to use then item.key and item.value with with\_dict construct and everything worked fine but now i need to add different dictionary of ansible-slave-1 host how can i add that since this dictionary is common?

I tried adding one more kafka\_dict with different values in brokers:vars but one of the kafka\_dict is used in playbook for both of hosts i.e. last one. for example like below

[brokers]  
ansible-slave  
ansible-slave-1

[brokers:vars]  
kafka\_dict={1:9092,2:9093,3:9094}

kafka\_dict={1:9092,2:9093,3:9094,4:9094}

Can we define a host specific variables?

Regards,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 13, 2017, 8:31pm UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/6 "2017-11-13T20:31:34Z")

</div>

> \*Thank you so much kai. your solution made my day and it is totally great  
> and worked out finally.\*
> 
> One more Question - Can we use dictionary in inventory hosts section like  
> below and access it in task using \*with\_dict\* and \*item.key\* and  
> \*item.value\* as i have tried this but didn't get success  
> [brokers]  
> ansible-slave node\_id=1 kafka\_dict={1:9092,2:9093,3:9094}  
> ansible-slave-1 node\_id=2 kafka\_dict={1:9092,2:9093,3:9094,4:9095}

I tried this and it works in my test, I also tried to use a list, and that also worked.  
Are you using correct syntax?

&nbsp;&nbsp;- debug: msg='KEY {{ item.key}} VALUE {{ item.value}}'  
&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: '{{ kafka\_dict }}'

> Then i declared them in variables section then i got success  
> [brokers]  
> ansible-slave  
> ansible-slave-1
> 
> [brokers:vars]  
> kafka\_dict={1:9092,2:9093,3:9094}
> 
> I was able to use then item.key and item.value with with\_dict construct and  
> everything worked fine but now i need to add different dictionary of  
> ansible-slave-1 host how can i add that since this dictionary is common?
> 
> I tried adding one more kafka\_dict with different values in brokers:vars  
> but one of the kafka\_dict is used in playbook for both of hosts i.e. last  
> one. for example like below  
> [brokers]  
> ansible-slave  
> ansible-slave-1
> 
> [brokers:vars]  
> kafka\_dict={1:9092,2:9093,3:9094}  
> kafka\_dict={1:9092,2:9093,3:9094,4:9094}
> 
> Can we define a host specific variables?

In the inventory it's specified on the same line as the host.  
But you can use host\_vars/ansible-slave.yml file instead.

---

<div class="post-metadata">

**Author:** ![SHUBHAM\_JAIN1](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@SHUBHAM\_JAIN1](https://forum.ansible.com/u/SHUBHAM_JAIN1)\
**Post date:** [November 17, 2017, 8:24pm UTC](https://forum.ansible.com/t/running-ansible-script-on-same-host-multiple-times/25985/7 "2017-11-17T20:24:30Z")

</div>

Thank you so much kai you helped me for all my queries.
