在 ansible 上安装 yum distro-sync

yum distro-sync on ansible

我正在使用 Ansible (v2.1.2) 配置一台 CentOS 7 机器。为此,我需要确保在这台机器上安装了给定包的存储库中可用的版本,并在需要时安装和 upgrading/downgrading 包。我以前 运行

- 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

您可以注册您的发行版同步任务的标准输出并评估它是否真的对 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 测试的,所以 'No Packages marked for Distribution Synchronization' 在你的系统上可能会有所不同。