Skip to content
Welcome To Charanjit Cheema Blog

Welcome To Charanjit Cheema Blog

An Open Source and Cloud Blog

Menu
  • Home
  • About Me!
  • Way to my Technical Blog
  • Contact me
  • Privacy Policy
Menu

What are Ansible Host and Group Variables

Posted on August 28, 2023September 7, 2023 by Charanjit Singh

Variables or Vars are used to store the data which is further used by ansible playbook. These variables can either take input from user or it can also be stored in YAML or INI file as well like main.yml or inventory. Variables stored in either playbook or in separate YAML or INI format file can be read and invoked by ansible playbook anytime during task execution these variables can be used to control the behavior of playbook jobs and provide data during endpoint configuration as well. In Ansible variables have precedence rule which help Ansible playbook to look for data accordingly. Below is the list of ansible variables location which have least to greatest precedence:

 

  • command line values (for example, -u my_user, these are not variables)
  • role defaults (defined in role/defaults/main.yml)
  • inventory file or script group vars
  • inventory group_vars/all
  • playbook group_vars/all
  • inventory group_vars/*
  • playbook group_vars/*
  • inventory file or script host vars
  • inventory host_vars/*
  • playbook host_vars/*
  • host facts / cached set_facts
  • play vars
  • play vars_prompt
  • play vars_files
  • role vars (defined in role/vars/main.yml)
  • block vars (only for tasks in block)
  • task vars (only for the task)
  • include_vars
  • set_facts / registered vars
  • role (and include_role) params
  • include params
  • extra vars (for example, -e “user=my_user”)(always win precedence)

 

Source: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html

 Host Variables

Host Variables can be created directly in the inventory file in INI format or for managing host variables in an efficient way the Host variables can be created in separate YAML file with the same endpoints hostname file name which can be stored in host_vars folder in an Ansible project. As you can see in an example below NTP server variables are defined as per webservers region by using two methods:

  • NTP server variables are defined in a host inventory. You can see I have defined each region based NTP servers name in inventory file in INI format.

  • Below is another example of host variables which is recommended way for managing host variables efficiently here, you can see I have created separate YAML files with the name of webservers hostname under host_vars folder which contain each region NTP servers detail.

Below is the example of NTP sync ansible playbook code in which you can see I am invoking the host variables:

Using host variables in such condition where host has distinct variables is a good practice but in real-world scenario as per above example if count of endpoints is high and are scattered region wise then creating and managing the host variables will be difficult hence for addressing this situation Group Variables comes in the picture.

 

Group Variables

Group Variables (group_vars) read variables from YAML file which is stored under group_vars folder like host_vars folder group_vars folder also need to be created inside the Ansible Project. Group Variables read variables which are associated with endpoints group. The difference between host variables and group variables is that host variable is associated with a specific host or endpoint whereas group variables are associated with endpoints group. Group variables YAML file name should be matched with Group name which is created in inventory file.

A real-world scenario is to use group variables for endpoints which are scattered as per geographical, or region wise each region endpoints geographically or regionally grouped, and the variables are defined for those groups under group_var. As referring to previous example below webservers are grouped as per region wise their variables are also defined and stored in different region name based folder under group_vars for reading their respective region NTP server information:

Below is the example of NTP sync ansible playbook code in which you can see I am invoking the group variables. In this example, playbook will read the data from Group variables and sync the webservers time as per their respective region NTP servers:

Below is an example of tree structure of host and group variable files and also you can see the other YAML files as well like inventory and playbooks.

 

Wrap up!

Host variables are only good in such conditions if there are distinct or specific variables for each host or endpoint but in case if you want to use variables on more then one endpoint which are also scattered across regions or globe then using group variables is recommended. Also, since ansible use precedence rule for its variables so using group_vars or host_vars is a best practice, but in case if you have defined or provided the same variables in exta_vars or extra variables during playbook execution then Ansible will only give preference to extra variables as per precedence rule.

 

Loading

  • Author
  • Recent Posts
Charanjit Singh
Follow him
Charanjit Singh
Charanjit is currently working as a Cloud Architect at Mphasis, with 18 years of experience in IT infrastructure projects, implementation, and support. While his main role is as a DevOps engineer, he holds a Cloud Architect position and has strong skills in cloud technologies and automation. His expertise includes Terraform, AWS, Azure DevOps, Azure Cloud, VMware, and Linux systems.

Charanjit is passionate about automating tasks and improving processes. He uses tools like Terraform and Azure DevOps to build and manage cloud infrastructure and streamline deployment. He also enjoys using Shell scripts and Ansible playbooks to make systems run more efficiently.

In his free time, Charanjit enjoys learning about new technologies and sharing his knowledge through his blog. When he’s not working, he likes listening to music, having a cup of coffee, and relaxing in nature.

You can connect with Charanjit on Twitter, Facebook, LinkedIn, or email him at charanjit.cheema@cjcheema.com.
Charanjit Singh
Follow him
Latest posts by Charanjit Singh (see all)
  • How to Deploy Docker Containers with NGINX on AWS EC2 Using Ansible and GitHub Actions - April 26, 2025
  • No More DynamoDB! Use Native S3 locking for Terraform State - February 7, 2025
  • How to Bring and Manage Manually Created AWS Resources Under Terraform Management - January 31, 2025

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tags

AWS Cloud Computing Dockers Networking Open Networking OpenSource RHEL-CentOS SDN Server Hardware SLES tcpdump Ubuntu WSL

Follow me @

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers

Recent Posts

  • How to Deploy Docker Containers with NGINX on AWS EC2 Using Ansible and GitHub Actions
  • No More DynamoDB! Use Native S3 locking for Terraform State
  • How to Bring and Manage Manually Created AWS Resources Under Terraform Management
  • Iterating Cloud Resource Provisioning Using Terraform Count and For_Each Meta-Arguments
  • Terraform and Ansible Collaboration for AWS Cloud Deployment

Recent Comments

  1. Charanjit Singh on Terraform and Ansible Collaboration for AWS Cloud Deployment
  2. christinatodd2020aeaa798563 on Terraform and Ansible Collaboration for AWS Cloud Deployment
  3. Charanjit Singh on How to Set password policy in CentOS or RHEL system
  4. SAURABH on How to recover or rebuild initramfs in CentOS 7 Linux
  5. Sangita on How to Set password policy in CentOS or RHEL system

Archives

  • April 2025
  • February 2025
  • January 2025
  • August 2024
  • July 2024
  • June 2024
  • January 2024
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • September 2022
  • August 2022
  • July 2020
  • May 2020
  • February 2020
  • November 2019
  • June 2019
  • May 2019
  • March 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • June 2018
  • May 2018
  • April 2018

Categories

  • Automation
  • Cloud Computing
  • Coding
  • CyberSecurity
  • Networking
  • OpenSource
  • RHEL-CentOS
  • Server Hardware
  • SLES
  • Technical Blog
  • Ubuntu
  • WSL

Blog Stats

  • 18,353 hits
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Home
  • About Me!
  • Way to my Technical Blog
  • Contact me
  • Privacy Policy
© 2025 Welcome To Charanjit Cheema Blog | Powered by Superbs Personal Blog theme
 

Loading Comments...
 

    %d