Ansible Roles and Variables

While automation is great, we have to be careful not to recreate past problems. What I mean is that playbooks should be written in a generic fashion that can be applied to more than one host. If we’re writing playbooks that only work on one single host, we aren’t much further ahead than we were before.

Two of the key components of making playbooks reusable are Ansible variables and roles.  Let’s try and define each of them individually and while showing some examples along the way.

Roles
Roles allow you to call a set of variables, tasks, and handlers by simply specifying a defined role.  Roles require the use of a defined file structure in order to work.  Per the Ansible documentation, that structure looks like this…

image 
Roles are really just a way to split up your playbook into smaller reusable parts.  For instance, let’s consider we added another host to our lab…

image 
Now look at this playbook…

Its a modification of the playbook we used in our first post.  It includes the original play, but also a second play that targets only the second Ansible client.  The second play copies an amazing page off the of the well known website www.purple.com and makes it the index page for our 2nd client.  Since both clients need the web server configuration, we can simplify this playbook by making the web server configuration its own role.  To do this, we create a file structure as show above for this role…

Note: Theres an easier way to make this folder structure using Ansible galaxy but that’s out of scope for this post.

While we’re creating all the possible folders required in a role, we’ll only be focusing on the tasks and handlers folders at this point.  The first thing we want to do is create a ‘main.yml’ file in the tasks folder that looks like this…

Notice that this is just the task that was defined in our initial playbook.  Next create another ‘main.yml’ file under the handler directory that looks like this…

Same thing here.  All we did was take the handler information from the original playbook definition and move it into the handler directory.  The last piece is to modify the playbook to call the role rather than the task.  Modify your playbook to look like this…

Petty simple huh?  Now if we want to run the tasks associated with building a web server we can just call that role.  If we put ansibleclient1 back to it’s original default state, running the playbook should look like this…

image
Very similar output to what we saw the last time we ran a playbook.  A quick test of browsing to each server should show the results we’re looking for…

image 
As you can see, roles can come in pretty handy for generic tasks.  Keep in mind that we’ve only scratched the surface of the role construct here to get you up and running.  We’ll be talking more about other aspects of roles in later posts. 

Variables
One of the ways to make playbooks more generic is to use Ansible variables. Variables can be defined in multiple locations.  Let’s walk through each one of them and a quick example…

In the inventory file
Variables can be assigned right along with the host definition in your inventory file.  For instance, look at this example host file…

Note: In many places you’ll see variables assigned in the above manner referred to as ‘host_vars’ and ‘group_vars’.

In the above example, we set variables at both a group and an individual host level.  Here we define that the variable ‘webserver_package’ is httpd for each host.  Then, at the group level, we set the variable ‘http_server_port’ to 80.  We can then change our task definition (main.yml) to look like this…

And our handler definition to look like this…

In both cases, we’re making use of the variables defined as part of the role definition.  While we can certainly define variables as part of the inventory file itself, best practice calls for these variables to be stored in separate files.  Much like many other components of Ansible, this relies on a default folder structure. Assuming you are using the default inventory file (/etc/ansible/hosts) you need to create two additional folders…

For host variables – /etc/ansible/host_vars
For group variables – /etc/ansible/group_vars

Inside of these folders, you place YAML files named after the host or group you wish to define variables for.  For instance, my folder structure might look like this…

image
And the files definitions themselves would look like this…

In the playbook itself
These same variables can also be defined directly within the plays.  If we remove the variables from the host inventory file, we can add them directly into one of the plays in the playbook definition.  Keep in mind, this is in the playbook definition, not in the role definition, we’ll cover that shortly.  So the playbook would look like this…

Notice that we now have a ‘vars:’ section defined at the top of the playbook. 

In the role definition
Variables can also be defined as part of a role.  There are two places this can be done with variables defined in either the ‘vars’ or the ‘default’ directory of the role.  Much in the same way we defined other role functions, we can also define a ‘main.yml’ file in both of these directories that define variable to use as part of the role.  For instance let’s assume that we have the following defined as ‘/etc/ansible/roles/webserver/defaults/main.yml’

and we have the following defined as ‘/etc/ansible/roles/webserver/vars/main.yml’

Above we define the same variables as previous examples, but in the case of ‘webserver_package’ we’ve defined them in both files.  When the playbook is run, we’ll find that the value from ‘vars’ directory is the one that’s used.  So why is that?  The ‘default’ directory stores values that are role default.  That is, they are sane default settings which can be overridden in certain circumstances.  Defaults can be overridden using values from the ‘vars’ directory, group vars, or host vars.  At the time of execution the variables are merged and precedence rules are applied to figure out the winning values for all of the variables.  The default precedence model looks like this…

-Variables defined in role ‘defaults’
-Variables defined as group_vars
-Variables defined as host_vars
-Variables defined in plays
-Variables defined in role ‘vars’
-Variables defined at runtime with –e switch

All of those should be familiar to you now except for the runtime option which we’ll cover last.  Priority increases as you move down the list.  That is the, if you want to override absolutely any other defined variable, you should do it at  runtime with the ‘–e’ flag. 

At runtime
The last, and most powerful, place to define variables is when you execute a playbook.  To prove this, lets go back to our role example and change the variables we defined.  We’ll remove the ‘main.yml’ file from the ‘default’ folder, and change the ‘main.yml’ in the ‘vars’ folder to look like this…

If we run the playbook now, it will fail since these values are the only ones defined (and because this a poorly written example playbook that doesn’t use all the variables in all the right places).  If we want it to work as intended, we can rerun the playbook by specifying variables at runtime.  To do that, we run this command…

Note: Its important to put the single quotes around the variables when passing more than one variable in this manner.

When we do this, the variables we provide at runtime will have precedence over the ones defined in the ‘vars’ directory and we’ll get a successful playbook execution resulting in the web servers working as expected…

image

The more I play with Ansible the more I like it. The role construct coupled with scoped variables is pretty powerful.  I hope you found this look at roles and variables useful, comments always welcome!

Tags:

Reply

Your email address will not be published. Required fields are marked *