LogoOwlDocs

Advanced Concepts in Ansible

Exploring variables, roles, and templates to enhance Ansible automation.

Advanced Concepts in Ansible

In this section we will explore key advanced concepts in Ansible: variables, roles, and templates. It is essential to understand these concepts for better organization of your tasks.

Variables

Variables in Ansible are used to store data that can be reused throughout playbooks, making them dynamic and flexible. They allow you to customize configurations and manage different environments easily.

Types of Variables

  • Playbook Variables : Defined directly in playbooks, these variables are accessible within the playbook's scope.
  • Inventory Variables : Variables defined in the inventory file, associated with specific hosts or groups.
  • Facts : Automatically gathered variables about the managed hosts, such as OS type, IP address, and more.
  • Extra Variables : Variables that can be passed at runtime using the -e flag, allowing for on-the-fly customization.
- hosts: all
  vars:
    my_variable: "Hello, Ansible!"
  tasks:
    - name: Print variable
      debug:
        msg: "{{ my_variable }}"

To define and use a variable in a playbook, you can refer to the examples above for each type of variable.

Roles

Roles in Ansible are a way to organize playbooks and other files into reusable components. They are used for modularity and reusability, making it easier to manage complex automation tasks.

Structure of a Role

A typical role has the following directory structure:

my_role/
├── tasks/
   └── main.yml
├── handlers/
   └── main.yml
├── templates/
   └── my_template.j2
├── files/
   ├── my_file.txt
   └── foo.sh
├── vars/
   └── main.yml
└── defaults/
    └── main.yml

Creating a Role

To create a role, you can use the ansible-galaxy command :

ansible-galaxy role init my_role

Description of Role Files

Tasks

This file contains the main list of tasks that the role will execute. Each task is defined in YAML format and can include modules, loops, and conditionals.

# tasks/main.yml
- name: Install Apache
  apt:
    name: apache2
    state: present

- name: Start Apache service
  service:
    name: apache2
    state: started

Handlers

Handlers are special tasks that run only when notified by other tasks. This file defines those handlers.

# handlers/main.yml
- name: Restart Apache
  service:
    name: apache2
    state: restarted

To call a handler, you can do it like this:

# tasks/main.yml
- name: Install Apache
  apt:
    name: apache2
    state: present
  notify: Restart Apache  # This will call the handler to restart Apache if the task changes

Templates

This directory contains Jinja2 template files that can be used to generate configuration files dynamically. Templates allow you to use variables and control structures.

# templates/my_template.j2
server_name: {{ inventory_hostname }}
document_root: /var/www/{{ inventory_hostname }}

Files

This directory contains static files that can be copied to the managed hosts. These files are not processed as templates.

# files/my_file.txt
This is a static file that will be copied to the target host.

Vars

This file is used to define variables that are specific to the role. These variables can be used in tasks and templates.

# vars/main.yml
my_variable: "This is a variable specific to my_role"

Defaults

This file contains default variables for the role. These variables can be overridden by variables defined in playbooks or inventory.

# defaults/main.yml
apache_port: 80

Templates

Templates in Ansible are files that contain placeholders for variables, allowing you to generate dynamic configuration files. They are processed using the Jinja2 templating engine.

Jinja2 Templating

Jinja2 is a powerful templating engine that allows you to use control structures, filters, and expressions.

For example:

Hello, {{ user }}! Today is {{ ansible_date_time.date }}.

Creating Templates

To create a template, create a .j2 file in the templates directory of your role:

# templates/my_template.j2
server_name: {{ inventory_hostname }}
document_root: /var/www/{{ inventory_hostname }}

Using Templates in Playbooks

You can use the template module to deploy configuration files from templates:

- name: Deploy configuration file
  template:
    src: my_template.j2
    dest: /etc/my_config.conf

Example of Template Usage

- hosts: webservers
  roles:
    - my_role
  tasks:
    - name: Deploy Apache configuration
      template:
        src: my_template.j2
        dest: /etc/apache2/sites-available/{{ inventory_hostname }}.conf

Now you can start to do fun things 🥳!