HomeAutomation Tools

Creating an Ansible static inventory with variables

Creating an Ansible static inventory with variables
Like Tweet Pin it Share Share Email
Personal Development Category (English)728x90

The previous article talks about the Ansible introduction and Configuration on Enterprise Linux 7. Now in this article, I am going to talk about the Ansible Static Inventory creation with variables. Let’s start with the introduction.

Ansible Static Inventory

Static Inventory: Static inventory is default inventory and is defined in the /etc/ansible/ansible.cfg file. Default file can be changed inside the ansible.cfg file. If you want to use a custom file as inventory input can specify it using ” -i /path/to/file ” with Ansible command line.

Ansible works based on your inventory and it will execute the tasks in multiple systems in your infrastructure at the same time. If your infrastructure is designed with multiple variables like servers/protocols are running on a custom port number then you have to use ansible variable to configure the static inventory.

General inventory inputs:
Inventory Configuration file location – /etc/ansible/hosts
shell variable:  If you want to use shell variables in your server below is the input.
$ANSIBLE_HOSTS

normale entry: It is a normal entry if you’re having FQDN name.
www.example.com

multi-definition:  Adding multiple servers with the same pattern hostname.
db[0-9].example.com

custom ip: Host with Custom Ip Address – To listen from another NIC card.
ansible_ssh_host=

Grouping: Groups start with [] braces and collection of the servers represent that group.
[group]

specific user: To use the specific user for remote clients.
ansible_ssh_user=

specific port: If your server is configured with custom ssh port number you have to use this variable in inventory file.
ansible_ssh_port=

Also Read:  Introduction and Installation & Configuration of Ansible 2.3 on CentOS/Redhat Linux 7

Suggested Read: Quick Introduction to Static and Dynamic Inventories

Use the default one /etc/ansible/hosts or create a host file. The format for /etc/ansible/hosts is an INI format and looks like this

Let’s have a look on the static inventory file with some examples as below:

 

Have a look at my lab setup with multiple variables

Using Inventories for selecting the Target hosts:

Example Static Inventory

In the below example, Ansible will check ping status of all servers part of group “virtual-env”.

Ansible Static Inventory

In the above screenshot, you will find the host’s ping response information from “virtual-env” group and also it show’s one of the client (ansible-client1) is unable to respond to ping module. That means it may not connect to the network or it may be ICMP protocol has disabled on that host.

Conclusion: That’s all about Ansible Static Inventory. Not only is this inventory configurable, but you can also use multiple inventory files at the same time (explained in the introduction) and also pull inventory from dynamic or cloud sources, as described in Dynamic Inventory. I will discuss Dynamic Inventory configuration in the next articles.

If you like to add any other variables/information into this Ansible Static Inventory article. Please do comment on the below. So that, it may useful to others. Have a great begin with Ansible Automation.

Follow our Youtube Channel for more about Ansible Tutorials :

Check out the below Video for More about Ansible Static Inventory.

Comments (0)

Leave a Reply

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

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