Introduction to Ansible

Introduction to Ansible

Introduction

What is the best way to manage a fleet of remote machines from a central point without going through too much trouble?

Contemporary software engineers use modern tools to automate tasks and provision remote systems with resources without repeating the same process for each host.

One of such tools is Ansible. It is described as a secure, agentless IT automation tool that encourages simplicity in numerous ways; it is capable of deploying software, orchestrating services, configuring systems and performing continuous deployment tasks.

Ansible does not rely on third-party agents to make connections with the remote host as compared to several alternatives. Ansible uses OpenSSH (a secure networking utility) for transport and this takes away the burden of writing extra configurations.

Benefits of Ansible

The type of server doesn’t matter

Ansible does not care what the host machine is, it could be a cloud server, a virtualization platform or a bare metal. Ansible is ready to jump right in and perform the arduous series of tasks you’ve specified.

You don’t need to know the commands you want to execute

Writing Bash commands can be fun but can also be prone to errors. When using Ansible, you don’t need to worry about writing the commands to be executed; you just make a declarative description of the state you want the machine to be in.

Easy syntax

The real power of Ansible lies in its scripting capabilities using Playbooks. A Playbook is used to perform many actions on multiple machines and they are written in YAML.

Prerequisites

  • Two bare metal servers. One of these will act as the Ansible server (where we install Ansible) and the other as the remote host which we will connect to, quickly setup one here.
  • A non-root user with sudo privileges on the remote host.

Step 1 - Install Python

If you do not already have Python installed on your remote server, you can install it with the following command:

sudo apt update
sudo apt install python

You must have Python installed because Ansible uses a Python interpreter to run its modules on the host machine.

Step 2 - Install Ansible

To use Ansible to manage several remote machines, you need to have it installed on at least one machine — the central location. We can install Ansible for Ubuntu with the following commands:

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

The command above will update the system’s package index, add a PPA (personal package archive) then install the Ansible package. If you use an operating system different from Ubuntu then you can check out the official installation page to find a fitting guide.

Step 3 - Configure Remote host

Ansible connects to remote servers using SSH, we need to make sure that the server we are running Ansible on has its public key as an authorized key on the remote server(s) you want to connect to. you need to get the public key on the Ansible server by running the following command:

cat ~/.ssh/id_rsa.pub

You can copy the output of the command and save it as you would need it soon. Next, connect to your remote server via SSH on the terminal:

ssh username@server_ip

This command will log is in as the specified username. If the specified user is not the root user, switch to the root user by running:

su -

As the root user, we can add our copied public key to the authorized_keys file by running the following command:

nano ~/.ssh/authorized_keys

This command will launch the authorized_keys file in the nano editor. Paste in the public key then save the file and close the editor — press CTRL + X, Y, then ENTER.

Step 4 - Install Nginx using Ansible

Now that we have prepared the host server to receive SSH connections from our Ansible server, you will install NGINX on the host using an Ansible Playbook. On your Ansible server’s terminal, create a new folder and navigate into it:

mkdir ansible-tutorial
cd ansible-tutorial

Create a hosts file within the newly created ansible-tutorial folder. This is an inventory file where we will list out the target hosts for our Playbook to be run against. By default, Ansible reads the hosts from /etc/ansible/hosts but you will be placing your inventory file in the working directory for simplicity:

touch hosts

Launch the host file in an editor and paste in the following code:

# hosts
 
[remote]
 
host1 ansible_host=host_server_ip ansible_user=host_server_username

In the code above:

  • [remote] is the name of a group of hosts (we only have one host here
  • host1 is an alias for the host
  • ansible_host is the address of the host
  • ansible_user is the user Ansible would log into the host as

Ideally, we would add more than one host under a group name but this is just a simple tutorial. If you are following this tutorial with more than one remote server, feel free to add more hosts.

To inform Ansible of this hosts file you just created, you can add an -I flag to the terminal whenever we run a Playbook. An alternative would be to set up a configuration file, let us go by the latter. You will create a file named ansible.cfg in the working directory and paste in the following content:

# ansible.cfg
 
[defaults]
inventory = ./hosts

Here, we are just setting the default inventory file path as the hosts file we created.

Next, you will write your Ansible Playbook, it is just a script to install Nginx on the remote host for you. Create a new file in the working directory:

touch ansible_ngnix.yml

Now paste in the following command:

---
- hosts: remote
  gather_facts: False
  tasks:
    - name: Install NGINX
      apt: pkg=nginx state=present update_cache=true
      notify:
        - start nginx
  handlers:
    - name: start nginx
      service: name=nginx state=started

Let’s break down the code above:

  • YAML files start with ---
  • We specified remote as the host for this Playbook to run on
  • Setting gather_facts to False skips the implicit gathering of facts.
  • tasks marks the section where we define all of our tasks

Let’s run the Playbook using the ansible-playbook command and pass the YML file as an argument:

ansible-playbook ansible_ngnix.yml

Playbook

Open a new tab in our browser and paste in the IP address of the remote host server that you just ran this Playbook against. This is what you will see:

image_preview-6_k6hjfv.png

We just easily installed Nginx on our remote host using Ansible.

Conclusion

In this guide, you understood what Ansible is, took a look at its benefits and saw how to install it. You’ve also written an Ansible Playbook to install the NGINX web server on a remote host in barely twelve lines of YAML code.

Learn more about Ansible from the Official Documentation.