Demystifying Hosts, Inventory Roles, and Tasks

Are you interested in finding out how to automate your IT infrastructure and manage your servers with Ansible?

You've come to the proper location if so! We'll go in-depth on the in this blog article and demonstrate how to use the following -

  1. Inventory file - What are Hosts or Inventory Files?
  2. Tasks - How to define Roles in Ansible Playbook
  3. Main playbook - How to write your ansible playbook?

Ansible is an open-source automation tool that supports server, app, and network device management. It can simultaneously run the tasks you specify on one or more servers and utilizes a simple language called YAML to do it.

1. What are Hosts or Inventory Files?

Firstly, let's look at the inventory file. The hosts and host groups that Ansible will operate with are specified in this file.

Ansible hosts file containing host entry

  1. The inventory file is found in the top-level directory of an Ansible playbook's standard project structure.
  2. This file can also be generated automatically by a dynamic inventory script or manually.
  3. You must provide the IP addresses or domain names of the remote computers that Ansible will manage in the inventory file.
  4. For each host or set of hosts, a section is made to do this. A remote server with a dedicated IP, for instance, might be added to the inventory file as follows:

2. How to define Roles in Ansible Playbook

A role in Ansible is a group of tasks connected to a certain function or goal. You may have a role for setting up your firewall or another role for deploying a web server, for instance. Within your Ansible project, roles are kept in a directory structure and may be utilized in several playbooks.

Let's examine a role's anatomy in more detail. The "main.yml" file may be found within each role directory. You provide the duties that must be carried out as part of the position in this file. Each job is specified in YAML language and may involve carrying out operations like setting up files, performing commands, or installing software.

How to define ansible roles

As an illustration, suppose you have a job named "python" that is in charge of installing Python and configuring a web server.

Your "main.yml" file may like the following:

 1- name: Install Python
 2  yum:
 3    name: python
 4    state: present
 6- name: Create directory
 7  file:
 8    path: /var/www/html
 9    state: directory
11- name: Install Apache
12  apt:
13    name: httpd
14    state: present 

In this example, our "main.yml" file specifies three tasks.

  1. Installing Python using the "yum" module is the first task.
  2. The second job uses the "file" module to create a directory at /var/www/html.
  3. The "yum" module is used to install Apache in the third task.

Ansible's role system is a potent tool that can automate your workflow and simplify your IT infrastructure. You may develop reusable components that can be used across several playbooks by specifying tasks that are tailored to a given function or purpose. Therefore, why not give roles a shot and see how they may aid in your server management.

3. How to write your ansible playbook?

Let's start by examining the code. Here is the screenshot to understand the structure of the ansible project -

Main ansible playbook

2- name: Example Ansible playbook
3  hosts: all
4  remote_user: rahulwagh
5  roles:
6    - python

This is a simple Ansible playbook that defines a single play. Let's break down each line:

  1. ---: This is a YAML directive that indicates the start of a YAML document. All Ansible playbooks are written in YAML format.

  2. name: This is a descriptive name for the playbook. It's used to identify the playbook when you run it.

  3. hosts: This line specifies which hosts the playbook should run on. In this case, all means the playbook will run on all hosts defined in the inventory file.

  4. remote_user: This line specifies the remote user that Ansible should use when connecting to the hosts. In this example, the user is rahulwagh.

  5. roles: This line specifies the roles that should be executed as part of the playbook. In this example, there is only one role, python.

The roles section is particularly important because it allows you to organize your tasks into reusable components. A role is essentially a collection of tasks and files that are related to a specific function or purpose. In this example, the python role would contain tasks for installing Python and any other dependencies required for your project.

When you run this playbook, Ansible will connect to each host in the inventory file and execute the tasks defined in the python role.

How to run your ansible playbook

Verify the Apache2 Installation after successful run of ansible playbook

SSH into the remote server and run the command systemctl status apach2 and it should show the Apache2 process running successfully -

Verify the Apache2 service after running ansible playbook

4. Conclusion

Ansible playbooks can become much more complex than this example, but the basic structure remains the same. By defining plays, hosts, and tasks, you can automate virtually any task related to IT infrastructure management. And by using roles, you can easily reuse code and organize your tasks into manageable components.

Posts in this series