Technology

What is Ansible, How to Use it?

What is Ansible?

Ansible Automation Platform has grown over the past years to provide powerful automation solutions that work for operators, administrators, and IT decision-makers across a variety of technology domains. It’s a leading enterprise automation solution from Red Hat®, a thriving open-source community, and the de facto standard technology of IT automation.

Installation of Ansible in Red hat Linux in offline Manner {Air-Gap Environment}

Packages For Installing Ansible

  • ansible-2.9.27-1.el7.ans.noarch.rpm
  • python2-httplib2-0.18.1-3.el7.noarch.rpm
  • python2-jmespath-0.9.4-2.el7.noarch.rpm
  • sshpass-1.06-2.el7.x86_64.rpm

Link:  https://drive.google.com/drive/folders/1EzMm1WjwOxeMUT4T0RkbJPmkphEE6mw3

After Downloading these Packages go to the Terminal

Command: yum install *.rpm

Check the Ansible Version :

Command:  ansible –version

As You can see these Will Show us the ansible Version as well as which python module is used by ansible in my case it’s python 2.7

Note: Why Ansible is Secure, Light Weight and agentless  because It uses SSH as its medium of communication 

My Lab Set-Up

Make Sure in Every Server SSH Service Start you can check that by using Command :

 systemctl status sshd

Ansible Configuration :

nano /etc/ansible/hosts

Ansible Usages SSH, So We need to configure the ssh in Ansible Controller and Client

ssh-keygen

Now We generated the ssh key and we are going to copy these keys to all the client machine

 ssh-copy-id  root@192.168.43.109

ssh-copy-id  root@192.168.43.46

ssh-copy-id  root@192.168.43.29

Output :

Now if you want to ssh into any of the three machines it doesn’t going to prompt for a password

Output:

Now Check for the Ansible Connection

To Check the Ansible Connection:

Command: ansible Client_IP_Addresses -m ping

It should Prompt pong in response

You can check the connectivity using the Group name also in my case group name was webservers

 ansible webservers   -m ping

Ansible Ad-hoc Command

Ad hoc commands are commands which can be run individually to perform quick functions. These commands need not be performed later.

Ansible Ad-hoc Command Syntax:

Examples:

To Check Memory :

ansible all -m shell -a “free -m”

To Check about uptime of the servers:

 ansible all -m shell -a uptime

Command to generate a file with specific permission on the same location to all the servers!

 ansible webservers -m file -a “path=/tmp/testfile state=touch mode=0755”

Check that the file created on all the servers or not

Note:

These ad-hoc commands are not used for configuration management and deployment, because these commands are of single-time usage only. Ansible-playbook is used for configuration management and deployment.

Next Step we will learn to create and copy files and folders to nodes

cal > cal.txt

 ansible webservers -m copy -a “src=/home/sourabh/cal.txt dest=/tmp”

Check on all the is the file exists or not

Bingo cal.txt file exists on every node in /tmp directory just as we mentioned above

Install Packages:

I will be going to install http packages on all the nodes First Check if http packages are installed on the nodes or not

Nope Service could not be found So let’s install that

 ansible webservers -m yum -a ‘name=httpd state=present’ -become -u root

Now Let’s Check http package is installed or not

Bingo on every node httpd package is installed!

Ansible

I hope this blog will be helpful for you to understand the Core Concept of Ansible and Its different methodologies which we can use in a production environment.

The goal of the Ansible series blog is to provide you, the reader, with a fundamental grasp of Ansible. A complete SMB or enterprise-level deployment, on the other hand, is significantly more complicated and needs solution architecture from the ground up, taking into consideration future scalability as well as real deployment and daily operations, as we all know. Zindagi Technologies offers services like a security operation centre, DevOps tools, and many more to aid you in implementing cloud-based applications successfully. If you have any questions, comments, or suggestions, please contact us at +919773973971.  

Author
Sourabh Dey
Associate Consultant – Server & Virtualization

Author

Team ZT

Leave a comment

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