首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Ansible干净地管理printers.conf?

如何使用Ansible干净地管理printers.conf?
EN

Stack Overflow用户
提问于 2014-10-02 19:21:52
回答 3查看 5.2K关注 0票数 3

我有下面的Ansible剧本文件,它试图管理一组printers.conf 6框上的CentOS。

代码语言:javascript
复制
---
# 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匹配。

代码语言:javascript
复制
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值。

代码语言:javascript
复制
- local_action: stat path=roles/common/files/printers.conf
  register: locst

然而,我确实注意到,md5值出乎意料地不同。在运行时,cups似乎重写了printers.conf文件,其中包括一个名为"StateTime“的时间戳。简单地通过编写配置文件来管理cups就够了。

AFAICT是清洁管理cups的唯一方法,因此每次它只会关闭服务,要么在比较之前过滤现有的printers.conf,要么编写一个webscraper来对您希望用来配置打印机的接口cups进行操作。

EN

回答 3

Stack Overflow用户

发布于 2015-04-15 05:03:33

我试着使用AlexKing的答案,但是无法使“扩展”正确工作。因此,下面是我最后得到的(测试和工作)

代码语言:javascript
复制
- 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,

拉塞尔。

票数 1
EN

Stack Overflow用户

发布于 2016-06-28 21:53:02

您的方法可能很少有潜在的问题:

  • - stat: path=printers.conf指的是远程位置,而不是{{ playbook_dir }}/roles/common/下的本地文件
  • 默认情况下不启用md5处理。您将显式地编写get_md5=yes
  • 运行剧本的用户可能需要有一些统计/etc/cups/printers.conf的功能。

根据您的问题提出的解决方案:

代码语言:javascript
复制
---
- 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

处理人员:

代码语言:javascript
复制
---
- name: restart cups
  service: name=cups state=restarted
  become: yes

使用ansible 2.1.0.0

票数 1
EN

Stack Overflow用户

发布于 2014-10-26 09:11:24

假设仅在文件不同时运行验证命令,并因此尝试替换该命令,则可以“扩展”验证功能以停止cups:

代码语言:javascript
复制
- 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26168051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档