How to call registered variable data in loops?

60 views
Skip to first unread message

Aharonu

unread,
Mar 3, 2023, 1:25:45 PM3/3/23
to ansible...@googlegroups.com
Hi Team,

Could anyone please help on below query? Thank you.

I have 2 registered variables data as example below. How to call those data over the loop?

Vars:
    Cluster_data:  ['cluster1','cluster2']
    Aggr_data:  ['aggr1','aggr2',]
 Tasks:
     - name: calling var data
       <module_name>:
           cluster: ??<item>    --> here, I need to use 'Cluster_data' list
           aggregate: ??<item>  --> here, I need to use 'Aggr_data' list
        loop: ??
        
Once again, thank you for your help.
    

Todd Lewis

unread,
Mar 3, 2023, 5:02:25 PM3/3/23
to ansible...@googlegroups.com, uto...@gmail.com
Your question can be interpreted at least two ways.
Here are two answers, one of which may be what you're looking for.
	---
	- name: Combining lists
	  hosts: localhost
	  gather_facts: false
	
	  vars:
	    cluster_data: ['cluster1','cluster2']
	    aggr_data: ['aggr1','aggr2',]
	
	  tasks:
	    - name: Combine with zip
	      ansible.builtin.debug:
	        msg: "zip: {{ ','.join(item) }}"
	      loop: "{{ cluster_data | zip(aggr_data) }}"
	
	    - name: Combine with product
	      ansible.builtin.debug:
	        msg: "product: {{ ','.join(item) }}"
	      loop: "{{ cluster_data | product(aggr_data) }}"
The output from the above playbook looks like this:
	PLAY [Combining lists] *****************************
	
	TASK [Combine with zip] ****************************
	ok: [localhost] => (item=['cluster1', 'aggr1']) => {
	    "msg": "zip: cluster1,aggr1"
	}
	ok: [localhost] => (item=['cluster2', 'aggr2']) => {
	    "msg": "zip: cluster2,aggr2"
	}
	
	TASK [Combine with product] ************************
	ok: [localhost] => (item=['cluster1', 'aggr1']) => {
	    "msg": "product: cluster1,aggr1"
	}
	ok: [localhost] => (item=['cluster1', 'aggr2']) => {
	    "msg": "product: cluster1,aggr2"
	}
	ok: [localhost] => (item=['cluster2', 'aggr1']) => {
	    "msg": "product: cluster2,aggr1"
	}
	ok: [localhost] => (item=['cluster2', 'aggr2']) => {
	    "msg": "product: cluster2,aggr2"
	}

Reply all
Reply to author
Forward
0 new messages