Hazem Allbabidi

May 11, 2023 | 7 min read


How to Use Ansible & Ansible Playbooks

Ansible is a software tool that allows you to set up a server in an automated fashion. You can install the necessary packages and tools you need, change files, and so much more, while being fully automated.

In this article, I will show you how to:

Prerequisites

In order to achieve the most from this article, you will need to have the following

Installing Ansible

To install Ansible on Ubuntu, you can run the commands below:

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

You may check out the Ansible documentation on other ways to install it.

Configuring Ansible

There are two main files that you might need to configure or change in order to have the best experience with Ansible. The first file is:

/etc/ansible/ansible.cfg

This file includes configurations of Ansible, which include things like the usernames of the users in the remote server, the log files path, and much more. Note: for the sake of this article, we will not change anything in this file since we don’t really have to

The second file is the

/etc/ansible/hosts

This file includes all the hosts of the systems that we wish to access using Ansible to run the commands/playbook on. You can have IP Address in here, domain names, and a more dynamic way of adding some domains.

_Note: we will need to have a hosts file to add the IP Address of the server we will run the Ansible commands on, but we will use a different method, which will be explained later in the tutorial.

Running Ansible Commands

Before we start, create a directory for all the files we will be creating in this tutorial. I called mine “testing_ansible”.

In order to run an Ansible command on a server, we need to first understand what are Ansible Modules, and how to create a local “hosts” file that has all the IP Addresses and Domain names of the servers that you will run the

What are Ansible Modules?

In Ansible, we have what is referred to as modules. These modules are basically like tools in Ansible that can do certain things. For example, there is a module called file that allows you to create files and directories under a specific user, along with their permissions. There is another module apt that allows you to install and update Ubuntu apt packages.

How to create a local Hosts file?

First, we simple need to create a file in our directory with the name of hosts

touch hosts

You can name it anything else if you wish.

Next, we need to add in it the IP Addresses of all the servers we would like to run the commands on. For example, my file simply looks like this

192.168.56.10

This is the IP Address of the server I will be configuring using Ansible.

Pinging the server

We will run the Ansible command using the Ping module, since it the simplest to deal with. The command looks like this:

ansible 192.168.56.10 -i hosts -u vagrant -m ping

Explanation of the command:

The above is a simple example of how to use Ansible in the terminal.

Creating An Ansible Playbook

This section goes through the actual use case of Ansible, and how we can use it to fully set up a server. We will need to create a new YAML file, and we can name it whatever we want. I will name the file ansible-playbook.yml and will add the following content to the file:

---
- name: Testing Ansible Playbooks
  hosts: all
  become: yes
  remote_user: vagrant
  tasks:
  - name: Install nginx
    apt:
      name: nginx
      state: latest
  - name: Create index.html File
    file:
      name: /var/www/html/index.html
      state: touch
  - name: Add Web Content
    lineinfile:
      line: "Hello from Ansible!"
      path: /var/www/html/index.html
  - name: Restart Nginx
    service:
      name: nginx
      state: restarted

The above code is divided in multiple sections and are explained below:

Running The Ansible Playbook

In order to run the playbook on the remote server, we need to run the following command:

ansible-playbook -i hosts ansible-playbook.yaml

Explanation:

What Should Happen?

After doing the above, including running the command, you should see the below:

hazem@hazem-PC ~/D/test_ansible> ansible-playbook -i hosts ansible-playbook.yaml

PLAY [Testing Ansible Playbooks] **************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.56.10]

TASK [Install nginx] **************************************************************************************************************************************************************************
changed: [192.168.56.10]

TASK [Create index.html File] *****************************************************************************************************************************************************************
changed: [192.168.56.10]

TASK [Add Web Content] ************************************************************************************************************************************************************************
changed: [192.168.56.10]

TASK [Restart Nginx] ****************************************************************************************************************************************************************************
changed: [192.168.56.10]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.56.10              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

You should see the tasks being done sequentially and the results being shown. Notice how we have two different statuses here: ok and changed. The first one shows that everything worked fine during that step, the second one shows that there have been changes that occurred on the server. For example, in the last task, we restarted the Nginx service, that is why we see that it has changed.

Conclusion

We now know how Ansible and Ansible Playbooks work and how we can use them to our advantage. We are able to automate many tasks to be done on a server, without us having to run each command manually.

I hope this tutorial benefitted you and hope you learned how to use Ansible and Ansible Playbooks to automate tasks.

Thank you reading and have a great day!


Previous

Monthly Tracker iOS App - Privacy Policy

Next

Building a CLI Tool Using Go and Cobra-CLI
Sign Up To Binance To Get 10% Off Commission Fees Sign Up To Kucoin