Advance

Tutorial set up Ansible inventories

Tutorial set up Ansible inventories
1
(1)

Previously, we explained for Ansible installation. In this article, you will review the Tutorial set up Ansible inventories. Firstly, what is Ansible basically? Ansible is a modern configuration management tool that facilitates the task of setting up and maintaining remote servers, with a minimalist design intended to get users up and running quickly. Secondly, how does it work? Ansible uses an inventory file to keep track of which hosts are part of your infrastructure, and how to reach them for running commands and playbooks.

 

Prerequisites

The tutorial may be more useful if you know:

  • a non-root user with sudo privileges
  • To set up, follow our Initial server setup on Ubuntu 20.04
  • To set up Ansible, please follow our guide on How to Install and Configure Ansible on Ubuntu 20.04.
  • Two or more Ansible Hosts: two or more remote Ubuntu 20.04 servers.

 

Recommended Article: Answer To MySQL Queries Troubleshooting

Tutorial set up Ansible inventories

If you search, you may find multiple ways in which you can set up your Ansible inventory file, depending on your environment and project needs. But in this guide, we will demonstrate how to create inventory files and organize servers into groups and subgroups, how to set up host variables, and how to use patterns to control the execution of Ansible commands and playbooks per host and per group.

 

Creating a Custom Inventory File

After the installation, you could see that Ansible creates an inventory file that is typically located at /etc/ansible/hosts. This is the default location used by Ansible when a custom inventory file is not provided with the -i option, during a playbook or command execution. It would be used without problems, using per-project inventory files is a good practice to avoid mixing servers when executing commands and playbooks. Also, having per-project inventory files will also facilitate sharing your provisioning setup with collaborators, given you include the inventory file within the project’s code repository.

To start and to hold your Ansible files, access your home folder, and create a new directory.

cd ~  mkdir ansible

Then, move to that directory and open a new inventory file using nano or your text editor of choice.

cd ansible  nano inventory

A list of your nodes, with one server per line, is enough for setting up a functional inventory file.

Point: Hostnames and IP addresses are interchangeable.

~/ansible/inventory
203.0.113.111  203.0.113.112  203.0.113.113  server_hostname

you can use the ansible-inventory a command to validate and obtain information about your Ansible inventory when you have an inventory file set up.

ansible-inventory -i inventory --list
Output
{      "_meta": {          "hostvars": {}      },      "all": {          "children": [              "ungrouped"          ]      },      "ungrouped": {          "hosts": [              "203.0.113.111",              "203.0.113.112",              "203.0.113.113",              "server_hostname"          ]      }  }

It is useful for you to know that even though you haven’t set up any groups within our inventory, the output shows two distinct groups that are automatically inferred by Ansible: all and ungrouped.

As the name suggests, all is used to refer to all servers from your inventory file, no matter how they are organized. The ungrouped group is used to refer to servers that aren’t listed within a group.

 

Buy VPS Server

 

Running Commands and Playbooks with Custom Inventories

Run Ansible commands with a custom inventory file as below.

ansible all -i inventory -m ping

So, it will execute the ping module on all hosts listed in your custom inventory file.

And now, execute Ansible playbooks with a custom inventory file:

ansible-playbook -i inventory playbook.yml  

Organizing Servers Into Groups and Subgroups

At this point, you are able to organize your servers into different groups and subgroups within the inventory file. It lets you keep your hosts in order, this practice will enable you to use group variables, a feature that can greatly facilitate managing multiple staging environments with Ansible.

A host can be part of multiple groups. The following inventory file in INI format demonstrates a setup with four groups: webservers, dbservers, development, and production.

Remember that the servers are grouped by two different qualities: their purpose, and how they’re being used.

~/ansible/inventory
[webservers]  203.0.113.111  203.0.113.112    [dbservers]  203.0.113.113  server_hostname    [development]  203.0.113.111  203.0.113.113    [production]  203.0.113.112  server_hostname

Also, if you were to run the ansible-inventory command again with this inventory file, the following output will be displayed.

Output
{      "_meta": {          "hostvars": {}      },      "all": {          "children": [              "dbservers",              "development",              "production",              "ungrouped",              "webservers"          ]      },      "dbservers": {          "hosts": [              "203.0.113.113",              "server_hostname"          ]      },      "development": {          "hosts": [              "203.0.113.111",              "203.0.113.113"          ]      },      "production": {          "hosts": [              "203.0.113.112",              "server_hostname"          ]      },      "webservers": {          "hosts": [              "203.0.113.111",              "203.0.113.112"          ]      }  }

Even though, you can aggregate multiple groups as children under a “parent” group.
The “parent” is then called a metagroup. The following example demonstrates another way to organize the previous inventory using metagroups to achieve a comparable, yet more granular arrangement:

~/ansible/inventory
[web_dev]  203.0.113.111    [web_prod]  203.0.113.112    [db_dev]  203.0.113.113    [db_prod]  server_hostname    [webservers:children]  web_dev  web_prod    [dbservers:children]  db_dev  db_prod    [development:children]  web_dev  db_dev    [production:children]  web_prod  db_prod

Point: The more servers you have, the more it makes sense to break groups down or create alternative arrangements so that you can target smaller groups of servers as needed.

