Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible doesn't pick up group_vars without loading it manually

Tags:

ansible

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 
like image 442
sdot257 Avatar asked May 20 '14 18:05

sdot257


People also ask

How does Ansible Group_vars work?

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.

Where is Group_vars in Ansible?

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', '.

Can you run an Ansible playbook without inventory?

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.

Where is Ansible inventory stored default?

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?

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.

How does Ansible load hosts and group variable files?

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.

How do I use Ansible Vars with red hat?

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.

What is the inventory file in Ansible?

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


1 Answers

You're confusing the structure a bit.

  • The group_vars directory contains files for each hostgroup defined in your inventory file. The files define variables that member hosts can use.
  • The inventory file doesn't reside in the group_vars dir, it should be outside.
  • Only hosts that are members of a group can use its variables, so unless you put 127.0.0.1 in a group, it won't be able to use any group_vars beside those defined in 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.

like image 135
hkariti Avatar answered Sep 20 '22 01:09

hkariti