# Create the hooks helper, passing a list of hooks which will be # handled by default by running all sections of the playbook # tagged with the hook name. hooks = charmhelpers.contrib.ansible.AnsibleHooks( playbook_path='playbooks/site.yaml', default_hooks=['start', 'stop', 'config-changed', 'solr-relation-changed']) @hooks.hook() def install(): charmhelpers.contrib.ansible.install_ansible_support(from_ppa=True)
And that’s it.
If I need something done outside of ansible, like in the install hook above, I can write a simple hook with the non-ansible setup (in this case, installing ansible), but the decorator will still ensure all the sections of the playbook tagged by the hook-name (in this case, ‘install’) are applied once the custom hook function finishes. All the other hooks (start, stop, config-changed and solr-relation-changed) are registered so that ansible will run the tagged sections automatically on those hooks.
Why am I excited about this? Because it means that practically everything related to ensuring the state of the machine is now handled by ansibles yaml declarations (and I trust those to do what I declare). Of coures those playbooks could themselves get quite large and hard to maintain, but ansible has plenty of ways to break up declarations into includes and roles.
It also means that I need to write and maintain fewer unit-tests – in the above example I need to ensure that when the install() hook is called that ansible is installed, but that’s about it. I no longer need to unit-test the code which creates directories and users, ensures permissions etc., or even calls out to relevant charm-helper functions, as it’s all instead declared as part of the machine state. That said, I’m still just as dependent on integration testing to ensure the started state of the machine is what I need.
I’m pretty sure that ansible + juju has even more possibilities for being able to create extensible charms with plugins (using roles), rather than forcing too much into the charms config.yaml, and other benefits… looking forward to trying it out!
 The merge proposal still needs to be reviewed, possibly updated and landed :)
Filed under: Uncategorized Read more