Setting Up Host Aliases

As you guess, we explain about using aliases in this step. you can use it to name servers in a way that facilitates referencing those servers later when running commands and playbooks.

To use an alias, include a variable named ansible_host after the alias name, containing the corresponding IP address or hostname of the server that should respond to that alias:

~/ansible/inventory
server1 ansible_host=203.0.113.111  server2 ansible_host=203.0.113.112  server3 ansible_host=203.0.113.113  server4 ansible_host=server_hostname

If you were to run the ansible-inventory command with this inventory file, the following output will be displayed.

Output
{      "_meta": {          "hostvars": {              "server1": {                  "ansible_host": "203.0.113.111"              },              "server2": {                  "ansible_host": "203.0.113.112"              },              "server3": {                  "ansible_host": "203.0.113.113"              },              "server4": {                  "ansible_host": "server_hostname"              }          }      },      "all": {          "children": [              "ungrouped"          ]      },      "ungrouped": {          "hosts": [              "server1",              "server2",              "server3",              "server4"          ]      }  }

Remember how the servers are now referenced by their aliases instead of their IP addresses or hostnames. This makes it easier for targeting individual servers when running commands and playbooks.

 

Setting Up Host Variables

Also, you will be able to use the inventory file to set up variables that will change Ansible’s default behavior when connecting and executing commands on your nodes. As you did in the previous step. In case an alias is used to refer to that server, The ansible_host variable tells Ansible where to find the remote nodes.

You can set the inventory variables per host or per group. In addition to customizing Ansible’s default settings, these variables are also accessible from your playbooks, which enables further customization for individual hosts and groups.

Have a look at the following example to define the default remote user when connecting to each of the nodes listed in this inventory file.

~/ansible/inventory
server1 ansible_host=203.0.113.111 ansible_user=noodi  server2 ansible_host=203.0.113.112 ansible_user=noodi  server3 ansible_host=203.0.113.113 ansible_user=myuser  server4 ansible_host=server_hostname ansible_user=myuser
Then, you can create a group to aggregate the hosts with similar settings, and then set up their variables at the group level:
~/ansible/inventory
[group_a]  server1 ansible_host=203.0.113.111  server2 ansible_host=203.0.113.112    [group_b]  server3 ansible_host=203.0.113.113  server4 ansible_host=server_hostname    [group_a:vars]  ansible_user=noodi    [group_b:vars]  ansible_user=myuser  

This inventory arrangement would generate the following output with ansible-inventory:

Output
{      "_meta": {          "hostvars": {              "server1": {                  "ansible_host": "203.0.113.111",                  "ansible_user": "noodi"              },              "server2": {                  "ansible_host": "203.0.113.112",                  "ansible_user": "noodi"              },              "server3": {                  "ansible_host": "203.0.113.113",                  "ansible_user": "myuser"              },              "server4": {                  "ansible_host": "server_hostname",                  "ansible_user": "myuser"              }          }      },      "all": {          "children": [              "group_a",              "group_b",              "ungrouped"          ]      },      "group_a": {          "hosts": [              "server1",              "server2"          ]      },      "group_b": {          "hosts": [              "server3",              "server4"          ]      }  }

Point: All inventory variables are listed within the _meta node in the JSON output produced by ansible-inventory.

Using Patterns to Target Execution of Commands and Playbooks

You must provide a target when executing commands and playbooks with Ansible. And patterns allow you to target specific hosts, groups, or subgroups in your inventory file. They’re very flexible, supporting regular expressions and wildcards.

Consider the following inventory file:

~/ansible/inventory
[webservers]  203.0.113.111  203.0.113.112    [dbservers]  203.0.113.113  server_hostname    [development]  203.0.113.111  203.0.113.113    [production]  203.0.113.112  server_hostname

Next, to execute a command targeting only the database server(s) that are running on production, follow this example.

consider that only server_hostname matching that criteria; however, it could be the case that you have a large group of database servers in that group. Instead of individually targeting each server, you could use the following pattern:

ansible dbservers:&production -m ping  

This pattern would target only servers that are present both in the dbservers as well as in the production groups.

Point: If you wanted to do the opposite, targeting only servers that are present in the dbservers but not in the production group, you would use the following pattern instead:

ansible dbservers:!production -m ping  

To know more about a few different examples of common patterns you can use when running commands and playbooks with Ansible, attention to the following list.

Pattern                          Result Target

all                      All Hosts from your inventory file

host1                 A single host (host1)

host1:host2      Both host1 and host2

group1              A single group (group1)

group1:group2  All servers in group1 and group2

group1:&group2  Only servers that are both in group1 and group2

group1:!group2  Servers in group1 except those also in group2

In case you need more advanced pattern options, such as using positional patterns and regex to define targets, please refer to the official Ansible documentation on patterns.

Recommended Article: How to install MongoDB 4 on Debian 10

Good job! You succeded to look into Ansible inventories. And see how to organize nodes into groups and subgroups, how to set up inventory variables. Also how to use patterns to target different groups of servers when running commands and playbooks.

Dear user, we hope you would enjoy this article of tutorial set up Ansible inventories, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

View More Posts
Tom Veitch
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

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

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day