How would you feel if it was just the push of a button and all your systems were up to date, all the necessary packages installed and ready to go? Sounds awesome, doesn't it?

In the modern IT world, DevOps engineers make this dream come true. We now have tools like Ansible that make IT management processes more manageable and faster with just a few clicks, reducing errors and manual input by a human.

Read on to find out everything you need to know about Ansible, including a brief overview of how to use it.

What is Ansible?

Ansible is a powerful open source automation engine. It's cross-platform and allows developers to manage devices from any operating system, including Windows, macOS, and Linux.

Ansible offers a range of features: configuration management, cloud provisioning for application delivery, system updates, security automation, continuous provisioning, and much more. It's a lightweight tool and doesn't require any additional software or packages to run, making it easy to deploy.

Ansible core functions

Below are some of the features that Ansible offers as an automation tool.

App delivery

Ansible enables developers to quickly and easily deploy apps on servers. It eliminates the need to write custom code or manually run package management. Instead, instead of using Ansible playbooks, you define the tasks required to run an application and execute them remotely on the target system. You can send a single playbook to multiple devices at the same time and run the application on multiple servers.

Cloud deployment

Cloud deployment is the method by which cloud computing resources are installed, configured, and managed.

Ansible enables you to automate cloud provisioning tasks, including setting up infrastructure, setting up hosts, managing network interfaces, managing firewalls and users on the remote hosts.

Configuration management

Configuration management is another function of Ansible. With Ansible playbooks or ad hoc commands, you can get your systems up and running quickly with just a few commands. Because Ansible uses YAML as the default configuration language, it's easy to create tasks in a human-readable format and run them on remote computers.

For example, if you have a freshly provisioned server, you can use Ansible to update the system, install the required packages, and start the services on the server.

Security automation

Ansible is helpful in security automation tasks. For example, you can use some Ansible commands to set up custom firewall rules, scan for malware, clean up your system with custom tools, and much more. It also allows you to monitor the health of your system and quickly back up your system in the event of a failure.

It also uses SSH to authenticate users and run commands on remote hosts, which makes it very safe to use.

Related: Why Software Security Is A Skill All Programmers Should Have

Ansible architecture

Let's learn more about the components of Ansible Engine.

Stocks

Ansible manages remote hosts using inventories. An inventory is a simple text file that contains the IP addresses and host names of remote computers. Hosts in the inventory file can be organized into different groups such as web servers, databases, backups, etc. Organizing hosts into groups helps users run commands on a specific group of computers.

Note: You must populate the Ansible inventory file with at least one host before performing any tasks.

Playbooks

Ansible playbooks are simple text files that are written in YAML (YAML Ain’t Markup Language). They contain instructions or commands that are executed on a remote host to perform a specific task.

Since Ansible playbooks are written in YAML, they are in a human-readable format. This makes Ansible very easy to use and understand since users don't have to master any new syntax. Each playbook has one or more tasks that are performed on all or specific hosts.

Connected:

Modules

Ansible makes modules available to users. Modules are commands that help manage a remote host from the control node. These modules allow Ansible to manage users, install and uninstall packages, update systems, etc. They are executed in playbooks for specific tasks as needed. Ansible offers more than 4600 modules. Take a look at all Ansible modules.

Plugins

The Ansible architecture enables custom plugins. They help to expand or improve the basic functionality of the Ansible engine. Although it comes with multiple plugins by default, you can also create custom plugins to suit your needs.

API

Ansible extends its functionality by providing users with various API endpoints for debugging, executing commands, callbacks, and more.

Read more: What does API stand for? Examples of using APIs

Getting started with Ansible

Ansible is easy and quick to set up and use. This section shows you how to install Ansible on a host machine, configure remote hosts, and perform basic automation tasks.

Ansible is cross-platform and is therefore supported by the major operating systems. For the sake of simplicity, this guide covers the Ansible installation on a Debian-based Linux distribution. Check the Ansible installation process for other operating systems.

Start editing the /etc/apt/sources.list file

sudo vim /etc/apt/sources.list

In the file, add the following entries:

deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main

The above entry adds Ubuntu PPA to the Debian repositories so that you can install Ansible.

Next, run the following commands:

$ sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 93C4A3FD7BB9C367
$ sudo apt update
$ sudo apt install ansible -y

The first command adds the necessary keys.

The following commands update the software repositories and eventually install Ansible.

After installing Ansible on your system, proceed.

Create an inventory

The first step before running Ansible automation tasks is to set up a host inventory file.

The standard inventory file is located in / etc / ansible / hosts. Using your favorite text editor, add the IP addresses of the remote hosts that you want to automate.

$ sudo vim / etc / apt / hosts

Save the file and close it.

Create a simple playbook

Ansible playbooks allow users to automate tasks by specifying the commands to be run on the remote host.

You create a simple Ansible playbook that installs the Apache web server, creates a firewall rule to allow Apache, and restarts the service

Ansible uses YAML as the standard configuration language for its playbooks.

Create a YAML file with the touch command as:

$ touch first-playbook.yaml

Using your text editor, add the entries as shown in the following sample playbook:

$ vim first-ansible-playbook.yaml


– Host: everyone
come true
Tasks:
– Name: update apt packages
suitable:
Condition: current
update_cache: yes
– Name: Install Apache
suitable:
Name: Apache2
Condition: current
– Name: Install UFW firewall
suitable:
Name: ufw
Condition: current
– Name: Allow HTTP-UFW
ufw:
State: activated
Rule: allow
Port: "80"
proto: tcp
– Name: restart Apache
Service:
Name: Apache2
State: restarted

In the playbook above, start by specifying the hosts that you want the playbook to run on. Since there is only one host in the inventory file, set the hosts to all.

Next, specify the “will” instruction, which will allow the tasks in the playbook to be performed as root.

The preceding lines define the tasks to be performed on the remote host. They contain a name that describes the functions and the actual tasks that are to be performed on the host.

The first tells Ansible to update the repository cache; this corresponds to the command:

$ sudo apt-get update

The following instructions will install the latest version of the Apache web server.

This is followed by the installation of the UFW firewall. This requires enabling and allowing port 80, which Apache uses for the TCP protocol.

Finally, the last statement restarts the Apache web server.

Run Ansible Playbooks

To run the Ansible Playbook we need to ansible-playbook Command that comes with Ansible when it is installed.

To run the first-playbook.yaml file, use the command:

$ ansible-playbook –user = ubuntu first-playbook.yaml

In the above command, the –User pass the option that specifies the user on the remote host.

NOTE: Make sure the remote host has SSH keys installed.

Once you run the above command you will get output as shown.

The above shows that all commands on the remote host ran successfully.

Review tasks

To verify that the tasks in the playbook ran successfully, open your browser and navigate to the remote host's IP address.

With the commands executed and the Apache server running, you should see the Apache Ubuntu default page like this:

Should you be using Ansible?

In conclusion, it becomes clear that Ansible is a powerful tool. Its flexibility, functions and capabilities make it a promising tool for current and future automation tasks. If you are a DevOps engineer looking to automate your IT tasks quickly and easily, Ansible will play an important role on your way.

How to safely test desktop applications in a secure container with Docker

Docker is a popular platform for developing and testing server-based applications. But did you know that it also allows you to safely run new programs on your desktop?

Continue reading

About the author

Wachira Faust
(2 articles published)

Faust is a self-taught geek who loves Linux, open source technologies and shares his knowledge with the world. When he's not writing, he spends his time staring at a computer screen debugging code.

More
From Wachira Faust

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!

One more step …!

Please confirm your email address in the email we just sent you.