title: How can I cleanly manage printers.conf with Ansible?
body: <p>I have the following Ansible playbook file, which attempts to manage the printers.conf on a set of CentOS 6 boxes.</p>
  
  <pre><code>---
  # 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: &gt;
      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
  </code></pre>
  
  <p>Unfortunately, I receive the error "fatal: [hostxxx] => error while evaluating conditional: locst.stat.md5 ne remst.stat.md5" from the <code>when</code> conditional controlling the stopping of the cups service.</p>
  
  <p>Is there a way to see the values in the conditional being evaluated? Adding <code>-vvv</code> hasn't helped me here.</p>
  
  <p>Or is there another way of debugging the conditional?</p>
  
  <p><strong>EDIT1:</strong></p>
  
  <p>Apparently the stat module is always remote - it fails to match against the local printers.conf in roles/common/files/printers.conf</p>
  
  <pre><code>TASK: [common | stat path=printers.conf] **************************************
  &lt;hostxxx&gt; ESTABLISH CONNECTION FOR USER[...]
  &lt;hostxxx&gt; REMOTE_MODULE stat path=printers.conf
  [...]
  ok: [hostxxx] =&gt; {"changed": false, "stat": {"exists": false}}
  </code></pre>
  
  <p>This would be the source of my "error while evaluating conditional".</p>
  
  <p>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.</p>
  
  <p>This <a href="https://stackoverflow.com/q/22395733/761374">stackoverflow question</a> is looking for pretty much the same thing.</p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>Although I'm now able to get the stat module executing against the local file, using <a href="http://docs.ansible.com/playbooks_delegation.html" rel="nofollow noreferrer"><code>local_action</code></a> and a longer path to work-around the <a href="https://github.com/ansible/ansible/issues/5344" rel="nofollow noreferrer">lack of role-path searching in local actions</a>, I still get the same error while evaluating conditional, despite having valid .stat.md5 values.</p>
  
  <pre><code>- local_action: stat path=roles/common/files/printers.conf
    register: locst
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>