I want to specify different key for different host. but there are host in group production
. For example, I have host-vars inventory file and group-vars inventory file.
inventory/hostvars/host-01
:
secret-key: <>
inventory/group-vars/production
:
secret-key: <>
I want that if host is specified one should pick secret key from host file else from production.Is it possible in ansible? Or what approach we can have to carry this task out?
I tried to use tag but its adding extra variable while running playbook from command line
It all comes down to variable precedence.
In general, Ansible gives precedence to variables that were defined more recently, more actively, and with more explicit scope.
In your case, variables set in inventory/hostvars/host-01
(9) will override variables set in inventory/group-vars/production
(6).
You can manually change that. I explicitly set the order in some of my playbooks, like this:
- name: Run a playbook with some extra variables
hosts: all
vars_files:
- 'group_vars/override-less-important/all.yml'
- 'group_vars/override-more-important/all.yml'
You can use Jinja2 templating here, too, with extra variables.
- name: Run a playbook with some extra variables
hosts: all
vars_files:
- 'group_vars/{{ group }}/all.yml'