Ansible Playbooks: Files Versus Templates

In software development it is a good practice to avoid so-called “magic numbers” in your code. Instead of wait = 30, use a variable called numberOfSecondsToPause and write wait = numberOfSecondsToPause. It’s good documentation and even you won’t remember what 30 means when you look at the code a few months later. This applies to Ansible playbooks as well.

I have started using templates for most of my configuration files for this reason. I define my network variables in the group_vars/all file and then reference them in my templated configuration files. So, instead of I use {{ ntp_server }}. Instead of 8ad91af3-0075-f996-9c8b-d9301d4aa802 I use {{ centos_template_uuid }}.

The all file starts to look like a description of your network which is a good thing. This has the additional benefit of making changes easier. If you want to change the IP address of your NTP server you only have to change it in one place. I now use static files (the Ansible copy module) for files that contain boilerplate that doesn’t change. For example, I use a template for the DNS configuration file named.conf but a static copy for named.rfc1912.zones (which just has the standard localhost data in it).

Categories: DevOps


Share Your Ideas

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: