Ansible group_vars

Ansible group_vars
Written by Nilima PaulJanuary 27, 2022
11 min read
Nilima Paul

Technology Security Analyst

In this blog post. We will discuss Ansible group_vars.

About Ansible group_vars

In Ansible, we realize that factors are vital as they store have-to-have information and are usable to send a few errands in light of remote host's state-based. There are different ways of characterizing factors like in the stock record, in the playbook document, in a variable record imported in the playbook. Essentially, there is a method for characterizing factors at an area from where the factors will be acknowledged for a gathering of hosts. This area falls under group_vars and this can be under a predefined area. In this article, we will find out about group_vars for certain models and ways of utilizing them.

What is Ansible group_vars?

It utilizes has record and group_vars registry to set factors for having gatherings and conveying Ansible plays/assignments against each host/bunch. Records under group_var registry are named after the host gathering's name or all, in like manner, the factors will be allocated to that host bunch or every one of the hosts.

Even though you can store the factors in stock records and playbooks, having put away and characterized factors in discrete documents and that too on a host bunch premise, adds straightforwardness and makes it simple to perceive, then, at that point, use.

Records under group_vars are conventionally used to store fundamental distinguishing factors and afterward influence these with other positive factors by utilizing include_vars and var_files.

How Does ANSIBLE group_vars work?

In this, group_vars catalog is under Ansible base registry, which is naturally/and so forth/ansible/. The records under group_vars can have augmentations including '.yaml', '.yml', '.json' or no document expansion.

It loads has and bunch variable records via looking through the way which is comparative with the stock document or playbook document. So that implies assuming you have your hosts document at area/and so on/ansible/hosts and there are three gatherings names grp1, grp2, and grp3 in the hosts' record. Then, at that point, under three records will be stacked for a host, which has a place with every one of the three gatherings, is focused on for playbook execution.

Additionally, different mixes of these documents are utilized, in light of the host's planning to bunches in the host's record.

  • /and so on/ansible/group_vars/grp1.yaml
  • /and so on/ansible/group_vars/grp2.yml
  • /and so on/ansible/group_vars/grp3

In tIfre is document/and so on/ansible/group_vars/all current factors in this record will be pulled for each execution. The meaning of factors in these records is straightforward key: esteem pair design. A model resembles underneath:


# file location is
/etc/ansible/group_vars/grp1 fruit: apple
vegetable: potato

Also, there are a few points that should be noted when using group_vars:

  • We can make indexes under the gathering named registry, it will peruse every one of the records under these catalogs in lexicographical
  • Assuming some worth is valid for your current circumstance and each server, the variable containing that worth ought to be characterized under/and so forth/ansible/group_vars/all document
  • In AWS AWS, when we have a powerful stock where host bunches are made and taken out naturally, we want to label the EC2 examples like "class: webserver". Then, at that point, factors characterized under the/and so forth/ansible/group_vars/ec2_tag_class_webserver will be found document.
  • On the off chance that in has documented, bunches are coordinated in such a request that one gathering is an offspring of another, then, at that point, the factors characterized for kids will have higher priority over the variable with a similar name characterized for the parent
  • At the point when a similar host is characterized for a considerable length of time on a similar level of the parent-youngsters progressive system, the variable document priority with being on the name of gatherings in alphabetic request. That implies assuming a host is planned under bunches alpha, beta, and gamma. Then, at that point, for this have factors under/and so forth/ansible/group_vars/gamma will be pulled.
  • We can involve Ansible Vault for these records under group_vars, to ensure private information.


Presently by utilizing models, we will attempt to find out with regards to Ansible group_vars, which you may need to use in everyday tasks. We will take a few models, however, priobeforeng there, we will initially comprehend our lab, which we will use for testing reasons.

Here it control server is named ansible-regulator and two controllers have named have one and host-two. We will make playbooks and run orders on the ansible-regulator hub and see the outcomes on remote hosts.

Additionally, group_vars registry is characterized as/and so on/ansible/group_vars. The stock document is/and so forth/ansible/has.

We made three records named as underneath: -

  • /and so forth/ansible/group_vars/alpha
  • /and so forth/ansible/group_vars/beta
  • /and so forth/ansible/group_vars/gamma

The host's record/and so on/ansible/has had same gatherings named and the hosts planning resembles underneath

Example #1

[alpha] host-one
[beta] host-two
[gamma] host-one host-two

In this model, we put substance like beneath under/and so forth/ansible/group_vars/alpha to characterize factors and other two records

  • /and so forth/ansible/group_vars/beta and
  • /and so forth/ansible/group_vars/gamma are unfilled.

Example #2

fruit: apple

Now running debug module in ad-hoc command like below:

Example #3

ansible beta -m debug -a "var=fruit"
ansible gamma -m debug -a "var=fruit"

We get the following output where we can see that only variable defined in the file /etc/ansible/group_vars/alpha for alpha group means the host-one host will be pulled.


In this example, we have a playbook with below

Example #4

hosts: host-one tasks:
name: Here we copy the variable value to remote
content: "fruit variable valus is {{ fruit }}\n" dest: /tmp/sample.ini

Also, group_vars directory is defined as /etc/ansible/group_vars and the files with content under this directory are listed as below:

cat /etc/ansible/group_vars/alpha
cat /etc/ansible/group_vars/beta
cat /etc/ansible/group_vars/gamma

When running playbook like below

Example #5

ansible-playbook ansible_group_vars.yaml

We get the following output where the variable value is copied to a file on the target host.


On checking, we will see that the value of the variable is pulled from

  • /etc/ansible/group_vars/gamma.
  • ssh host-one “cat /tmp/sample.ini”
  • In this example, we have a playbook with the below content. Using this playbook, we try to print variables from various directories in a hierarchy under /etc/ansible/group_vars.

Example #6

hosts: host-one tasks:
name: Here we print the variables from different debug:
msg: "Username is {{ username }} and connection port is {{ port }}"
  • Also, group_vars directory is defined as /etc/ansible/group_vars, which have files like below
ll /etc/ansible/group_vars/*/*
  • The files under this directory have content like below
cat /etc/ansible/group_vars/alpha/db_settings

cat /etc/ansible/group_vars/Charlie/connection_setting
  • When running playbook like below
ansible-playbook ansible_group_vars_dir.YAML
  • We get the following output where


As we can find in this article, utilizing group_vars I simple to characterize and can be extremely useful in situations where we want to deal with a gathering of hosts. This not just saves our endeavors to sort the hosts yet additionally improves the adaptability in our code. Additionally, the reusability of these gathering factors makes it more pertinent to use in a creative climate.

Ansible group_vars
Was this blog helpful?
You must be Logged in to comment
Code Block
Nilima Paul
Technology Security Analyst
201 Blog Posts
0 Discussion Threads
Trending Technologies
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Penetration Testing16

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance