Ansible Installation & Configuration

Ansible Installation & Configuration


Overview


In this article we are going to cover -

  • Ansible server installation and configuration.
  • Ping modul.
  • Shell module.
  • Run Ad Hoc command on remote node.


Prerequisites


  • Two CentOS /any Linux OS installed machine, one for Ansible server installation and other for remote node.
  • Proper networking access between Ansible server and remote node.

In this article we are using the two machines.

  • CentOS Linux release 7.2.1511 (192.168.20.23) - where we are going to install Ansible.
  • CentOS Linux release 7.2.1511 (192.168.20.24) - where we are going to run the playbook.


Brief Overview of Ansible


Ansible is an IT automation software developed by Michael DeHaan and owned by Red Hat. Ansible facilitates the automation of common IT tasks such as configuration management, deploying and updating applications on premises or in the cloud and creating development environments.


Ansible have three major components -

  • An automation language that describes application infrastructure in Ansible Playbooks.
  • An automation engine that runs Playbooks.
  • An enterprise framework, called Ansible Tower, that controls, secures and manages automation.


Ansible Installation


On our first machine (192.168.20.23), we are installing the Ansible tool with the below command.

# yum -y install epel-release.noarch

# yum -y install ansible


SSH key generation

First we need to create the key on ansible server and then copy the key to remote machine using the below commands.

# ssh-keygen

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
11:39:fd:38:94:12:e4:21:a8:53:64:3d:17:18:be:ed root@ansible-server
The key's randomart image is:
+--[ RSA 2048]----+
| .ooo+*= . |
| .o.+o=o+ |
| o .oo+ o |
| o o .o . |
| . . S . |
| . |
| E |
| |
| |
+-----------------+


# ssh-copy-id root@192.168.20.24
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.24's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.20.24'"
and check to make sure that only the key(s) you wanted were added.


# ssh root@192.168.20.24
[root@remote-node ~]#

 
 
 
 
 
 
Now we can see that the key is added successfully and able to login into remote machine without password authentication.


Configuring Ansible Hosts


We are now configure the host file as below.

# cp /etc/ansible/hosts  /etc/ansible/hosts.orignal


# vi /etc/ansible/hosts

# [group of servers] we can add below this

[remote-node]
# If the key is stored on remote machine
192.168.20.24  ansible_ssh_user=somesh


# If the key is not stored on remote machine
192.168.20.24  ansible_ssh_pass=somesh123  ansible_ssh_user=somesh


# If we want to check the machine on particular port
192.168.20.24  http_port=8080  ansible_ssh_pass=somesh123  ansible_ssh_user=somesh

 
 
 
 


Ping module - Check the remote machine connectivity using inventory file


# ansible all -m ping
     192.168.20.24 | SUCCESS => {
             "changed": false,
             "ping": "pong"
     }


# ansible all -l remote-node -m ping -u somesh
     192.168.20.24 | SUCCESS => {
             "changed": false,
             "ping": "pong"
     }

 
 
 
 


Shell module - send a terminal command to the remote host and retrieve the results


# ansible -m shell -a 'free -m' remote-node
     192.168.20.24 | SUCCESS | rc=0 >>

    total          used         free       shared       buff/cache         available

    Mem:       3791          148        3049              73                 593               3340
    Swap:      2135              0        2135

 
 
 
 


Ansible Ad Hoc Commands


# ansible remote-node -a "/bin/echo Hello Somesh"
     192.168.20.24 | SUCCESS | rc=0 >>
     Hello Somesh

 

 
 
 
 
 

Now we can see that our Ansible machine is ready and working… 🙂 🙂


That’s all for now. In my upcoming article, we’ll cover:

  • Create the simple website which is running on apache web servers using Ansible roles

Next Article: Create website running on apache web servers using Ansible roles



Leave a Reply

Your email address will not be published. Required fields are marked *