Reputation: 176
I am provisioning a CentOS 7 machine using Ansible (v2.1.2). For this, I need to ensure that the version available in a repository for a given package is installed on this machine, installing and upgrading/downgrading the package whenever needed. I previously ran
- name: install pkg package yum: name: "{{ pkg }}" state: latest
but this will install only the latest version (on certain versions of Ansible not even this https://github.com/ansible/ansible-modules-core/issues/4229) and not e.g. downgrade.
On the other hand, I can do
- name: install pkg package yum: name: "{{ pkg }}" state: present - name: sync pkg version with repo command: yum distro-sync -y "{{ pkg }}"
This works like a charm, with the only inconvenience that Ansible will always show the task as changed
, even when the distro-sync has not actually done anything
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"]}
The changed message will also suggest to use the Ansible YUM module docs but it doesn't appear to have the distro-sync option.
Is there a way to make it show ok
when there are no changes instead of changed
?
Upvotes: 0
Views: 1833
Reputation: 1579
You can register the stdout of your distro-sync task and evaluate if it actually did anything with 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"
I was testing with CentOS 6 so 'No Packages marked for Distribution Synchronization' may be different on your system.
Upvotes: 2