2014-10-02 14 views
2

我有以下Ansible playbook文件,它試圖在一組CentOS 6盒子上管理printers.conf。如何使用Ansible乾淨地管理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 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 

不幸的是,我收到錯誤從when條件控制所述杯服務的停止「致命:locst.stat.md5 NE remst.stat.md5:[hostxxx] =>錯誤而評價條件」。

有沒有辦法看到被評估的條件中的值?在這裏添加-vvv並沒有幫助我。

還是有另一種方式來調試條件?

EDIT1:

顯然,統計模塊始終遠程 - 它不能用於匹配在角色本地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值編碼到任務中。

這個stackoverflow question正在尋找幾乎相同的東西。

EDIT2:

雖然我現在能夠得到的統計模塊對本地文件執行,使用local_action和較長的路徑變通的lack of role-path searching in local actions,我仍然得到同樣的錯誤,而評估有條件的,儘管有有效的.stat.md5值。

- local_action: stat path=roles/common/files/printers.conf 
    register: locst 

但是,我注意到md5值意外地不同。看來在運行時,cups會重新編寫printers.conf文件,其中包括所有內容,稱爲「StateTime」的時間戳。非常簡單,通過編寫配置文件來管理杯子。 AFAICT,乾淨地管理杯子的唯一方法,使得它只會每次取下服務,都是在比較之前過濾現有的printer.conf,或者不太合理,編寫webscraper來操作接口杯希望您用來配置打印機。

+0

我在-vvv輸出中找到了錯誤的源頭:「stat path = printers.conf」與本地文件不匹配。 – 2014-10-02 19:29:35

回答

0

假設ansible只運行,如果該文件不同的validate命令,因此將試圖取代它,你可以「擴展」的驗證功能來阻止杯:

- 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系統沒有任何影響),但根據文檔需要安全。見http://docs.ansible.com/copy_module.html

1

我試圖從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,

羅素。

0

有可能是您的方法幾個潛在的問題:

  • - stat: path=printers.conf指的是遠程位置,而不是下{{ playbook_dir }}/roles/common/
  • MD5處理本地文件默認情況下不啓用。你會明確寫入get_md5=yes
  • 用戶運行的劇本可能需要有須藤permitions根據您的問題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 

正確使用2.1.0.0