Boyan Iliev

A Super Quick Intro to Ansible

Created May 30, 2022

Introduction

According to Wikipedia:

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows.

It is one of the most beloved and used automation systems. It is a must-know for any Backend or DevOps developer. And even if you don't spend much of your time looking for great automation systems, it is a huge benefit to know what Ansible is and how it works.

What is Ansible?

Ansible is a push configuration tool. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. You can write YAML (YAML Aint а Markup Language) codes that automate deployment.

Ansible is installed only on the local machine. This makes Ansible agentless which means it doesn't have self-healing like other tools which use an agent. Since the local machine has complete control, it pushes the playbooks onto the nodes, and thus it's called a push configuration tool. The playbooks and the inventory are written at the local machine, and it connects with the nodes through SSH Client.

Ansible also has automated reporting. In case our playbook has several tasks, and all these tasks are named so whenever we run or execute our playbook it gives a report on which tasks ran successfully, which failed, which clients were not reachable, and so on.

How to install Ansible

If you are using a Ubuntu machine, run these 4 commands to install Ansible:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

If you are using a different OS, then check out the official documentation on how to install Ansible here.

How to create and use a Host File

The host file stores the information about remote nodes information. There can actually be more than one Host File, depending on how many clients(the remote nodes) you have and what automation tasks they need to run on them.

In order to add clients, we first must make a group and add the IP address of our client. And next to the IP we add the username and the password of our client like this:

[group-name]
199.999.9.999 ansible_ssh_pass=password ansible_ssh_user=user

After that just save the file.

How to create a Playbook

In order to create a playbook we just need to create a YAML file like this:

nano file_name.yml

Remember that spacing is really important in YAML files.

YAML files always start with ---. Then after that, we need to give our playbook a name. Then after that, we add hosts, which is where this would be executed. So add the name of the clients' group name that you made in the host file. Then we add remote_user which is the user that we will be using at our client. And then we add become so that we can set our privileges at true.

It should look something like this:

---
- name: sample book
  hosts: ansible_clients
  remote_user: root
  become: true

After we set all of this up, we must add our tasks to the playbook. In the example below, we are going to say that the first task is going to install httpd(Apache) in its latest state. The next task is running httpd. And our last task is to create the content and point out its destination with dest.

---
- name: sample book
  hosts: ansible_clients
  remote_user: root
  become: true

  tasks:
   - name: install httpd
		 apt:
       name: httpd
			 state: latest
   - name: run httpd
     service:
			 name: httpd
       state: started
   - name: create content
		 copy:
			 content: "welcome"
			 dest: /var/www/html/index.html

After saving the file run this command to check if the syntax is OK:

ansible-playbook file_name.yml --syntax-check

If there aren't any errors then to execute your playbook run the following command:

ansible-playbook file_name.yml

Conclusion

This was a super quick intro to Ansible, but hopefully, it helped you see what an awesome tool Ansible is! Hopefully, it will get you to take a deeper dive into it, which will help you take your skill level to the next level!

I hope you enjoyed this post and I wish you all happy coding!