• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

Roger Perkin

Network Automation Architect

  • Network Automation
    • Network Automation Courses
    • What is NetDevOps?
    • Workflow Orchestration
    • Ansible Automation Platform
    • Ansible Workshop
    • What is Network Automation?
    • Network Automation Tools
    • ContainerLab
    • Ansible Training
      • What is Ansible?
      • Ansible Tutorial for Beginners
      • Ansible Network Automation
      • Ansible Inventory Example
    • Python Network Automation
      • Nornir
      • Python Network Automation Course
      • Python for Network Engineers
      • Python VENV / Virtual Environment Tutorial
      • Python Tutorial for Beginners
      • pyATS
    • Network Source of Truth
      • NetBox
      • Infrahub
    • NetDevops
    • DevOps Tutorial
      • Git Training
      • Terraform Training
      • Linux Training
      • Kubernetes Training
      • Devops Training Course
      • Azure Devops Training
    • Terraform
    • GIT
      • Git Commands
      • What is GitHub?
    • Docker Training
    • Confluence
    • Microsoft Azure
  • Cisco
    • ISE
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • CCIE
  • Blog
  • About
    • My Red Special Guitar
  • Contact

Ansible Host_Vars vs Group_Vars

Home » Network Automation » Ansible

What are host_vars and group_vars in Ansible?

Variables or VARS are used to input data or a variable into a place in a playbook instead of manually entering the value in multiple places.

The main difference between Ansible host_vars and group_vars is that hosts_vars represent a single host in the Ansible inventory and group_vars represent a group of hosts defined under groups.

Host_Vars and Group_Vars (group variables and host variables) allow you to associate variables to specific devices or groups of devices.

This video should explain it pretty well.

host_vars is a folder that you create and within the folder are YAML files which reference each specific device

group_vars is also a folder you create and within the folder are YAML files which reference groups of devices or all devices.

Both the Ansible host_vars directory and the group_vars directory have to be created manually and are not created by default.

The names of the YAML files in group_vars must match the group defined in the inventory and also the YAML files in host_vars must be named exactly as the hosts in the inventory.

You can also input variables or vars directly into an Ansible playbook.

You create a place holder and then reference the variable name which is defined elsewhere. The benefit is you can then change the value of the variable and it will update in all the places where the variable is defined in the playbook.

ansible course for network engineers
Check out my other Courses

Ansible group_vars vs host_vars precedence

When using variables in Ansible there is a precedence where Ansible looks for the data

The full list is below

  1. command line values (for example, -u my_user, these are not variables)
  2. role defaults (defined in role/defaults/main.yml) 
  3. inventory file or script group vars 
  4. inventory group_vars/all 
  5. playbook group_vars/all 
  6. inventory group_vars/* 
  7. playbook group_vars/* 
  8. inventory file or script host vars 
  9. inventory host_vars/* 
  10. playbook host_vars/* 
  11. host facts / cached set_facts 
  12. play vars
  13. play vars_prompt
  14. play vars_files
  15. role vars (defined in role/vars/main.yml)
  16. block vars (only for tasks in block)
  17. task vars (only for the task)
  18. include_vars
  19. set_facts / registered vars
  20. role (and include_role) params
  21. include params
  22. extra vars (for example, -e "user=my_user")(always win precedence)

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

How to use host_vars in Ansible

Ansible group_vars example

Ansible host_vars example

How do you define host vars in Ansible?

Host Vars in Ansible are defined in a YAML file with the same filename as the host the variables are referencing, the file is saved into a folder which must be called host_vars

Example below shows a device called N9Kv1 and it’s associated host vars file called N9Kv1.yml and stored in the host_vars folder

ansible host vars example

Ansible inventory vars

Inventory Aliases

You can define an alias in the inventory using host variables

jumphost ansible_port=1234 ansible_host=192.1.1.50

Frequently asked questions

How do I make an Ansible host group?

Within your Ansible inventory you create a host group by defining the name of the group within square brackets and then, note: hyphens are deprecated in group names. Then underneath the group name you list all the hosts e.g.
[SERVERS]
Server1
Server2
Server3

What are host vars in Ansible

Host vars in Ansible are variables that are specific to a host only and are defined in a YAML file which must be named exactly as the host in the inventory file.

Table of Contents

  • What are host_vars and group_vars in Ansible?
  • Ansible group_vars vs host_vars precedence
  • How to use host_vars in Ansible
  • Ansible group_vars example
  • Ansible host_vars example
    • How do you define host vars in Ansible?
  • Ansible inventory vars
    • Inventory Aliases
    • Frequently asked questions
    • How do I make an Ansible host group?
    • What are host vars in Ansible
Category: Ansible Network Automation
ansible course for network engineers
Get Access to my Ansible Course NOW
Previous Post:cisco python course imageCisco Python Course Review
Next Post:Secure data transfer in automation processes

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Sidebar

Hi I'm Roger Perkin,
Based in the UK working as a Network Automation Architect, CCIE #50038
About Roger | Twitter | Linkedin

python course for network engineers

More Ansible

  • What is Ansible?
  • AWX
  • Ansible Template Module
  • Ansible Vault Tutorial
  • Ansible vs Python Scripts
  • Advantages of Ansible Roles
  • Ansible Hosts File Tutorial
  • How to install Ansible Tower
  • Ansible Training
  • Ansible Cisco IOS Command
  • Use Ansible to Backup Cisco Config
  • Where are Ansible Modules Stored?
  • AnsibleFest London Review

Topics

Network Automation
Ansible
Python for Network Automation
CCIE
Cisco ISE
F5 Certification
BGP
OSPF
Network Automation Conferences
auvik promo banner
Pluralsight Trial

Git for Network Engineers

Ansible vs Nornir

Start learning today with my Network Automation Courses

Master Ansible, Python, Git, Nornir, Jenkins and more..


Buy me a coffeeBuy me a coffee

ansible network automation course

Have you seen my YouTube Channel?

YouTube Subscribe

Let’s get started

Take a look at my premium courses on Ansible, Nornir & Git or buy them all with the Network Automation Bundle!

Network Automation Courses

Navigation

Python VENV Tutorial
Python for Network Engineers

Network Automation
Network Automation Courses
Network Discovery Tools
Network Automation Conferences
Ansible Training
What is Ansible?
Devops Tutorial
Network Source of Truth
DevOps Glossary
Network Monitoring Software

Contact

Contact

Get in touch with me here

[email protected]

  • Twitter
  • LinkedIn
  • YouTube
Buy me a coffeeBuy me a coffee

Copyright © 2025 · Roger Perkin · All Rights Reserved · Privacy Policy – Terms