How to configure ansible in CentOS 7

How to configure ansible in CentOS 7

In this article we will demonstrate how to configure Ansible in CentOS 7 and how to execute command to commute the remote servers.

Ansible Servers  : server.ghanshammahajan.com ( 192.168.1.111 )
Nodes            : 192.168.0.112 , 192.168.0.113, 192.168.0.114, 192.168.0.115

Installing Ansible on CentOS 7 

Now before configuring the ansible, you need to install the ansible on server. For info refer the link  How to install Ansible in CentOS 7 using Yum

Ansible Structure :

Before we proceed to create a basic configuration, I would like to take a moment to explain the Ansible file/folder structure. You will note that if you list the files/folders in /etc/ansible that you’re presented with the following. Alongside, I have included an explanation for each file or folder.

How to configure ansible in CentOS 7/RHEL 7

How to configure ansible in CentOS 7

  • /etc/ansible :- The main configuration folder which encompasses all Ansible config
  • /etc/ansible/hosts :- This file holds information for the hosts/and host groups you will configure
  • /etc/ansible/ansible.cfg :- The main configuration file for Ansible
  • /etc/ansible/roles :- This folder allows you to create folders for each server role, web/app/db, etc.

It is advisable to use a proper role structure in Ansible, as this makes it easier to manage your playbooks.

Ansible Hosts Configuring

Inventory file (/etc/ansible/hosts), This file hold the host information’s like which host we need to get connect from local to remote. Default inventory file will be under /etc/ansible/hosts.

We need to keep track of all the servers and clients from ‘hosts’ file, we also need to start communicating with the other client or server machines.

# vi /etc/ansible/hosts  — Example File

How to configure ansible in CentOS 7/RHEL 7

How to configure ansible in CentOS 7

When we open the configuration file, we will see that all the commented lines and none of the configuration in the files works.

General Syntax for adding the Host or Client in the Group adds

<hostname>

<IP Address>

[group_name]
<IP Address>
<IP Address>

We have added 5 host IP address as client in different groups. 2 IP as webservers group, 1 IP as dbservers and single IP address no group. Refer the below screenshot.

How to configure ansible in CentOS 7/RHEL 7

How to configure ansible in CentOS 7

We needed to configure so that SSH keys are copied to all the client machines so that they are authorized without any password.

In our scenario, we are using two client machines and all the client machines are accessible using the SSH keys without prompting for the password.

Executing the Simple Ansible Commands

To test our configuration we run the below command which will ping all the clients in the configuration file.

# ansible all -m ping
How to configure ansible in CentOS 7/RHEL 7

How to configure ansible in CentOS 7

We can also execute the command as per mentioned groups.

 # ansible webservers -m ping
How to configure ansible in CentOS 7/RHEL 7

How to configure ansible in CentOS 7

You can also execute command with single IP address.

 # ansible 192.168.0.115 -m ping
How to configure ansible in CentOS 7/RHEL 7

How to configure ansible in CentOS 7

For more information of the ansible hosts file configuration click the ANSIBLE DOCS

Thanks for reading the post 🙂 Please share and leave your comment if any.

Bookmark the permalink.