Reputation: 428
I am trying to fetch the variable value from group_vars but ansible is unable to locate it from the default location /etc/ansible
. Here are the details.
Ansible Version
[admin@ansicontrol newvarsexample]$ ansible --version
ansible 2.9.18
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.2 (default, Feb 28 2020, 00:00:00) [GCC 10.0.1 20200216 (Red Hat 10.0.1-0.8)]
Ansible Inventory file /etc/ansible/hosts
:
[stack]
192.168.47.130
Ansible Playbook fedoragroupvarfile.yaml
:
- hosts: all
remote_user: admin
tasks:
- name: Set OS distribution dependent variables
include_vars: "os_{{ ansible_facts['distribution'] }}.yaml"
- debug:
var: fedcharm
Ansible group_vars file /etc/ansible/group_vars/os_Fedora.yaml
:
fedcharm: "This is Fedora 32 OS variable file"
Error
TASK [Set OS distribution dependent variables] *******************************************************************************
fatal: [192.168.47.130]: FAILED! => {"ansible_facts": {}, "ansible_included_var_files": [], "changed": false, "message": "Could not find or access 'os_Fedora.yaml'\nSearched in:\n\t/home/admin/practiceansible/newvarsexample/vars/os_Fedora.yaml\n\t/home/admin/practiceansible/newvarsexample/os_Fedora.yaml\n\t/home/admin/practiceansible/newvarsexample/vars/os_Fedora.yaml\n\t/home/admin/practiceansible/newvarsexample/os_Fedora.yaml on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
Why does include_vars
try to find the variables file in the current playbook's directory rather than the default /etc/ansible/group_vars
location where the file is located.
Upvotes: 2
Views: 3227
Reputation: 4554
The "default location" is where ansible looks for the inventory if you don't specify one with -i /path/to/inventory.yaml
, but a file you include with include_vars
is not an inventory, but only contains bare variables.
If you use include_vars
with a relative path, ansible will look for the file in a location relative to the playbook/role (see documentation).
You can specify an absolute path in include_vars
as well. E.g. /etc/ansible/variables.yaml
(the file has to exist).
But actually, I don't think that is what you want. If you have your variables in /etc/ansible/group_vars
, they will be loaded by default without include_vars
if you structure your directories correctly. Check the docs.
Place a file containing this line in /etc/ansible/group_vars/stack.yaml
:
fedcharm: "This is Fedora 32 OS variable file"
And fedoragroupvarsfile.yaml
should contain this:
- hosts: all
remote_user: admin
tasks:
- debug:
var: fedcharm
Then run your playbook as before. If you did everything correctly, ansible will load the variables for the group stack
in your inventory from /etc/ansible/group_vars/stack.yaml
and display the correct message.
Upvotes: 2