I have two variable files app1.yml
and app2.yml
having the same variable name viz dbconn
cat app1.yml
dbconn:
- host1 port1
cat app2.yml
dbconn:
- host4 port4
- host5 port5
cat main.yml
- name: Load Variable Files
include_vars: "{{ playbook_dir }}/{{ item }}.yml"
loop:
- app1
- app2
run_once: yes
- debug:
msg: "{{ dbconn }}"
My expectation is that variable dbconn
to have values from both the variable files i.e
- host1 port1
- host4 port4
- host5 port5
However, it prints only the last loaded variable file values i.e - host4 port4
and - host5 port5
Can you please suggest?
CodePudding user response:
Concatenate the lists in the loop. For example,
- set_fact:
dbconn: "{{ dbconn|d([]) (lookup('file', item)|from_yaml).dbconn }}"
loop:
- app1.yml
- app2.yml
gives
dbconn:
- host1 port1
- host4 port4
- host5 port5
CodePudding user response:
you have a specific problem so this playbook is a solution to resolve your problem:
- name: "tips3"
hosts: localhost
vars:
appx: [app1, app2]
tasks:
- name: Load Variable Files
include_vars:
file: "{{ playbook_dir }}/{{ item }}.yml"
name: "{{ item }}"
loop: "{{ appx }}"
run_once: yes
- name: group var
set_fact:
dbconn: "{{dbconn | d([]) lookup('vars', item )['dbconn'] }}"
loop: "{{ appx }}"
- debug:
msg: "{{ dbconn }}"
the first task gives as variables:
app1:
dbconn:
- host1 port1
app2:
dbconn:
- host4 port4
- host5 port5
and the second task concats all lists in one list dbconn:
final result:
ok: [localhost] => {
"msg": [
"host1 port1",
"host4 port4",
"host5 port5"
]
}