Ansible pkgin module improvements


The default pkgsrc package manager for a bunch of platforms, including SmartOS, is pkgin. While working on a role for dsapid (see: Setting up a SmartOS image server) I noticed that several pieces of functionality of pkgin were not supported by the Ansible module.

As a result one couldn't use the module in a newly provisioned zone. This was due to the fact that in a fresh zone the pkgin cache is not yet populated, so installing a package would fail. Secondly, it's become a habit to upgrade all the packages in zone when I manually log in the first time. Ansible couldn't do that either.

After the recent OpenSSL debacle one had to force a reinstall python in order to unbreak it. Again, Ansible couldn't handle it. *sadface*

Now, one could use a task such as following:

- name: reinstall python
  command: /usr/local/bin/pkgin -Fy python

However that feels very, very wrong.

Extending Ansible

So, this felt like a good moment to learn a thing or two about writing Ansible modules. First off was adding support for updating the local cache, with that in place we can now have a task to first update the cache, and then install a package:

- name: install tmux
  pkgin: name=tmux update_cache=yes

Or you can update the cache as a standalone task.

Logically we'd need a clean option too:

- name: clean pkgin caches
  pkgin: clean=yes

Next up were handling upgrades of individual/main packages (pkgin ug):

- name: upgrade tmux
  pkgin: name=tmux upgrade=yes

and all packages (pkgin fug):

- name: upgrade all packages
  pkgin: full_upgrade=yes

With the force option added, we could now force a python re-install in a more idiomatic manner:

- name: re-install python
  pkgin: name=python force=yes

Conclusion

To tie everything together, you can now update your caches and upgrade all packages in a install or zone with Ansible:

- name: upgrade packages
  pkgin: update_cache=yes full_upgrade=yes

Thanks to the Ansible community reviewers the pull requests have been merged upstream, so I expect this to be part of the upcoming Ansible 2.1 release for the extras modules!

Update: As pointed out by Jonathan Perkin via Sevan Janiyan, update_cache is no longer needed with pkgin 0.9.4.