I would like to loop on variables that are in a vars.yml
file but with an identical id here is my example:
In my playbook :
- name: Mise en conformité avec la CMDB
replace:
path: "{{ playbook_dir }}/results/wifi.csv"
regexp: '{{ item.syslocation }}'
replace: '{{ item.emplacement_cmdb }}'
with_items:
- { syslocation: "{{ syslocation_var }}", emplacement_cmdb: "{{ emplacement_cmdb_var }}" }
In my var file:
syslocation_var: ;test;
emplacement_cmdb_var: ;newtest;
syslocation_var: ;test2;
emplacement_cmdb_var: ;newtest2;
I would like to do the same thing :
- name: Mise en conformité avec la CMDB
replace:
path: "{{ playbook_dir }}/results/wifi.csv"
regexp: '{{ item.syslocation }}'
replace: '{{ item.emplacement_cmdb }}'
with_items:
- { syslocation: ;test;, emplacement_cmdb: ;newtest; }
- { syslocation: ;test2;, emplacement_cmdb: ;nextest2; }
But in var file
I hope I'm understandable enough...
Thks
Yaml is.... yaml. A dict in yaml is... a dict. You cannot have duplicated keys in a dict although it is not an error: a linter will simply issue a warning except that the last definition wins. So for essence:
a: 1
a: 2
a: 3
results in
a: 3
Now, you say that you want to do the same thing as in:
with_items:
- { syslocation: ;test;, emplacement_cmdb: ;newtest; }
- { syslocation: ;test2;, emplacement_cmdb: ;nextest2; }
You are actually looping on a list of dicts which could also be written as:
with_items:
- syslocation: ;test;
emplacement_cmdb: ;newtest;
- syslocation: ;test2;
emplacement_cmdb: ;nextest2;
You simply have to define a var in your vars.yml
file:
mes_entrees_cmdb:
- syslocation: ;test;
emplacement_cmdb: ;newtest;
- syslocation: ;test2;
emplacement_cmdb: ;nextest2;
Then load that file either in a vars_files
section in your play or in a task with include_vars
. And finally loop on that variable in your task:
- name: Mise en conformité avec la CMDB
replace:
path: "{{ playbook_dir }}/results/wifi.csv"
regexp: '{{ item.syslocation }}'
replace: '{{ item.emplacement_cmdb }}'
with_items: "{{ mes_entrees_cmdb }}"