Introducing roles

Overview

In this example, we will take a quick digression to look at Ansible Roles and how they can be used to make your code more reusable.

As roles are a core feature of Ansible — i.e. they are completely independent of the IONOS Core Ansible Module — we won't go into any explanation of their structure or how to create them (though a few useful references are mentioned at the bottom of this document); rather, this is more about providing an example that you can look at and (potentially) use as a starting point for some of your own sample projects.

Before you begin

If you're not already familiar with the concepts of roles, you might want to look at the Ansible Roles section of the official documentation before continuing with the rest of this tutorial.

What's in this example?

This example includes and/or depends on the following files:

FileDescription

part-1.yml

This playbook provisions a single server and creates a few temporary files that are used by the subsequent playbooks

part-2.yml

This playbook runs on the newly-provisioned server and applies a few roles to this VM

part-3.yml

This playbook cleans up all of the resources provisioned in Part 1, and should be run, once you're done with the earlier parts

vars.yml

This file contains the variable definitions common to part-1.yml, part-2.yml and part-3.yml

../vars.yml

This file is common to all of our Ansible examples and contains a set of more generally-used variable definitions

templates/ssh_config.j2

A simple Jinja template, in this case, to dynamically create the ssh_config file mentioned above

roles/

A set simple, unofficial but usable roles that will be used in this example, and which you cold look at and/or edit

Usage

  1. To provision our 'starting' infrastructure, run the following command:

    ansible-playbook part-1.yml
  2. To execute the 'meat' of this example, run the following command (possibly after enabling verbose_debugging — see below):

    ansible-playbook -i inventory.yml part-2.yml
  3. Optionally, examine the outputs of the previous step; to ssh into the VM, type the following from the same directory in another shell:

    ssh -F ssh_config example-server
  4. Execute the following to delete the resources provisioned in the previous steps:

    ansible-playbook part-3.yml

Resources and references

Last updated