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

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:

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.

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

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:

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

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.


Related posts: