Devops Methods for Automation of Server Management using Ansible
Ansible is a simple, open-source, IT automation engine that automates cloud provisioning, intra-service orchestration, configuration management, application deployment, and many other IT needs in day to day life in Software engineer.
It is designed for multi-tier deployments. It will aligning with the cloud’s basic structure precisely. Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.
Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency.
Ansible Architecture
The Ansible orchestration engine interacts with a user who is writing the Ansible playbook to execute the Ansible orchestration and interact along with the services of private or public cloud and configuration management database. The below diagram explains it.
Playbooks here actually define your workflow because whatever tasks that you write in a playbook, it gets executed in the same order that you have written them. They are written in YAML format, which describes the tasks and executes through the Ansible. Also, you can launch the tasks synchronously and asynchronously with playbooks.
The above architecture has a bunch of Host machines to which ansible server connects and pushes the playbooks through SSH. It has an Ansible automation engine using which users can directly run a playbook that gets deployed on the hosts. There are multiple components in the Ansible automation engine. The first is a host inventory. It’s a list of all the IP addresses of all the hosts. Let’s go one by one, in the Ansible automation engine.
Pre-requisites for using Ansible
There are a few basic pre-requisites you need to fulfill, before installing and configuring Ansible.
- Minimum two cloud machines (here we are using AWS instance)
- A good internet connection
Step 1: Preparing the system
Before we start off, we need to ensure that our system is updated, for that run following command:
$ sudo apt-get update && sudo apt-get upgrade -y
The command will ask for root privileges. Enter your user password and wait for your system to upgrade. After the upgrade is finished, make sure to reboot your system. It will initialize and setup your upgrades in the next reboot.
Step 2: Install Ansible Package
Now we are using EC2 instance to install the Ansible Package:
$ sudo apt-get install ansible
This above command will install the Ansible and we are using a Two EC2 instance so we have to install on the both instances and we took them as Ansible server and Remove Machine.
Step 3: Generate a Public key
Here, we are using a RSA algorithm to create the ssh key:
$ ssh-keygen -t rsa
This ssh key will be shared with all the remote servers which will be configured by ansible. Following command will generate a public and a private ssh key. The public key will be shared. After entering the command just press enter for the following options that will come so that it will get generated with default settings.
Step 4: Setting up SSH keys
After generating a ssh keys we need to copy RSA public key from Ansible sever and we should have to Paste in the Remote Machine in a Authorized_key and again we have to repeat from Remote machine to Ansible server.
Step 5: Configuring the hosts file
Now we need to ensure that IP Address of the instance should to updated in the hosts files:
$ sudo nano /etc/ansible/hosts
Step 6: Connection Test with Ansible Commands
On this both commands we test the connection between the worker machine:
$ ansible all -m ping
$ ansible 172.31.09.60 -m ping
Worker machine is successfully added in ansible server. To verify it use the following command. You will get the private ip of al the worker machines which are attached with the ansible server
Actually, when we use ansible ping, three things are checked in the remote servers added inside ansible host file.
1. Ping is successful
2. Login without password is successful
3. Python is installed
Step 7: Create a YAML file
Now once the ansible server and remote machines are successfully configured, we can write ansible playbook file to automatically configure the remote server. Ansible playbook is a .yaml file in which ansible related programming is written. Following is a sample playbook file
---- hosts: all #server host or group namebecome: yestasks:- name: install apache2apt: name=apache2 update_cache=yes state=latest
Step 8: Run the Playbook
once the playbook has been created,run the playbook by using the following command.
$ ansible-playbook apache2.yaml
Here in this playbook we actually installing a Apache2 from the ansible server to remote machine.If the command successfully runs, you will get response like following.
Now Check with the Remote Machine. If the Apache2 is installed or not!
Conclusion
Ansible was successfully implemented starting the connection test, partitioning, server IP group division, login checking. By utilizing ansible existing processes will be executed at once in sequence.