Quickstart: How to install and configure Ansible on Linux for automation

Why you need to install and configure Ansible on Linux.

Through the use of configuration management systems, we can control a large number of servers, which make life easier for system administrators. In a nutshell, we can control or manage a huge number of servers or systems from one central server using automation tools.

There are many such tools available for automation such as Chef or Puppet. However, these tools are difficult to configure. We have a great alternative for these options, Ansible. With it, you can very easily install and configure Ansible on Linux.

This guide will quickly walk you through how to install and configure Ansible on Linux.

How much Ansible works!

Ansible does not use any tools to perform automation tasks, which means that there are no background processes running on clients. Instead, Ansible makes a connection using SSH to perform its operations.

Installation details for installing Ansible.

Details for Ansible management server:

OS: Red Hat Enterprise Linux Server release 7.2 (Maipo)
Hostname: destroyer
IP: 168.123.15.60
User: lradmin

Customer Machine Details:

client 1
 
Hostname: web1
IP: 168.123.2.10
 
Client 2
 
Hostname: web2
IP: 168.123.12.12

Step 1: Install Ansible on the management server.

For Linux Mint, Ubuntu and Debian

#apt-add-repository ppa:Ansible/Ansible
#apt-get update && sudo apt-get install Ansible

For RHEL, CentOS and Fedora

#yum install Ansible

Please note that there is no official Ansible repository for RedHat, we can still install Ansible by adding the Epel repository under RHEL or Centos.

After installing Ansible, you can check the version using the following command.

[[email protected] ~]# Ansible --version
Ansible 2.3.1.0
  config file = /etc/Ansible/Ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Oct 11 2015, 17:47:16) [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]
[[email protected] ~]#

Step 2: Configure SSH to configure Ansible.

To deploy or manage on a client machine from our “destroyer” management server, we need to set an SSH password between them. In this demo, we configured the lradmin user for an SSH password.

Generate SSH keys on the management server for the lradmin user.

[[email protected] root]# id
uid=0(root) gid=1001(lradmin) groups=1001(lradmin) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[[email protected] root]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
5e:a3:5b:4c:dc:15:7d:96:59:a8:85:97:43:03:70:a2 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|          o.o+++=|
|         . o. *=+|
|        E    +.o.|
|         . ...   |
|        S = .    |
|       . = .     |
|        o o      |
|         o       |
|        .        |
+-----------------+

Here we have done key generation, in the next step we need to copy the contents of “/root/.ssh/id_rsa.pub” to the client system for the lradmin user. Please follow the instructions in this post so that the password can communicate between the management server and the web1 and web2 client machine.

After you are done with the configuration password, you can double-check by doing SSH like below.

[[email protected] ~]# id
uid=0(root) gid=1001(lradmin) groups=1001(lradmin) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[[email protected] ~]# ssh [email protected]
Last login: Mon Jun 19 13.25:34 2017 from ip-172-31-15-60.ap-south-1.compute.internal
[[email protected] ~]# hostname
web2
[[email protected] ~]# logout
Connection to 168.123.12.12 closed.
[[email protected] ~]# ssh [email protected]
Last login: Mon Jun 19 13:49:23 2017 from ip-168-123-15-60.ap-south-1.compute.internal
[[email protected] ~]# hostname
web1
[[email protected] ~]# logout
Connection to 168.123.2.10 closed.
[[email protected] ~]#

Step 3: Create an inventory file for Ansible.

In the next step, we need to create an inventory file for the customer list. Basically its a list of client IP or hostname for machines that are used for automation.

Please add the below lines of code to the “/ etc / Ansible / hosts” file on our management server.

[web]
168.123.2.10 Ansible_user=lradmin
168.123.12.12 Ansible_user=lradmin

The above lines of code describe how the list of client machines falls under the web user category having Ansible as lradmin.

Step 4: Check the connection.

Now it’s time to check if our configuration is set correctly or not. For this, we will be using Ansible ping.

#Ansible -m ping web

In the above command, we used a ping for our web server.

The system should respond with the message below if everything is Ok.

[[email protected] ~]# Ansible -m ping web
168.123.12.12 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
168.123.2.10 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[[email protected] ~]#

Step 4: Executing remote commands on client machines.

We now have the ability to run arbitrary commands on client machines to extract some information about them. So, to do this, you need to use the “command” module within Ansiblensible.

  • In case you want to use the file system on the client machine.
[[email protected] ~]# ansible -m command -a "df -h" web
168.123.12.12 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda2       10G  876M  8.5G  10% /
devtmpfs        418M     0  418M   0% /dev
tmpfs           426M     0  426M   0% /dev/shm
tmpfs           436M   13M  434M   3% /run
tmpfs           446M     0  446M   0% /sys/fs/cgroup
tmpfs           130M     0  130M   0% /run/user/1000
tmpfs           160M     0  160M   0% /run/user/0
 
168.123.2.10 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda2       10G  1.7G  7.7G  16% /
devtmpfs        418M     0  418M   0% /dev
tmpfs           446M     0  446M   0% /dev/shm
tmpfs           426M   13M  424M   3% /run
tmpfs           466M     0  466M   0% /sys/fs/cgroup
tmpfs           140M     0  140M   0% /run/user/1000
tmpfs           180M     0  180M   0% /run/user/0
 
[[email protected] ~]#

  • Checking the smooth operation of client machines.

    [[email protected] ~]# ansible -m command -a "uptime" web
    168.123.12.12 | SUCCESS | rc=0 >>
    14:35:23 up  13:49,  2 users,  load average: 0.00, 0.01, 0.05
     
    168.123.2.10 | SUCCESS | rc=0 >>
    14:35:23 up  13:49,  2 users,  load average: 0.00, 0.01, 0.05
     
    [[email protected] ~]#

    Conclusion:

    The need for automation in the IT industry is increasing every day to reduce costs as well as the time to complete various tasks. There are tools and applications available in the market. Ansilble is easy to install and configure. In addition, its agent starts any background process on clients.

Sidebar