In the world of DevOps, Ansible is one of the leading open-source IT automation solutions. IT automation doesn’t need to require an advanced degree. With Ansible, the largest independent provider of IT automation solutions, DevOps and IT teams are three times as productive and spend three times less time on their IT automation projects. Whether it’s cloud, hybrid, physical, or virtual, Ansible delivers simple IT automation that ends repetitive tasks and frees up DevOps teams for more strategic work.
Ansible is a simple IT automation platform used by more than 100,000 organizations worldwide. It enables IT teams, to execute commands and manage applications, devices, and infrastructure reliably and repeatedly. Ansible works alongside IT teams to execute commands and automate applications, devices, and infrastructure with simple, proven playbooks. This blog will look at what Ansible is, how it works, and how this simple IT automation tool ends repetitive tasks.
If you are not using a cloud management platform, you need to make sure that you’re using a machine that can run Ansible. Ansible works with many machines and operating systems, but Windows is not supported. You can use any machine with Python 3.8 or newer, including those from Red Hat, Debian, CentOS, and macOS. (Though it’s also possible to use Ansible with Python 2.7, Python 3.8 is the system default now.)
Installation and Setup
To begin experimenting with Ansible to manage your server infrastructure, you need to install the Ansible software on the machine that will serve as your control node. You can install the latest release with your OS package manager like apt on Ubuntu or yum on RHEL etc. or just use the PIP package manager.
But first, although not necessary, we should install a virtual environment that would allow us to install Python libraries separately, not interfering with anything. To install or update your virtual environment with:
sudo pip install -U virtualenv
After the installation is done successfully, we can get Ansible inside of a virtual environment, first create a virtual environment then enable it.
Now if we install anything it will be inside of that virtual environment. Now install ansible with the following command:
pip install -U ansible
Since we are using a virtual environment in this tutorial, we won’t have some configuration files that come with Ansible but we won’t need them either. We’ll continue this way – we can create the configuration files within our local directory as needed. As a result, we won’t need any configuration files in /etc or other locations.
With Ansible, we can create an inventory file and use it to define which servers or systems it will be managing. This can be named anything, but it’s typically named ‘hosts’. Ansible can be run on both remote servers and locally, I’m using Ansible to manage the same server that I’m using Ansible on, which is a valid way to use Ansible. It’s not its main use case, but that doesn’t make it invalid.
Now let’s set the hosts file to point to a local machine under local and point remote server under remote:
And to make sure all these work, let’s run a task against the server.
# Run against localhost
ansible -i ./hosts --connection=local local -m ping
If everything worked successfully, you would get results something like this:
The output we got back from Ansible is a JSON document, which tells us if we were able to ping the device and whether or not it acted differently.
We also check our remote server with the following command:
# Run against remote server
ansible -i ./hosts remote -m ping
This is a simple setup of Ansible Inventory files; they are simple to write and understand, but you have the flexibility to create more complex configurations by defining ranges of hosts, multiple groups, variables, etc. so you should go ahead and try to customize them on your own. You can read more about it here.
Now, let’s move on to Playbooks. Playbooks are the essence of Ansible. Playbooks can run multiple Tasks and provide some more advanced functionality that we would miss out on using ad-hoc commands. This is where you create the instructions that you write to define a blueprint for your hardware infrastructure. This is what you will spend most of your time working with Ansible.
To start with we can create a simple playbook, we’ll check if an application is present in our system or not; just copy and paste the following line into a file with a .yml extension:
- name: playbook to install software #name the playbook itself
become: true #this is to give root privileges to the playbook
hosts: all #this is the user on the machine on which this playbook will work on
tasks: #this is a task schema under which we define the tasks to perform, a playbook can also handle multiple tasks
- name: ensure software is installed #define the tasks for the playbook
apt: #define the module you want to use
name: ansible #name of the application to work on
state: present #check the state of the application
update_cache: true #this is to cache the information performed by the playbook
- debug: var=output.stdout_lines
Note: In this playbook, I have defined the main configuration of this playbook in the upper half and the tasks in the lower half. Also, the connection: variable only has to be defined when running locally.
I have commented on what each line is used for in the code itself. So after seeing the code example above, we can understand that the playbook is just a set of instructions that configure the different nodes that you have, and each of those sets of instructions is written in a language called YAML.
Now, let’s check if that’s working, run the following command:
ansible-playbook -i hosts -K playbook.yml
Note: Here I am running Ansible on my system only. If you want to run it on a remote server you can just the hosts specifically.
And as you can see, it works perfectly. We get some helpful feedback in the form of the Tasks Ansible is completing and their result. Here we see all ran OK, but nothing was changed.
In this tutorial, we took a look at what Ansible is, how it works, and how this simple IT automation tool ends repetitive tasks. Ansible is a relatively new tool that makes it easier than ever to automate tasks and configuration management. There are many different methods and tools available to help manage your configurations, but Ansible stands out because it is simple to learn and use. There is also great documentation available for it to take a look at and if you are interested in it I’d highly recommend you!