2017-07-21 78 views
0

我使用Ansible(v2.1.2)供應CentOS 7機器。爲此,我需要確保在本機上安裝指定軟件包的存儲庫中可用的版本,並在需要時安裝和升級/降級軟件包。我以前跑yum distro-sync on ansible

 
- name: install pkg package 
    yum: 
    name: "{{ pkg }}" 
    state: latest 

但這僅安裝最新版本(上Ansible的某些版本不會連這個https://github.com/ansible/ansible-modules-core/issues/4229),而不是如降級。

在另一方面,我可以做

 
- name: install pkg package 
    yum: 
    name: "{{ pkg }}" 
    state: present 

- name: sync pkg version with repo 
    command: yum distro-sync -y "{{ pkg }}" 

這個工程就像一個魅力,唯一的不便Ansible將始終顯示任務爲changed,即使在發行版同步實際上並沒有做任何事

 
changed: [fake.host.com] => {"changed": true, "cmd": ["yum", "distro-sync", "-y", "mlocate"], "delta": "0:00:00.994328", "end": "2017-07-21 15:58:50.924873", "invocation": {"module_args": {"_raw_params": "yum distro-sync -y \"mlocate\"", "_uses_shell": false, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 0, "start": "2017-07-21 15:58:49.930545", "stderr": "", "stdout": "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirror.vorboss.net\n * epel: mirrors.coreix.net\n * extras: mirror.vorboss.net\n * updates: ftp.nluug.nl\nNo packages marked for distribution synchronization", "stdout_lines": ["Loaded plugins: fastestmirror", "Loading mirror speeds from cached hostfile", " * base: mirror.vorboss.net", " * epel: mirrors.coreix.net", " * extras: mirror.vorboss.net", " * updates: ftp.nluug.nl", "No packages marked for distribution synchronization"], "warnings": ["Consider using yum module rather than running yum"]} 

更改後的消息也將建議使用Ansible YUM module docs,但它似乎並不具備發行同步選項。

有沒有辦法讓它顯示ok當沒有改變而不是changed

回答

1

您可以註冊您的發行版同步任務的標準輸出,並評估它是否實際上用changed_when執行了任何操作。

- name: sync pkg version with repo 
    command: yum distro-sync -y "{{ pkg }}" 
    register: yum 
    changed_when: "'No Packages marked for Distribution Synchronization' not in yum.stdout" 

我使用CentOS 6進行測試,所以'沒有標記爲分發同步的軟件包'可能與您的系統不同。