Install and Configure Ansible on CentOS 7

  Ansible

About

Ansible is an open source automation platform.¬†Ansible help us in configuration management, application deployment, task automation and IT orchestration. Imagine have a number of machines running on same version of same operating system, and you want to install LAMP server on every machine. With ansible you don’t have to install individually on each one. You can install LAMP on every machine using a control machine on which ansible is installed. Ansible is agentless, so you don’t have to install any additional software package on client machines.

Here I will show you how to install and configure ansible on centos 7, which you can be used as your control machine.

To get Ansible for CentOS 7, first you have to install epel repository:

yum install epel-release

Install Ansible:

yum install ansible

Ansible keeps track of all of the servers that it knows about through a hosts file.

vi /etc/ansible/hosts

Add the following entries:

[group_name]
node1 ansible_ssh_host=ip_addr
node2 ansible_ssh_host=ip_addr

group_name is to identify all the servers under it together.

My hosts file is given below:

 

 

 

 

I added only one host (spotfixcrew) under the group (sfcgroup).

For managing hosts through ansible you must have ssh access to them. While connecting with ssh you should not be prompted for a password. So you have to setup a ssh key based authentication with all of the hosts.

Then you have to specify to ansible to which user on the client it should try to connect. Here I use the name ‘prince’.

Create a directory in the ansible configuration structure to specify the group variables.

mkdir /etc/ansible/group_vars

Now within this directory you can create YAML files for each group you want to configure:

vi /etc/ansible/group_vars/sfcgroup

My entry in this file is shown below, you have to enter the username through which you can ssh into the host. A YAML file always start with three hyphens (it is important).

 

 

 

Now that you have your hosts set up and enough configuration details to allow you to successfully connect to our hosts, let’s check the connection using ansible ping module.

Ping all of the servers you configured by typing:

ansible -m ping all

Ping a particular group of servers you configured by typing:

ansible -m ping sfcgroup

(My group name is sfcgroup, use your group name to ping)

Ping to one of the server you configured by typing:

ansible -m ping spotfixcrew

(‘spotfixcrew’ is my hostname.)

The output to the above three commands is given below:

 

 

 

 

 

 

 

 

 

 

 

The shell module lets us send a terminal command to the remote host and retrieve the results.

example_1:

ansible -m shell -a 'free -m' spotfixcrew

 

 

Example_2:

ansible -m shell -a 'df -h' spotfixcrew

 

 

 

 

 

 

 

 

 

 

 

 

LEAVE A COMMENT