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

Roger Perkin

Learn Network Automation

  • Network Automation
    • Ansible
    • Python
    • Terraform
    • pyATS
    • Git
    • Postman
  • ISE
  • Cisco
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • Blog
    • CCIE Blog
  • COURSES

Ansible Hosts File

What is the Ansible Host File?

Page Contents

  • What is the Ansible Host File?
  • Ansible Hosts File Default Location
  • Ansible Inventory File as YAML
  • Ansible Inventory File Groups
  • Ansible Inventory File Best Practices
  • Dynamic Ansible Inventory
  • Ansible Hosts File Comments
    • What is the Host file in Ansible?
    • Where is Ansible Hosts file?
    • What are the types of inventories in Ansible?
    • How do you create an inventory file in Ansible?
    • Other Ansible Resources

The Ansible Hosts File or Inventory file tells Ansible about the hosts that it can connect to.

For Ansible to automate a Linux Server, Network device or Cloud server it has to exist within the inventory (also known as the Ansible hosts file) and saved in either YAML or INI format.

The file can also be static or created dynamically by a script. All options will be covered in this tutorial below.

When you install Ansible the default location for inventory is /etc/ansible/hosts

Ansible Hosts File Default Location

ansible hosts file default location image

The file is named hosts by default and is one of the default files installed with Ansible – the other main one being ansible.cfg. If you cat the file you can see it does not contain any entries (they are all commented out) but what it does do is give you a template of how to start formatting your inventory file.

ansible network automation course
Go to my Ansible Course

What I suggest is to delete the default hosts file and create your own. I personally create a custom ansible inventory file within each project folder I create, I also then create a custom ansbile.cfg file within that same folder that references your custom host file.

#Custom ansible.cfg file
[defaults]
inventory=./hosts

The Ansible inventory file name can be anything you want as long as you reference it in the ansible.cfg file. I always use hosts for simplicity.

ansible inventory file example

Looking at the hosts file above you can see we have two groups of devices configured, CSR-Routers and SWITCHES. Each group contains some devices and in this instance the devices are defined by name and then the IP address is defined manually using the ansible_host= command.

The name CSR-1 or 2 or 3 is then what shows up when you run the playbook.

You can have multiple inventories and specify to use each inventory at the playbook runtime.

Simply create another inventory file i.e hosts-2 and at playbook runtime use the -i flag to tell Ansible to use that inventory e.g

ansible-playbook backup-router.yml -i hosts-2

You can also locate these inventory files anywhere on your device, in this example the inventory file is located in the same folder as the playbook.

ansible hosts file for home lab

In the example above CSR-1 is defined without the ansible_host command. This name must be resolvable in DNS or via a local hosts file on the Ansible control node.

The other entry is just an IP address, this will work, but when the playbook runs you will just see the IP address as the device being targeted.

All connections are made using SSH to the IP address defined in the file. Other variables such as passwords or specific ports or specific connection parameters can also be defined in this file.

Ansible Inventory File as YAML

The hosts file above is written in the default INI format, an Ansible Inventory can also be written in YAML

ansible inventory file as yaml
Ansible Inventory file Example YAML

In the above example we have the same inventory file but in a YAML format.

Ansible Inventory File Groups

To make managing your hosts easier you can put them into groups within your inventory file. e.g

[CSR-Routers]
CSR-1
CSR-2
CSR-3

[Switches]
SW1-3560
SW2-3850

[LAB:children]
CSR-Routers
Switches

In the above example you can see I have grouped the devies into two groups denoted with square brackets. I can now address our Ansible Playbooks to the group CSR-Routers and it will will only affect the devices in that group.

If I want to address all the devices in the inventory file, we make use of the children function, as you can see above I have created a group called LAB and it has children which are the two other groups.

Now if I address a Playbook to run against LAB it will affect all devices in this inventory.

Ansible Inventory File Best Practices

  1. Keep your hosts file names simple – I always use hosts
  2. Comment were required to ensure someone else can understand your inventory
  3. Use children to address multiple groups at once
  4. Do no put passwords in your inventory file – reference them in group_vars or host_vars or even better use Ansible vault within those files.

Dynamic Ansible Inventory

For small environments, a static inventory file is sufficient as there are typically not rapid changes to the devices in use.

If you are working within a cloud environment or one where multiple hosts are being spun up and tore down regularly your Ansible Inventory file will become very difficult to manage. Most cloud providers will provide you a script you can use within Ansible to regularly poll your devices and import the inventory, this is referred to as a dynamic inventory.

Ansible Hosts File Comments

If you want to put a comment next to any item in your Ansible Hosts file just start a new line with a #

For more information you can reference the Ansible Best Practice guide on the Ansible Documentation site here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html

Ansible is the most popular tool for Network Automation right now, mainly because of it simplicity to get started. You can be up and running in a few hours.

If you want a hand to get started I have a course which takes you from installation to running your first production ready playbooks.

ansible network automation course

What is the Host file in Ansible?

The Ansible hosts file is a file which lists all the hosts that you want to connect to. Hosts can grouped into groups and also put into a super-set with the the :children operator.

Where is Ansible Hosts file?

By default the Ansible Hosts file is located in /etc/ansible/hosts but you can create a custom hosts file and put it wherever you want and then specify it’s location using a custom ansible.cfg

What are the types of inventories in Ansible?

There are two types of inventory you can use within Ansible. Also there are two file types.
The Ansible Hosts file can be static or dynamic and can be in an ini format or YAML.

How do you create an inventory file in Ansible?

You can create an inventory file in Ansible anywhere you like. It is just a text file which by default is called hosts and located in /etc/ansible/ but you can create the file wherever you like and just tell Ansible where it is when you run the playbook either with the -i switch or by defining it in the ansible.cfg file.

Other Ansible Resources

I hope you found this short tutorial informative and if you want to keep learning check out these other Ansible posts

  • Ansible Backup Cisco Tutorial – this posts covers a step by step guide to installing Ansible and creating your first playbook to backup a Cisco router config.
  • What is Ansible used for? – this post covers the very basics of Ansible, what it is and what it can do?
  • Open Source Network Automation Tools – My Top 7 List – This post discusses some of the most popular network automation tools in use today.
  • Ansible Vault Tutorial – this post covers how to encrypt your passwords and sensitive information using Ansible Vault
  • Ansible Course – this is my flagship course covering all aspects of using Ansible for Network Automation
  • Ansible vs Python – Which one should you learn?
  • Cisco Switch IOS Upgrade using Ansible
  • How to install Ansible Tower
  • What are the advantages of using Ansible Roles
  • Ansible IOS Command Example
  • Ansible Tutorial for Network Engineers
Category: Ansible for Network Engineers
Previous Post: « NetYCE – Multi Vendor Network Automation
Next Post: Cisco NSO Installation Guide – Ubuntu cisco nso logo »

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

Social

Follow along on social media

Contact

Get in touch with me here

[email protected]

Navigation

Home

Blog

About

Contact

YouTube

Don’t forget to take a look at my YouTube Channel

youtube button

Copyright © 2021 · Roger Perkin · All Rights Reserved · Powered by Mai Theme