Hi all,
I was wondering if anyone has a strategy for recording when a playbook has been run against hosts in an inventory, and crucially which version of the repository (Git-based, in my case) was checked out when the playbook ran? Essentially I’d like to maintain a ‘version number’ for each instance that indicates when they were last provisioned.
Cheers!
Craig
In a similar situation I simply added a play to the role that stores a version number as a local fact. Then on subsequent runs I can check that local fact. The only proviso for that is that it is possible for someone to change the fact so don't rely on it for anything truly important.
Here’s how I set the fact…
Create the local facts directory if it doesn’t exist
- name: ensure that the local facts directory exists
file: state=directory path=“/etc/ansible/facts.d” mode=0755
Set a local fact so that we don’t do this again
- name: update the widget facts file so that this is not run next time.
ini_file: dest=/etc/ansible/facts.d/widgets.fact section=installed
option=version value={{ widgets_latest_version }} mode=0644
and in my case I add the following to all of the actual widget installation tasks so that I don’t reinstall (not quite your scenario)
when: “widgets_installed_version != widgets_latest_version”
and in that role I have a defaults/main.yml which sets these…
widgets_installed_version: “{{ ansible_local.widgets.installed.version | default(0) }}”
widgets_latest_version: “latest”
In your case you won’t need to check those local facts (unless you want to) but you can set as many facts as you want. The last modified time of the /etc/ansible/facts.d/widgets.fact file will tell you when it was last changed. If you rerun the playbook with the same version it will not update that time, if you store other facts you may also want to store times with them if they are all in the same facts file.
I hope that this helps,
Adam
Hi Craig,
I think what you’re looking for is callback plugins, but you could probably also use a notification module as well. There are several in Github as examples. The callback plugin could, for example, record something to a file, or to a database, or merely make an API call to some custom application that records the information you’re looking for.
http://docs.ansible.com/developing_plugins.html#callbacks
Ansible Tower contains some very nice logging of job history you may be interested in:
http://www.ansible.com/tower
This is probably the easiest way to go because you don’t have to build it yourself, but there’s still a searchable REST API to access everything.
Thanks guys, think each of these are exactly what I need. I’ll give them a try. Thanks for the help!