如何使用Ansible干净地管理printers.conf?
我有以下Ansible playbook文件,它试图在一组CentOS 6设备上管理printers.conf如何使用Ansible干净地管理printers.conf?,ansible,cups,Ansible,Cups,我有以下Ansible playbook文件,它试图在一组CentOS 6设备上管理printers.conf --- # 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 fil
---
# 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
不幸的是,当条件控制cups服务停止时,我从接收到错误“fatal:[hostxxx]=>error when evaluation conditional:locst.stat.md5 ne remst.stat.md5”
是否有办法查看正在计算的条件中的值?在这里添加-vvv
对我没有帮助
或者是否有其他方法调试条件语句
EDIT1:
显然,stat模块总是远程的——它无法与roles/common/files/printers.conf中的local 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}}
然而,我确实注意到md5值出人意料地不同。似乎在运行时,cups会重新写入printers.conf文件,其中包括一个名为“StateTime”的时间戳。通过编写配置文件来管理cups的简单方法到此为止
AFAICT是清洁管理cups的唯一方法,因此每次只能关闭服务,要么在比较之前过滤现有的printers.conf,要么就不那么合理,要编写webscraper以针对cups希望您用于配置打印机的接口进行操作。假设ansible仅在文件不同时运行validate命令,因此将尝试替换该文件,您可以“扩展”validate功能以停止cups:
- name: Configure printers
tags: configuration
copy: src=printers.conf dest=/etc/cups/printers.conf mode=600 owner=root group=lp validate="service cups stop %s"
- name: Ensure cups is running
service: name=cups state=started
%s对于服务来说是多余的(但对我的debian系统没有影响),但是根据文档,ansible是必需的。参见我试图从阿列克斯金那里得到答案,但无法让“扩展”正常工作。因此,以下是我最终得到的(测试和工作)
根据我对Ansible工作原理的理解,您不需要检查文件是否不同——这是复制模块中已经发生的事情
嗯,
Russell.您的方法可能存在一些潜在问题:
-stat:path=printers.conf
指的是远程位置,而不是{{playbook\u dir}}/roles/common/
- 默认情况下未启用md5处理。您需要明确地编写
get\u md5=yes
- 运行playbook的用户可能需要具有stat
/etc/cups/printers.conf
根据您的问题提出的解决方案:
---
- name: "cups - installation"
apt: name=cups state=installed update_cache=yes cache_valid_time=3600
become: yes
- name: "cups - md5 on local printers.conf"
local_action: stat path={{ playbook_dir }}/roles/homie/files/printers.conf get_md5=yes
register: locst
- name: "cups - md5 on remote printers.conf"
stat: path=/etc/cups/printers.conf get_md5=yes
register: remst
become: yes
- name: "cups - stop service"
service: name=cups state=stopped
when: not remst.stat.exists or (locst.stat.md5 != remst.stat.md5)
become: yes
- name: "cups - configure drivers"
copy: src=Canon_MG2900_series.ppd dest=/etc/cups/ppd/Canon_MG2900_series.ppd mode=640 owner=root group=lp backup=yes
become: yes
- name: "cups - configure printers"
copy: src=printers.conf dest=/etc/cups/printers.conf mode=600 owner=root group=lp backup=yes
notify: restart cups
when: not remst.stat.exists or (locst.stat.md5 != remst.stat.md5)
become: yes
和处理程序:
---
- name: restart cups
service: name=cups state=restarted
become: yes
使用ansible 2.1.0.0我在前面的-vvv输出中找到了错误源:“stat path=printers.conf”与本地文件不匹配。
---
- name: "cups - installation"
apt: name=cups state=installed update_cache=yes cache_valid_time=3600
become: yes
- name: "cups - md5 on local printers.conf"
local_action: stat path={{ playbook_dir }}/roles/homie/files/printers.conf get_md5=yes
register: locst
- name: "cups - md5 on remote printers.conf"
stat: path=/etc/cups/printers.conf get_md5=yes
register: remst
become: yes
- name: "cups - stop service"
service: name=cups state=stopped
when: not remst.stat.exists or (locst.stat.md5 != remst.stat.md5)
become: yes
- name: "cups - configure drivers"
copy: src=Canon_MG2900_series.ppd dest=/etc/cups/ppd/Canon_MG2900_series.ppd mode=640 owner=root group=lp backup=yes
become: yes
- name: "cups - configure printers"
copy: src=printers.conf dest=/etc/cups/printers.conf mode=600 owner=root group=lp backup=yes
notify: restart cups
when: not remst.stat.exists or (locst.stat.md5 != remst.stat.md5)
become: yes
---
- name: restart cups
service: name=cups state=restarted
become: yes