Running ansible script on same host multiple times?

1,092 views
Skip to first unread message

SHUBHAM JAIN

unread,
Nov 10, 2017, 1:42:33 AM11/10/17
to Ansible Project
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?


Kai Stian Olstad

unread,
Nov 10, 2017, 4:49:15 AM11/10/17
to ansible...@googlegroups.com
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.


--
Kai Stian Olstad

SHUBHAM JAIN

unread,
Nov 11, 2017, 2:42:12 AM11/11/17
to Ansible Project
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

Kai Stian Olstad

unread,
Nov 12, 2017, 9:39:42 AM11/12/17
to ansible...@googlegroups.com
On lørdag 11. november 2017 08.42.12 CET SHUBHAM JAIN wrote:
> 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 }}


--
Kai Stian Olstad

SHUBHAM JAIN

unread,
Nov 13, 2017, 12:40:50 PM11/13/17
to Ansible Project
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,

Kai Stian Olstad

unread,
Nov 13, 2017, 3:32:03 PM11/13/17
to ansible...@googlegroups.com
On Monday, 13 November 2017 18.40.50 CET SHUBHAM JAIN wrote:
> *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?

- debug: msg='KEY {{ item.key}} VALUE {{ item.value}}'
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.


--
Kai Stian Olstad

sjain

unread,
Nov 17, 2017, 3:24:30 PM11/17/17
to Ansible Project
Thank you so much kai you helped me for all my queries.
Reply all
Reply to author
Forward
0 new messages