I can define a variable/list in my main playbook (main.yml
) or in my group variables (group_vars/all.yml
) but the main.yml
variables have precedence over the group variables.
group_vars/all.yml
my_list:
- "item 1"
- "item 2"
main.yml
vars:
my_list:
- "item 3"
- "item 4"
I would like to combine the two but am not sure how to do it. Desired:
my_list: ['item 1', 'item 2', 'item 3', 'item 4']
I have tried adding to the list. For example if main.yml
could see group_vars
before overwriting (but this example throws an error).
my_list:
- "{{ my_list }}"
- "item 3"
- "item 4"
I am doing this because I am using code from another repository. I don't want to make changes to their code, but I do want to update (not overwrite) their variable list.
You can't do exactly what you want: you can only have one variable with a given name; there's no way to merge my_list
in one place with my_list
in another place. You can replace my_list
, by doing something like:
- hosts: localhost
gather_facts: false
vars:
my_new_list_content:
- item 3
- item 4
my_new_list: "{{ my_list + my_new_list_content }}"
tasks:
- set_fact:
my_list: "{{ my_new_list }}"
- debug:
var: my_list
Assuming that group_vars/all.yaml
contains:
my_list:
- item 1
- item 2
Then running the above playbook will produce as output:
TASK [debug] ********************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"my_list": [
"item 1",
"item 2",
"item 3",
"item 4"
]
}