Introduction:
The open-source automation tool Ansible transforms task automation, application deployment, and configuration management. System administrators, developers, and IT operations teams love it for its declarative style and ease of use. This tutorial covers Ansible’s architecture, overview, installation procedure, and configuring SSH agents for smooth remote machine management connections.
Ansible Overview:
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It allows you to manage and orchestrate infrastructure, software, and services in a simple, declarative manner, making it popular among system administrators, developers, and IT operations teams.
Ansible
uses playbook to describe automation jobs, and playbook uses very simple language i.e. YAML (It’s a human-readable data serialization language & is commonly used for configuration files, but could be used in many applications where data is being stored)which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed.
What is Configuration and Configuration Management?
Configuration: In the context of IT and system administration, Every minute detail of your system/machine is called configuration.
Configuration Management: It is the process of maintaining systems, such as computer hardware and software, in a desired state. Configuration Management (CM) is also a method of ensuring that systems perform in a manner consistent with expectations over time.
Ansible Architecture
Important Points:
- It is a lightweight push-based configuration management tool.
- Ansible Tower is a GUI-based system that is owned by RED-HAT.
- Ansible is written in Python language.
- It is easy to deploy because it does not use any agents it is agentless architecture, and it uses ssh only.
- It uses YAML (yet another markup language) that is based on the Key-Value pair.
- Ansible is primarily designed to manage Linux-based systems. As a result, managing Windows hosts with Ansible requires some additional setup and considerations.
- Ansible does not add any database. It does not require any daemons to start or keep it running.
- While managing remote machines, Ansible does not leave any software installed or running on them.
- Ansible was developed by Michael DeHaan, and was initially released in February 2012, RED-HAT acquired Ansible in 2015.
Installation Process:
Mainly, there are two types of machines when we talk about deployment:
- Control machine: This machine from where we can manage other machines.
- Remote machine: Machines that are handled/controlled by a control machine.
There can be multiple remote machines that are handled by one control machine. So, for managing remote machines we have to install Ansible on the control machine.
Control Machine Requirements
- Ansible can be run from any machine with Python 2 (versions 2.6 or 2.7) or Python 3 (versions 3.5 and higher) installed.
- Ansible can be installed on control machines that have the above-mentioned requirements in different ways. You can install the latest release through Apt, yum, pkg, pip, OpenCSW, Pacman, etc
Note: Windows does not support a control machine.
Here I am using Ubuntu Machine and Installation through Apt
For installing Ansible you have to configure PPA on your machine. For this, you have to run the following line of code:
$ sudo apt-get update -y $ sudo apt-get install software-properties-common -y $ sudo apt-add-repository ppa:ansible/ansible -y $ sudo apt-get update -y $ sudo apt-get install ansible -y $ sudo ansible --version<
After running the above line of code, you are ready to manage remote machines through Ansible. Just run Ansible–version to check the version and just to check whether Ansible was installed properly or not.
Setting up the SSH agent for connection allows you to use Ansible commands
For example, I am taking two more instances on AWS, apart from the control node.
NOTE: These instances should be in the same VPC and in the same availability zone as ansible-slave/host/remote machine, to manage these hosts using Ansible.
NOTE: A trust Relationship is built with the same user, so use the same name user on the control node and the host node.
ROOT with ROOT
USER with USER
ANSIBLE_user with ANSIBLE_user
Execute these commands on all the ansible-slaves/hosts
- Create a user on the slave node, I am taking my username as “ansible“
sudo adduser newusername
- Give the password
sudo passwd newusername
- Add the user in the sudoers file
sudo visudo
- Enable Password Authentication
sudo vi /etc/ssh/sshd_config
- Restart sshd service
sudo service ssh reload
- Ensure the security group that allows Ansible control nodes for all the hosts.
Come to the Ansible control node/Ansible Master to execute these commands
- Create a user on the control node, I am taking my username as “ansible“
sudo adduser newusername
- Give the password
sudo passwd newusername
- Add the user in the sudoers file
sudo visudo
- Enable Password Authentication
- Restart sshd service
sudo service ssh reload
- Add host/slave Private Ips in the Inventory file
An inventory file in Ansible is a text file that contains information about the remote hosts you want to manage. By default, this file is located at “/etc/ansible/hosts”, but you can specify a different location using the “-i” option when running Ansible commands.
Here’s an example of a simple inventory file:
vi /etc/ansible/hosts
Note: I am creating a group “cloudzenix“ to add my host Ips, you can add directly the Ips it will be considered as the default group.
- Switch to ansible user with the password.
-
su ansible
- Create an ssh key for this user
ssh-keygen
- Go to this path to see the keys
cd /home/ansible/.ssh/
- Copy the public key on all Ansible hosts by using the below commands
#ssh-copy-id ansible@192.168.0.177 #ssh-copy-id ansible@192.168.0.54 #ssh-copy-id ansible@127.0.0.1
- Now you are ready to configure your host/remote machines using Ansible, to verify use the below command
ansible all -m ping
Conclusion:
Installing Ansible provides you with a powerful automation tool to streamline your infrastructure and application management. By following the installation methods outlined in this guide, you can quickly get Ansible up and running on your preferred platform. With Ansible’s capabilities at your fingertips, you’ll be able to optimize your workflows, enhance productivity, and take control of your IT environment. Happy automating!