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:
- Install Ansible
- Find the configuration file
- Run Ansible commands manually on the terminal
- Create an Ansible Playbook
- Add variables in the Playbook
Prerequisites
In order to achieve the most from this article, you will need to have the following
- Ensure you can
python3
on the host machine - Have a server to run the Ansible Playbook on
- You need to have SSH access to that server with no password input (Using your public & private keys)
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:
- ansible - this is the tool we are using
- 192.168.56.10 - this is the IP Address of the server we would like to run the command on
- -i hosts - this -i here, indicates that we will be using a custom hosts file and not the one in the main configuration directory (/etc/ansible/hosts). The hosts here is the file that has the IP Addresses, which we created above.
- -u vagrant - when running such a command on a server, Ansible assumes that the username of the server we are accessing to, is the same as the one we are running the command from. For example, if you can the command with the user tester, then it will try to SSH into the server with the same username tester. But with this option, we are telling Ansible that the user we are trying to connect to is called vagrant.
- -m ping - the -m refers to the module we are trying to use, the name after it is the actual name of the module, which in this case is ping.
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:
- name: the name is simply the name of the playbook
- hosts: this value depends on what is written in the hosts file. In our case, we use all to run this playbook on all our hosts
- become: this refers to whether the commands can or should run with sudo. If the value is yes then these commands can be ran with sudo
- remote_user: this value refers to which user we would like to SSH into and use to run the commands. This username is the one on the remote server
- tasks:
- this is essential part of the Ansible Playbook. It is the list of tasks that need to be run (in sequence). In our case, we have 4 tasks that will need to be ran:
- Install Nginx
- This task uses the apt module to install the latest version of Nginx
- Create index.html File
- This task uses the file module to create an index.html file inside the necesary directory to view on Nginx (/var/www/html)
- Add Web Content
- This task uses the lineinfile to append the given text to the file listed as the path, which is the index.html file we created in the previous task (Note that the line will not be appended if the last line in the given file is already the same line that has to be appended)
- Restart Nginx
- This task uses the service module to restart Nginx so that the content can be viewed on the website
- Install Nginx
- this is essential part of the Ansible Playbook. It is the list of tasks that need to be run (in sequence). In our case, we have 4 tasks that will need to be ran:
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:
- ansible-playbook: this tool is installed as part of the Ansible installation we did earlier. It is used to run Ansible Playbooks
- -i hosts: this option allows us to set the hosts file (other than the default). We are using the same file we created earlier
- ansible-playbook.yaml: this is the path and name of the Ansible Playbook that we wish to run
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!