I have the following Ansible playbook file, which attempts to manage the printers.conf on a set of CentOS 6 boxes.
---
# file: roles/common/tasks/config-cups.yml
# Configure printing
- name: ensure cups is installed
yum: pkg=cups state=installed
# We want to compare the local and remote printers.conf files so that
# we can predetermine if the copy needs to happen. According to a
# comment in the default printers.conf file, we can't write
# printers.conf while cups is running. But to be idempotent, we want
# to avoid stopping the cups service if we don't need to.
- stat: path=printers.conf
register: locst
- stat: path=/etc/cups/printers.conf
register: remst
# can't write printers.conf while running, so says the default file
- name: ensure cups is stopped
service: name=cups state=stopped
when: locst.stat.md5 ne remst.stat.md5
- name: Configure printers
tags: configuration
copy: >
src=printers.conf
dest=/etc/cups/printers.conf
mode=600 owner=root group=lp
notify:
- restart cups
- name: Enable the cups service
service: name=cups enabled=yes
- name: Ensure cups is running
service: name=cups state=started
Unfortunately, I receive the error "fatal: [hostxxx] => error while evaluating conditional: locst.stat.md5 ne remst.stat.md5" from the when
conditional controlling the stopping of the cups service.
Is there a way to see the values in the conditional being evaluated? Adding -vvv
hasn't helped me here.
Or is there another way of debugging the conditional?
EDIT1:
Apparently the stat module is always remote - it fails to match against the local printers.conf in roles/common/files/printers.conf
TASK: [common | stat path=printers.conf] **************************************
<hostxxx> ESTABLISH CONNECTION FOR USER[...]
<hostxxx> REMOTE_MODULE stat path=printers.conf
[...]
ok: [hostxxx] => {"changed": false, "stat": {"exists": false}}
This would be the source of my "error while evaluating conditional".
So I still don't know how to cleanly manage the file. I don't want to get into hand coding md5 values into the tasks.
This stackoverflow question is looking for pretty much the same thing.
EDIT2:
Although I'm now able to get the stat module executing against the local file, using local_action
and a longer path to work-around the lack of role-path searching in local actions, I still get the same error while evaluating conditional, despite having valid .stat.md5 values.
- local_action: stat path=roles/common/files/printers.conf
register: locst
I did, however, notice that the md5 values unexpectedly differed. It seems that during run-time, cups re-writes the printers.conf file, including, of all things, a timestamp called "StateTime". So much for a straightforward way to manage cups via writing a config file.
AFAICT, the only way to cleanly manage cups, such that it would only take down the service each time would be to either filter the existing printers.conf before comparison, or far less reasonable, to write a webscraper to operate against the interface cups wants you to use to configure printers.