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 -
- Inventory file - What are Hosts or Inventory Files?
- Tasks - How to define Roles in Ansible Playbook
- 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.
- The inventory file is found in the top-level directory of an Ansible playbook's standard project structure.
- This file can also be generated automatically by a dynamic inventory script or manually.
- You must provide the IP addresses or domain names of the remote computers that Ansible will manage in the inventory file.
- 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.
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 5 6- name: Create directory 7 file: 8 path: /var/www/html 9 state: directory 10 11- name: Install Apache 12 apt: 13 name: httpd 14 state: present
In this example, our "main.yml" file specifies three tasks.
- Installing Python using the "yum" module is the first task.
- The second job uses the "file" module to create a directory at
- 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 -
1--- 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:
---: This is a YAML directive that indicates the start of a YAML document. All Ansible playbooks are written in YAML format.
name: This is a descriptive name for the playbook. It's used to identify the playbook when you run it.
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.
remote_user: This line specifies the remote user that Ansible should use when connecting to the hosts. In this example, the user is rahulwagh.
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.
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 -
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
- Ansible Handlers Explained Real-World Use Cases & Examples?
- How to Use SSH Keys with Ansible for Secure Server Management?
- Why YAML is so important in Ansible?
- Clone Public and Private Git Repositories with Ansible
- How to Limit Ansible playbook on Hosts, Groups and multiple hosts?
- Easy Fix for 'zsh command not found ansible' Error After Installing Ansible with Pip
- Demystifying Hosts, Inventory Roles, and Tasks
- Fixing-Unable to start service apache2 Job for apache2.service failed because the control process exited with error code?
- Why Ansible is the Ultimate Tool for DevOps Teams - A Beginner's Guide?
- Ansible how to fix destination path already exists and is not an empty directory?
- Install Ansible on MacOS, Windows, Ubuntu(debian) and Fedora(rpm) - Part 1