我有下面的Ansible剧本文件,它试图管理一组printers.conf 6框上的CentOS。
---
# 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服务停止的条件=>接收到错误“致命: hostxxx locst.stat.md5错误,同时计算条件:locst.stat.md5 ne remst.stat.md5”。
是否有一种方法可以查看正在计算的条件中的值?在这里添加-vvv并没有帮助我。
或者还有其他方法来调试条件?
EDIT1:
显然,stat模块总是远程的--它无法与角色/common/ printers.conf /printers.conf中的本地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值编码到任务中。
这个堆栈过流问题正在寻找几乎相同的东西。
EDIT2:
尽管我现在能够让stat模块对本地文件执行,使用local_action和更长的工作路径--绕过本地操作中缺少角色路径搜索 --但在计算条件时仍然会遇到相同的错误,尽管有有效的.stat.md5值。
- local_action: stat path=roles/common/files/printers.conf
register: locst然而,我确实注意到,md5值出乎意料地不同。在运行时,cups似乎重写了printers.conf文件,其中包括一个名为"StateTime“的时间戳。简单地通过编写配置文件来管理cups就够了。
AFAICT是清洁管理cups的唯一方法,因此每次它只会关闭服务,要么在比较之前过滤现有的printers.conf,要么编写一个webscraper来对您希望用来配置打印机的接口cups进行操作。
发布于 2015-04-15 05:03:33
我试着使用AlexKing的答案,但是无法使“扩展”正确工作。因此,下面是我最后得到的(测试和工作)
- name: Install Printer Driver | Create Kyocera directory as required
file: dest=/usr/share/cups/model/Kyocera mode=0755 state=directory owner=root
- name: Install Printer Driver | Copy PPD file
copy: src=Kyocera_TASKalfa_4551ci.PPD dest=/usr/share/cups/model/Kyocera/Kyocera_TASKalfa_4551ci.PPD owner=root group=lp mode=0444
- name: Install Printer Driver | Ensure cups is stopped before updating the printers.conf file
service: name=cups state=stopped
- name: Install Printer Driver | Configure Printer
tags: configuration
copy: src=printers.conf dest=/etc/cups/printers.conf mode=600 owner=root group=lp backup=yes
- name: Install Printer Driver | Ensure cups is running
service: name=cups state=started根据我对Ansible工作原理的理解,您不需要检查文件是否不同--这就是复制模块中已经发生的情况。
HTH,
拉塞尔。
发布于 2016-06-28 21:53:02
您的方法可能很少有潜在的问题:
- stat: path=printers.conf指的是远程位置,而不是{{ playbook_dir }}/roles/common/下的本地文件get_md5=yes/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
发布于 2014-10-26 09:11:24
假设仅在文件不同时运行验证命令,并因此尝试替换该命令,则可以“扩展”验证功能以停止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系统没有影响),但是根据docs,ansible是必需的。请参阅module.html
https://stackoverflow.com/questions/26168051
复制相似问题