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