Victor Castell   Dkron    About

Ansible tip 2

When developing a role to do a task, you often need to declare some variables to parametrize aspects of the role.

It’s not a good idea to name a role variable like:

http_port
host_name
path

As a general rule, namespace those variables based on the role name.

People can find collisions with their own defined group/host variables in their infrastructure.

Using the same mongodb role example of my previous post

#file roles/mongodb/defaults/main.yml
mongodb_version: '2.6.1'
mongodb_data_path: '/var/lib/mongodb'
mongodb_rs_hosts: ['mongo1', 'mongo2']

Naming this way it’s less probably that you will find any unintended interaction with variables defined by the user using your role, not impossible though.

Another idea that you want to avoid is to place your default variables nested in tree, like:

mongodb:
  version: '2.6.1'
  data_path: '/var/lib/mongodb'
  rs_hosts: ['mongo1', 'mongo2']

You want to avoid this because it’s not possible to override only one of those vars without overriding the whole set:

mongodb:
  version: '2.6.2'

Will unset all other values in the tree, an effect that you don’t want.

Written on May 13, 2014.