Install Ansible Core on CentOS 7

System: CentOS 7, Ansible-2.6.0

Step 1 — Installing Ansible
To begin exploring Ansible as a means of managing our various servers, we need to install the Ansible software on at least one machine.

To get Ansible for CentOS 7, first ensure that the CentOS 7 EPEL repository is installed:
# yum install epel-release

Install Ansible
# yum install ansible

Step 2 — Configuring Ansible
Ansible keeps track of all of the servers that it knows about through a “hosts” file. We need to set up this file first before we can begin to communicate with our other computers.

# vi /etc/ansible/hosts

[devservers]
192.168.99.17
192.168.99.18

Let’s specifically tell Ansible that it should connect to servers in the “servers” group with the bachem user.
Create a directory in the Ansible configuration structure called group_vars.

# mkdir /etc/ansible/group_vars
Within this folder, we can create YAML-formatted files for each group we want to configure:

# vi /etc/ansible/group_vars/devservers
YAML files start with "---", so make sure you don’t forget that part.
Add this code to the file:

---
ansible_connection: ssh
ansible_ssh_user: bachem
ansible_ssh_pass: password

Save and close this file when you are finished.
Now Ansible will always use the bachem user for the servers group, regardless of the current user.

Step 3 — Test Using Simple Ansible Commands to Hosts

Ping all of the servers you configured by typing:
# ansible -m ping all

This is a basic test to make sure that Ansible has a connection to all of its hosts.


[root@horse /]# ansible -m ping all
192.168.99.17 | SUCCESS => {
 "changed": false,
 "ping": "pong"
}
192.168.99.18 | SUCCESS => {
 "changed": false,
 "ping": "pong"
}
[root@horse /]#

The -m ping portion of the command is an instruction to Ansible to use the “ping” module.
These are basically commands that you can run on your remote hosts.
The ping module operates in many ways like the normal ping utility in Linux, but instead it checks for Ansible connectivity.

The all portion means “all hosts.” You could just as easily specify a group:

# ansible -m ping devservers

[root@horse /]# ansible -m ping devservers
192.168.99.18 | SUCCESS => {
 "changed": false,
 "ping": "pong"
}
192.168.99.17 | SUCCESS => {
 "changed": false,
 "ping": "pong"
}
[root@horse /]#

Below command is example to check memory usage of remote hosts devservers.
# ansible -m shell -a 'free -m' devservers
As you can see, you pass arguments into a script by using the -a switch. Here’s what the output might look like:

[root@horse /]# ansible -m shell -a 'free -m' devservers
192.168.99.17 | SUCCESS | rc=0 >>
 total used free shared buffers cached
Mem: 1860 379 1480 0 33 148
-/+ buffers/cache: 197 1662
Swap: 4095 0 4095

192.168.99.18 | SUCCESS | rc=0 >>
 total used free shared buff/cache available
Mem: 1821 151 1245 9 425 1481
Swap: 2047 0 2047

[root@horse /]#

Below command is example to check memory usage of remote hosts devservers and save to /tmp/devmem.txt.
# ansible -m shell -a 'free -m' devservers > /tmp/devmem.txt

[root@horse /]# ansible -m shell -a 'free -m' devservers > /tmp/devmem.txt
[root@horse /]# cat /tmp/devmem.txt
192.168.99.17 | SUCCESS | rc=0 >>
 total used free shared buffers cached
Mem: 1860 379 1480 0 33 148
-/+ buffers/cache: 198 1662
Swap: 4095 0 4095
192.168.99.18 | SUCCESS | rc=0 >>
 total used free shared buff/cache available
Mem: 1821 151 1245 9 425 1481
Swap: 2047 0 2047
[root@horse /]#

Conclusion:
By now, you should have your Ansible server configured to communicate with the servers that you would like to control. You can verify that Ansible can communicate with each host you know how to use the ansible command to execute simple tasks remotely.

Although this is useful, we have not covered the most powerful feature of Ansible in this article: Playbooks. You have configured a great foundation for working with your servers through Ansible, so your next step is to learn how to use Playbooks to do the heavy lifting for you.

Reference:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-centos-7