yum distro-sync on ansible

时间:2017-07-21 16:21:21

标签: centos ansible centos7 yum

我正在使用Ansible(v2.1.2)配置CentOS 7机器。为此,我需要确保在此计算机上安装了给定程序包的存储库中可用的版本,并在需要时安装和升级/降级程序包。我以前跑了

- 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 个答案:

答案 0 :(得分:2)

您可以注册发布同步任务的标准输出并评估它是否实际对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进行测试,所以'没有标记为分发同步的软件包'可能在您的系统上有所不同。