Why & What is Ansible - Ansible

Why & What is Ansible - Ansible

Earlier the administrators were managing the infrastructure manually, which took a lot of time to do and was repetitive. Let's take a real example an administrator has to manage the infrastructure by logging in to each server and doing patching, cleanup unwanted spaces from the drive, Installing new applications, managing the system-related files, system reboot, creating a new virtual machine in the cloud or in the on-premise datacentre in a manual way which consumes lots of time. So here Ansible comes into the picture which is taking care of all these tasks in an automated manner.

Ansible is a powerful automation tool which is having the ability to automate almost all manual tasks of an administrator or a DevOps engineer. Ansible is an open-source IT engine that automates application deployment, cloud provisioning, infrastructure orchestration, and other IT tools.

The above picture is showing the architecture of Ansible, in which we can learn about how Ansible works. First, we have to create an Ansible inventory file in which we add all details of VMs/machines, then we have to create an Ansible playbook in which we have to add all required tasks on which Ansible works. The default path of the inventory file is /etc/ansible/hosts inside the ansible-controller machine(here (A) is the ansible-controller machine). A Database can be integrated with ansible-controller machine so that we can get all the required details of machines that Ansible needs to target or work on those machines here all App VMs and DB VMs are target machines or hosts. We can also integrate an ITSM tool like ServiceNow so that whenever a request is generated then ServiceNow will trigger the ansible-controller machine and the Ansible will start automating the tasks which are written in the playbook.

Some of the best advantages of using Ansible:-

  1. Ansible provisions the Infrastructure within a few seconds.

  2. Ansible helps in installing the applications on top of the created infrastructure.

  3. Ansible helps in configuration management (patching, file management, etc).

  4. Ansible helps in establishing a connection between the infrastructure created.

  5. Ansible helps in managing firewalls.

  6. Ansible helps in doing application deployments.

What makes Ansible different from its alternative tools is - Ansible does not need any software installed on the target machine(host), so that Ansible can connect with it. That is why Ansible is called as "Agentless".

Remember, the ansible-controller machine is Linux only & the target machines can be Linux or Windows. Ansible playbooks are written in YAML language and the file format for Ansible inventory files is either INI or YAML. Ansible uses by default port to connect to Linux remote hosts/target machine is 22.

How to connect Ansible-Controller to Target Machines:-

There are mainly 3 steps required to perform for setting up the connection between Ansible and the target machines (Linux).

  1. Install Ansible on a machine called an ansible-controller machine.

  2. Do SSH to the target machine from the ansible-controller machine.

  3. Connecting an ansible-controller machine to a target machine.

[1]. Installing Ansible ( Linux flavor - CentOS)

$ sudo yum install ansible
$ ansible --version

[2]. SSH to the target machine from the ansible-controller machine.

$ ssh 196.123.1.114

IP1=196.123.1.114, Now you are logged into the target machine 1 and press exit.

[3]. Connecting an ansible-controller machine to a target machine.

Go to the ansible-controller machine and follow the below commands.

$ mkdir mytest-project
$ cd mytest-project/
$ cat > inventoryfile.txt
  target1 ansible_host=196.123.1.114 ansible_ssh_pass=password123 ansible_connection=ssh
$ ansible target -m ping -i inventory.txt

Now a success | target1 status will be shown which means the ansible-controller machine is connected properly with the target machine 1. target1 is the alias name for the target machine 1, ansible_ssh_pass is looking for the password for logging into target machine 1.

To connect to a Windows machine using Ansible, you need to use the WinRM (Windows Remote Management) protocol instead of SSH. WinRM allows us to remotely manage and execute commands on Windows machines. To enable Ansible to connect to Windows hosts (target machine 2), we need to configure WinRM on the target Windows machine and set up the necessary authentication methods. Once WinRM is configured, we can use Ansible's winrm connection plugin to establish a connection.

Setting up the connection between an ansible-controller machine with a Windows machine:-

[1]. Install the pywinrm Python package on the ansible-controller machine:

$ pip install pywinrm

[2]. Configure the inventory file with the Windows machine's details:

The inventory file will be same here - inventoryfile.txt

target2 ansible_host=192.168.1.100 ansible_user=administrator ansible_password=password321 ansible_connection=winrm

target2 is the alias name for the target machine 2, IP2=192.168.1.100.

[3]. Connecting an ansible-controller machine to a target machine(windows)

$ ansible target -m ping -i inventory.txt

Now, we can see two success status at the same time as in the inventory file there are two machines whose details are written inside it, so Ansible will ping both of these machines and give us the output.

Thank you so much for taking your valuable time for reading.

I have tried my level best to explain as much information as possible in the easiest manner. Any feedback for further improvement will be highly appreciated!

We can connect at Rahul Kumar Verma | LinkedIn Rahul Kumar Verma (@RahulKu28171925) / Twitter