In my local.yml
I'm able to run the playbook and reference variables within group_vars/all
however I'm not able to access variables within group_vars/phl-stage
. Let's assume the following.
ansible-playbook -i phl-stage site.yml
I have a variable, let's call it deploy_path
that's different for each environment. I place the variable within group_vars/< environment name >
. If I include the file group_vars/phl-stage
within vars_files
it works but I would've thought the group file would be automatically loaded?
site.yml
- include: local.yml
local.yml
- hosts: 127.0.0.1 connection: local vars_files: - "group_vars/perlservers" - "group_vars/deploy_list"
group_vars/phl-stage
[webservers] phl-web1 phl-web2 [perlservers] phl-perl1 phl-perl2 [phl-stage:children] webservers perlservers
Directory structure:
group_vars all phl-stage phl-prod site.yml local.yml
The group_vars in Ansible are a convenient way to apply variables to multiple hosts at once. Group_vars is an Ansible-specific folder as part of the repository structure. This folder contains YAML files created to have data models, and these data models apply to all the devices listed in the hosts. ini file.
In this, group_vars directory is under Ansible base directory, which is by default /etc/ansible/. The files under group_vars can have extensions including '. yaml', '. yml', '.
So, it seems this isn't widely used or documented, but it is possible to run a playbook without passing through an inventory file. It's very handy when trying to deploy hosts but you don't want to need to manage static files with host entries.
The default location for the inventory file is /etc/ansible/hosts. You can also create project-specific inventory files in alternate locations. The inventory file can list individual hosts or user-defined groups of hosts.
How Does ANSIBLE group_vars work? In this, group_vars directory is under Ansible base directory, which is by default /etc/ansible/. The files under group_vars can have extensions including ‘.yaml’, ‘.yml’, ‘.json’ or no file extension.
It loads hosts and group variable files by searching path which is relative to inventory file or playbook file. So which means if you have your hosts file at location /etc/ansible/hosts and there are three groups names grp1, grp2, and grp3 in the hosts’ file.
Red Hat subscribers, select 2.9 in the version selection to the left for the most recent Red Hat release. This vars plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name host_group_vars even without specifying the collections: keyword.
The inventory file is /etc/ansible/hosts. The hosts file /etc/ansible/hosts have same groups named and the hosts mapping is like below In this example, we put contents like below under /etc/ansible/group_vars/alpha to define variables and other two files
You're confusing the structure a bit.
group_vars
directory contains files for each hostgroup defined in your inventory file. The files define variables that member hosts can use.group_vars
dir, it should be outside.group_vars/all
. What you want is this dir structure:
group_vars/ all perlservers phl-stage hosts site.yml local.yml
Your hosts file should look like this, assuming 127.0.0.1 is just a staging server and not perl or web server:
[webservers] phl-web1 phl-web2 [perlservers] phl-perl1 phl-perl2 [phl-stage] 127.0.0.1 [phl-stage:children] webservers perlservers
So you define which hosts belong to which group in the inventory, and then for each group you define variables in its group_vars file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With