Skip to main content

Ansible Basics: Getting Started with Automation

·921 words·5 mins
Ansible Automation DevOps Configuration Management YAML Playbooks IT Infrastructure Open Source Tools
Author
Emre Hayta

Introduction to Ansible
#

Ansible is an open-source automation tool that simplifies the process of IT orchestration, configuration management, and application deployment. It helps automate repetitive tasks and manage complex IT environments, making it an essential tool in the DevOps toolkit.

Why Ansible?
#

Ansible’s popularity stems from its ease of use and powerful features:

  • Agentless Architecture: Unlike other automation tools, Ansible doesn’t require any agent software to be installed on target systems. It only requires SSH access, which makes setup and management easier.
  • Simple YAML Syntax: Ansible uses a simple, human-readable language called YAML, making it accessible for anyone to learn and write automation scripts without deep programming skills.

Key Concepts of Ansible
#

Playbooks
#

Playbooks are Ansible’s configuration, deployment, and orchestration language. They are written in YAML and describe the steps to achieve a particular outcome.

Inventories
#

The inventory file lists the systems you want to manage. It can be static or dynamic, and it allows Ansible to know which hosts it needs to communicate with.

Modules
#

Ansible comes with hundreds of built-in modules for different tasks, such as managing files, installing software packages, or interacting with cloud providers.

Roles
#

Roles provide a way to organize playbooks and related tasks, variables, files, and handlers in a reusable and structured format. They make it easier to reuse code and organize your automation logic.


Project Structure Overview
#

Before diving into Ansible, it’s important to understand how to organize your Ansible project. Here is a typical project structure that you might use:

├── inventory
├── roles
│   ├── common
│   │   ├── tasks
│   │   │   └── main.yml
├── playbook.yml

Getting Started with Ansible
#

Installation
#

Make sure you have Python and pip installed, then run:

pip install ansible

After the installation is complete, you can verify it by checking the version:

ansible --version

A Simple Playbook Example
#

This playbook defines a task that installs nginx on the web host group. The become: yes directive means that the task should run with elevated privileges.

---
- name: Install Nginx on web server
  hosts: web
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

Practical Examples
#

Install a Web Server
#

---
- name: Setup Apache Web Server
  hosts: web
  become: yes
  tasks:
    - name: Update apt repository
      apt:
        update_cache: yes

    - name: Install Apache2
      apt:
        name: apache2
        state: present

Deploying Software on Multiple Hosts
#

To deploy a piece of software across multiple servers, you can define them in an inventory file and create a playbook similar to the one above. Ansible will connect to each host and execute the tasks, ensuring consistency.

Example Inventory File
#

The inventory file defines the hosts that Ansible will manage. You can list your servers and organize them into groups.

Inventory File (inventory.ini)
#

[web]
webserver1 ansible_host=192.168.1.10 ansible_user=ubuntu
webserver2 ansible_host=192.168.1.11 ansible_user=ubuntu

[database]
dbserver1 ansible_host=192.168.1.20 ansible_user=ubuntu

[all:vars]
ansible_python_interpreter=/usr/bin/python3
  • [web]: Defines a group called web containing two servers (webserver1 and webserver2).
  • [database]: Defines a group called database containing one server (dbserver1).
  • [all]: This section defines variables that apply to all hosts, such as the Python interpreter to use.

Example Role
#

Roles allow you to organize your tasks, files, handlers, and variables in a structured way, making it easy to reuse and maintain automation.

Role Directory Structure
#

Here is an example of a role to install and configure nginx:

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

Role Components
#

  1. Tasks (tasks/main.yml) - The tasks file contains the list of actions to perform:
---
- name: Install Nginx
  apt:
    name: nginx
    state: present
  notify: Restart Nginx

- name: Copy Nginx configuration file
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    mode: '0644'
  • The first task installs nginx and triggers a handler to restart it.
  • The second task copies the nginx configuration file from a template.
  1. Handlers (handlers/main.yml) - Handlers are used to trigger actions when notified by tasks:
---
- name: Restart Nginx
  service:
    name: nginx
    state: restarted
  1. Templates (templates/nginx.conf.j2) - Templates allow you to create dynamic configuration files:
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
}
  1. Variables (vars/main.yml) - Define variables specific to this role:
---
nginx_user: www-data
worker_processes: auto
  1. Defaults (defaults/main.yml) - Default variables that can be overridden:
---
worker_connections: 768
keepalive_timeout: 65

Using the Role in a Playbook
#

You can use the role in your playbook like this:

Playbook (site.yml)

  • This playbook applies the nginx role to all hosts in the web group.
---
- name: Configure web servers
  hosts: web
  become: yes
  roles:
    - nginx

Tips and Best Practices
#

  1. Project Structure: Organize your playbooks in a structured format. Organize your playbooks in a structured format. Refer to the project structure overview for guidance on how to set up your Ansible project effectively.

  2. Reusability: Use roles to ensure your playbooks are modular and reusable. This helps reduce redundancy and makes your automation scripts easier to maintain.

  3. Sensitive Data: Use Ansible Vault to encrypt sensitive information like passwords and API keys. This keeps your configuration secure.


Summary and Further Resources
#

Ansible is a powerful tool for IT automation that makes infrastructure management more efficient. With its agentless architecture and simple YAML syntax, it’s ideal for both beginners and experienced engineers. To dive deeper, check out the official Ansible Documentation and start experimenting with more complex playbooks and roles.