Table of Contents

Ansible is an open-source automation tool that enables system administrators to automate IT infrastructure management. It provides a simple language to describe the desired state of infrastructure, and automatically enforces that state. This reduces the time and effort needed to manage large-scale, complex systems.

If you’re new to Ansible, this article will provide an introduction to the tool, including its basic concepts and how to get started using it.

Introduction to Ansible

Ansible was developed by Michael DeHaan in 2012 and acquired by Red Hat in 2015. Since then, it has become one of the most popular automation tools in the industry.

Ansible uses a simple, declarative language called YAML (short for “YAML Ain’t Markup Language”) to define the desired state of infrastructure. This makes it easy to read and understand, even for non-programmers.

Ansible can be used to automate a wide range of tasks, including:

  • Configuration management
  • Application deployment
  • Continuous delivery
  • Provisioning
  • Security compliance
  • Orchestration

Getting Started with Ansible

To get started with Ansible, you’ll need to install it on your system. Ansible can be installed on a wide range of operating systems, including Linux, macOS, and Windows.

To install Ansible on Linux, in this case Ubuntu, you can use the following commands:

sudo apt-get update
sudo apt-get install ansible -y

Otherwise you can use the following guides to install ansible:

Once Ansible is installed, you can verify that it’s working by running the following command:

ansible --version

This should display the version of Ansible that you have installed.

Ansible Inventory

The first step in using Ansible is to define an inventory. An inventory is a list of servers that Ansible will manage. An inventory can be defined in a variety of formats, including INI, YAML, and JSON.

Here is an example of an inventory file in INI format:

[webservers]
webserver1.example.com
webserver2.example.com

[databases]
dbserver1.example.com
dbserver2.example.com

This inventory file defines two groups of servers, “webservers” and “databases”, and lists the hostnames of the servers in each group.

Ansible Playbooks

Once you have defined an inventory, the next step is to define a playbook. A playbook is a YAML file that describes a set of tasks that Ansible should perform on the servers in the inventory.

Here is an example of a simple playbook:

name: Install Nginx
hosts: webservers
become: yes

tasks:
    - name: Install Nginx package
        apt:
        name: nginx
        state: present

This playbook installs the Nginx web server on all of the servers in the “webservers” group.

The hosts parameter specifies which group of servers the playbook should be run on. The become parameter specifies that the tasks should be run with elevated privileges (using sudo or su).

The tasks section lists the individual tasks that the playbook should perform. In this case, there is only one task, which installs the Nginx package using the apt module.

Ansible Modules

Ansible modules are reusable units of code that can be used to perform specific tasks. Ansible comes with a wide range of built-in modules, and there are also many third-party modules available.

Here are some examples of built-in modules:

  • apt - Manage packages on Debian-based systems
  • yum - Manage packages on Red Hat-based systems
  • file - Manage files
  • service - Manage system services
  • user - Manage users and groups
  • copy - Copy files from the control machine to the managed servers

You can find a complete list of built-in modules in the Ansible documentation .

Ansible Roles and Folder Structure

An Ansible role is a way to organize and reuse common tasks and configurations. It is a directory structure that contains tasks, handlers, templates, files, and other resources.

Here is an example of a simple Ansible role for installing and configuring Nginx:

roles/
└── nginx/
    ├── tasks/
    │   ├── main.yml
    ├── handlers/
    │   ├── main.yml
    ├── templates/
    │   ├── nginx.conf.j2
    ├── files/
    ├── vars/
    ├── defaults/
    ├── meta/

In this example, the nginx role is a directory that contains several subdirectories, each of which serves a specific purpose:

  • tasks: contains the tasks that will be executed by the role.
  • handlers: contains the handlers that the tasks can notify.
  • templates: contains the Jinja2 templates that will be used to generate the configuration files for Nginx.
  • files: contains any static files needed by the role.
  • vars: contains variables that are specific to the role.
  • defaults: contains default variables for the role.
  • meta: contains metadata about the role, such as its dependencies.

Roles can be easily shared and reused across multiple playbooks and projects.

Here is an example of a main.yml file in the tasks directory:

---
- name: Install Nginx
  apt:
    name: nginx
    state: present
  notify: restart nginx

- name: Enable Nginx
  systemd:
    name: nginx
    enabled: yes
    state: started

This task installs Nginx using the apt module and enables and starts the Nginx service using the systemd module. It also notifies the restart nginx handler, which will restart Nginx if any changes were made to the configuration.

Using an Ansible role like this can simplify the process of managing and configuring software across multiple servers or environments. By separating the tasks, handlers, templates, and other resources into a single directory structure, you can more easily manage and reuse these components across different playbooks and projects.

Best Practices for Ansible

Here are some best practices to follow when using Ansible:

1. Use Version Control

Storing your Ansible playbooks and roles in a version control system like Git is a best practice that can help you keep track of changes and collaborate with others. Version control provides a history of changes made to your codebase, allowing you to roll back to previous versions if needed. It also makes it easy to collaborate with others by sharing code and managing conflicts.

2. Use Roles to Organize Your Playbooks

Roles are a powerful way to organize your Ansible playbooks and tasks. By grouping related tasks together into roles, you can make your playbooks more modular and reusable. Roles also make it easy to share code across different projects.

Here’s an example of a playbook that uses a role to install and configure Nginx:

name: Install and configure Nginx
hosts: webservers
become: yes
roles:
  - nginx

This playbook uses a role called “nginx” to install and configure Nginx on the “webservers” group of hosts.

3. Use Tags to Group Tasks

Tags can be used to group related tasks together in your Ansible playbooks. This makes it easier to run specific parts of a playbook, especially when working with large, complex playbooks.

Here’s an example of how to use tags in an Ansible playbook:

name: Install and configure Nginx
hosts: webservers
become: yes
tasks:
  - name: Install Nginx
    apt:
    name: nginx
    state: present
    tags: nginx_install

  - name: Configure Nginx
    template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    tags: nginx_config

This playbook has two tasks, one for installing Nginx and one for configuring Nginx. Each task has a tag associated with it, making it easy to run only the tasks that are needed.

4. Use Variables to Make Playbooks More Flexible

Variables can be used to make your Ansible playbooks more flexible and reusable. By using variables, you can make your playbooks more generic and adaptable to different environments.

Here’s an example of how to use variables in an Ansible playbook:

name: Install and configure Nginx
hosts: webservers
become: yes

vars:
nginx_port: 80
nginx_user: www-data

tasks:
  - name: Install Nginx
    apt:
    name: nginx
    state: present
  - name: Configure Nginx
    template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    notify: restart nginx

handlers:
  - name: restart nginx
    service:
    name: nginx
    state: restarted

This playbook uses variables to specify the port that Nginx should listen on and the user that should run Nginx. This makes the playbook more flexible and adaptable to different environments.

5. Test Your Playbooks

Testing your Ansible playbooks is a best practice that can help you catch errors and ensure that your playbooks are working as expected. One popular tool for testing Ansible playbooks is Molecule.

Molecule is a testing framework that allows you to test your playbooks in a consistent and automated way. Molecule can create virtual machines, apply your playbook, and then verify that everything is working as expected. This can help you catch errors and ensure that your playbooks are working correctly before deploying to production.

Here’s an example of how to use Molecule to test an Ansible role:

molecule init role myrole
cd myrole
molecule test

Conclusion

In this article, we’ve introduced Ansible, a powerful automation tool that can help you manage complex IT infrastructure. We’ve covered the basic concepts of Ansible, including inventories, playbooks, modules, and roles.

We’ve also discussed best practices for using Ansible, including using version control, organizing playbooks with roles, using tags and variables, and testing your playbooks.

If you’re new to Ansible, we recommend that you start by experimenting with some simple playbooks and gradually build up your skills and knowledge over time. With practice, you’ll be able to automate even the most complex infrastructure tasks with ease!