Ansible group_vars

Ansible group_vars
Techiio-author
Written by Nilima PaulJanuary 27, 2022
11 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
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:

Example

---
# 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.

Examples

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 127.0.0.1

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
vegetable:
tomato

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.

blogpost

In this example, we have a playbook with below

Example #4

hosts: host-one tasks:
name: Here we copy the variable value to remote
copy:
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
blogpost

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.

blogpost

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”
blogpost
  • 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/*/*
blogpost
  • The files under this directory have content like below
cat /etc/ansible/group_vars/alpha/db_settings
blogpost

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
blogpost

Conclusion

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
Ansible group_vars
Python
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

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