Before we get started on working with our next configuration management system, Chef, I thought I’d post a follow-up to last week’s article covering Ansible. In this article, I will be touching up on some of the conclusions I drew last week, and my opinion on them now, and also some of the things I have learned about working with playbooks.
Programmability
First off, some things on programmability:
Advantages of sequential execution
I really do enjoy the top-down nature of Ansible. You don’t really have to set up any sort of dependency chain for classes or resources, which can really work to your advantage.
Consider the scenario where you have a file that needs to be written to or a command that needs to be run only when a specific package is installed.
In Puppet, this needs to be written:
file { '/etc/example/example.conf': content => template('example_mod/example.conf.erb'), require => Package['example'] } package { 'example': ensure => installed }
This is not so bad, and in fact, the declarative nature of Puppet makes it pretty easy to manage this stuff.
But what if you try to require a different resource, like a class? Or multiple packages?
Your child resource could end up with dependencies like this, which would then need to be repeated:
file { '/etc/example/example.conf': content => template('example_mod/example.conf.erb'), require => [ Package['example1'], Package['example2'], Package['example3'] ] }
You may also try to chain off a class or service, however when I have attempted these kinds of things, I have just ran into dependency loops.
Then you get so far down the rabbit hole you need a grapher. Consider the following article regarding dependency issues. When I need a whole article to try to figure out how to properly structure dependencies, I honestly tune out.
In Ansible, you don’t really need all of this. You just make sure your package tasks are set up before the tasks that depend on them:
- name: Install packages yum: name='{{ item }}' state=present with_items: - example1 - example2 - example3 -name: Put the file where it needs to go now template: src=example.conf.j2 dest=/etc/example/example.conf mode=0644
In this instance, if the first task fails, the other ones will not execute.
Another easy check that you can stick at the start of a playbook is an OS check or what not, such as:
- name: check for supported OS fail: msg=Unsupported distribution (Ubuntu 14.04 supported only) when: ansible_distribution_release != 'trusty'
This will fail if the playbook is not running against anything but Ubuntu 14.04, allowing you to have the rest of the playbook run without worrying about it being run against an unsupported OS.
Frustrations with bad error messages
A few things I have noticed so far with Ansible is the Python-ish error messages you get out of it, sometimes, unfortunately, out of context. I have gotten error messages with stack traces in them (ie: when processing Jinja2 stuff and also when a module fails thru some sort of untrapped Python method). I actually don’t mind this, because at least I know where to start looking.
It’s error messages like this that drive me up the wall.
msg: Error in creating instance: 'str' object has no attribute 'get' FATAL: all hosts have already failed -- aborting
This happened to me while trying to mess with the nova_compute
module. Where exactly am I supposed to start looking for something wrong here? I have even attempted to use pdb to try and chase it down, but I had to give up because I had other stuff to do. I may try again, but I really am on my own here, barring outside help. This error message does nothing to tell me where my problem is or how to correct it.
It almost would have been better if the error was left un-handled, so I could see the Python stack trace.
Playbooks
And also, some thoughts and bits of knowledge on playbooks.
The playbook as a site configuration
If you looked at the previous article, I had a bit of trouble trying to figure out what a playbook really was in relation to how Puppet is laid out. At first glance, it seemed to me that a playbook was close to what a module was, but that’s not really the case at all.
Playbooks are modular, sure, but they are more like individual site configurations more than anything else. Think about them as individual Puppet or Chef site installs.
According to the Ansible playbook best practices guide there is a very specific layout. This is reproduced below:
production # inventory file for production servers stage # inventory file for stage environment group_vars/ group1 # here we assign variables to particular groups group2 # "" host_vars/ hostname1 # if systems need specific variables, put them here hostname2 # "" library/ # if any custom modules, put them here (optional) filter_plugins/ # if any custom filter plugins, put them here (optional) site.yml # master playbook webservers.yml # playbook for webserver tier dbservers.yml # playbook for dbserver tier roles/ common/ # this hierarchy represents a "role" tasks/ # main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file templates/ # <-- files for use with the template resource ntp.conf.j2 # <------- templates end in .j2 files/ # bar.txt # <-- files for use with the copy resource foo.sh # <-- script files for use with the script resource vars/ # main.yml # <-- variables associated with this role defaults/ # main.yml # <-- default lower priority variables for this role meta/ # main.yml # <-- role dependencies webtier/ # same kind of structure as "common" was above, done for the webtier role monitoring/ # "" fooapp/ # ""
Note how everything is accounted for here. For someone like me who comes from Puppet, there are some easy analogs here that have helped me:
site.yml
and the other playbooks are pretty similar to Puppet site manifest files. From them entire configurations can be laid out without a need for anything else, or different roles can be referenced.group_vars
andhost_vars
behave similar to Hiera for storing variable overrides.- Finally, the
roles
structure is the closest thing that Ansible has to Puppet modules.
Honourable mentions to the library
and filter_plugins
directories. Ansible modules are actually closer to the core than Puppet’s, and normally do not need to be written, but you may find yourself doing so if your roles require functionality that cannot be reproduced in any existing module and you want that recycled across modules. Also, jinja2 filter plugins can be written (we give a small example below).
Roles
As mentioned, roles are probably the closest thing that Ansible has to a puppet module. These are recyclable “playbooks” that are self-contained and are then included within a proper playbook via the following means:
- hosts: webservers sudo: yes roles: - common - webtier - monitoring
This would ensure that webservers got any tasks related to the common
, webtier
, and monitoring
roles, running under sudo. Imagine that this means that some basic common tasks are done (ie: maybe push out a set of SSH keys and make sure that DNS servers are set correctly), set up a webserver, and install a monitoring agent.
Similar to Puppet or Chef, there is a file structure to roles that allows you to split out various parts of the roles to different directories:
tasks
for role tasks, the main workflow of a role and Ansible itself.handlers
for server actions and what not (ie:restart webserver
)templates
to store your jinja2 templates fortemplate
module tasksfiles
for your static contentdefaults
to define default variablesvars
to define local variables (NOTE: I have decided to stay away from these and I recommend you do too. Use defaults!)
main.yml
is the universal entry point for all of these. from here, you can include
all you wish. Example:
- name: check for supported OS fail: msg=Unsupported distribution (Ubuntu 14.04 supported only) when: ansible_distribution_release != 'trusty' - include: install_pkgs.yml
This would be a tasks/main.yml
file and demonstrates how you can mix includes and actual tasks in the role.
Group and host variables
This is where I would recommend you store variables if you need them. The obvious place would be group first, then host if you need specific host variables.
For example, this would apply foo=bar
to all webservers for all plays:
# group_vars/webservers.yml --- foo: bar
You can also include them in the playbook directly, ie: like so:
- hosts: webservers sudo: yes roles: - common - webtier - monitoring vars: foo: bar
Secure data
ansible-vault
is a very basic encryption system that encrypts an entire file (does not need to be YAML) off the command line. It hooks into ansible
and ansible-playbook
via the --ask-vault-pass
switch that will take any encrypted files it encounters and decrypt them.
It is recommended that anything with sensitive data be encrypted. The one main difference here is that as opposed to Hiera-eyaml, for example, the entire file is encrypted, so you may wish to separate encrypted and unencrypted data through separate files, if it can be helped. This ensures non-sensitive data can still be published to source control and be reviewed.
Unfortunately, it does not look like there is a certificate or private/public key system in use here, so remembering the password is very important, as is picking a secure one. Passwords can also be stored in a file (make sure it’s set to mode 0600), and passed using the --vault-password-file
option under all Ansible utilities, assumedly.
Filter Plugins for jinja2
And finally, I wrote that Jinja2 plugin I was hitting the wall with. 😉 It’s very simple and can be found below. This would go into the filter_plugins
directory.
# Add string split filter functionality def split(value, sep, max): return value.split(sep, max) class FilterModule(object): def filters(self): return { 'split': split }
This now gives you the split
filter that can be invoked like so:
{{ (example|split('.'))[0] }}
Assuming example.com
, this would give example
.
“Final” Thoughts
To be honest, I think that Ansible is more of an orchestration tool rather than a configuration management system. The agentless push nature of it, combined with its top-down workflow, roll-your-own inventories, and loosely enforced site conventions give it a chaotic feel that, even after giving it a week, still does not feel like configuration management to me.
This is not to say it’s a bad tool in any right, in fact, quite the opposite. Ansible is a great addition to my toolbox, and I am looking forward to applying it in everyday engineering. The above points that may be “weaknesses” when considering configuration management can equally be strengths when you need to get something done quickly and with as little of a footprint as possible.
Further to that, Ansible can be combined with something like Chef or Puppet, with each tool playing on its strengths. Ansible can be used to bootstrap either tool, with Chef or Puppet doing things like managing SSH keys (such as one for Ansible), enforcing configuration, etc. Ansible can be used in place of something like knife
or puppet kick
to force updates as well.
Some tools need work: Vault, for example, could do better to be more than just a wrapper for encryption, and set up some sort of keypair structure to reduce the amount of typing you have to do. And error messaging could do with a bit of work so that engineers are not scratching their heads trying to figure out what variable caused Python to barf when errors are trapped.
All in all though, I really like Ansible. There are some other projects that I want to try it with, namely working with the nova_compute and vsphere_guest modules to help automate orchestration even further.
I know that I mentioned that Chef was going to be this Sunday. Unfortunately (or maybe fortunately) I spent a bit more time with Ansible than I thought I was going to, and the research has not been done to properly get a Chef article up yet – ie: I have not yet started. 😛 in any case, that will be happening next – so stay tuned for the it next weekend!