Reputation: 1374
In this older question about "Can extra_vars
receive multiple files?", the original poster answered the question, saying that multiple vars files could be accomplished by just using multiple --extra-vars
parameters.
The followup question that I have is that, in such a case, where the ansible-playbook
command line has two --extra-vars
parameters, each pointing to a different file, what is the order or precedence of those files?
Also, what happens if both files have the same var name (e.g., my_host
) in them?
For example, say I have 2 files, extraVars1.yml
and extraVars2.yml
and in the ansible-playbook
command line I have:
ansible-playbook... --extra-vars "@extraVars1.yml" --extra-vars "@extraVars2.yml"
and the extraVars1.yml
file has:
my_host: 1.2.3.4
and the extraVars2.yml
file has:
my_host: 5.6.7.8
What will the value of the my_host
var be when the playbook is run?
Thanks! Jim
Upvotes: 4
Views: 2176
Reputation: 12124
According the Ansible documentation about Using Variables and Understanding variable precedence
extra vars
(for example,-e "user=my_user"
) (always win precedence)In general, Ansible gives precedence to variables that were defined more recently ...
This means the last defined wins.
Lets have a short test here with a vars.yml
playbook.
---
- hosts: localhost
become: false
gather_facts: false
vars:
my_host: 9.0.0.0
tasks:
- name: Show value
debug:
msg: "{{ my_host }}"
The execution of ansible-playbook vars.yml
will result into an output of
TASK [Show value] ***
ok: [localhost] =>
msg: 9.0.0.0
The execution of ansible-playbook -e "@extraVars1.yml" vars.yml
will result into an output of
TASK [Show value] ***
ok: [localhost] =>
msg: 1.2.3.4
The execution of ansible-playbook -e "@extraVars1.yml" -e "@extraVars2.yml" vars.yml
will result into an output of
TASK [Show value] ***
ok: [localhost] =>
msg: 5.6.7.8
The execution of ansible-playbook -e "@extraVars2.yml" -e "@extraVars1.yml" vars.yml
will result into an output of
TASK [Show value] ***
ok: [localhost] =>
msg: 1.2.3.4
Upvotes: 4