I have no idea why stdout
does not contain the same information in your example. I can't reproduce this. I get the same output in both cases if I test against a fedora:latest
docker image. The only quirk in my case is that dnf check-updates
returns with rc=100
which needs to be ignored if you don't wont to end the playbook on what ansible considers a failure.
But I don't think using shell
or command
is the right way to deal with your requirement anyway.
Ansible provides a dnf
module you can use to handle your packages. The module has a list
parameter for which the documentation is a bit sparse. But it basically accepts the same values described for the yum
module
So this is how I would get the required information.
Note: I made a quickNdirty template to have an output that looks more or less like the one from dnf check-update
in a debug task. But you can debug the entire packages
registered var to see all available information and use it in whatever best way.
---
- hosts: all
gather_facts: false
tasks:
- name: Check packages to upgrade
dnf:
list: updates
register: packages
- name: Show packages to upgrade
debug:
msg: >-
{%- set output=[] -%}
{%- for p in packages.results -%}
{{ output.append('%-40s' % (p.name ~ '-' ~ p.version) ~ ' | repo: ' ~ p.repo) }}
{%- endfor -%}
{{ output }}
Which gives running against my just downloaded fedora:latest
image:
PLAY [all] *******************************************************************************************************************************
TASK [Check packages to upgrade] *********************************************************************************************************
ok: [testfed]
TASK [Show packages to upgrade] **********************************************************************************************************
ok: [testfed] => {
"msg": [
"authselect-1.4.0 | repo: updates",
"authselect-libs-1.4.0 | repo: updates",
"ca-certificates-2022.2.54 | repo: updates",
"coreutils-9.0 | repo: updates",
"coreutils-common-9.0 | repo: updates",
"crypto-policies-20220428 | repo: updates",
"curl-7.82.0 | repo: updates",
"dnf-4.13.0 | repo: updates",
"dnf-data-4.13.0 | repo: updates",
"elfutils-default-yama-scope-0.187 | repo: updates",
"elfutils-libelf-0.187 | repo: updates",
"elfutils-libs-0.187 | repo: updates",
"fedora-release-common-36 | repo: updates",
"fedora-release-container-36 | repo: updates",
"fedora-release-identity-container-36 | repo: updates",
"filesystem-3.18 | repo: updates",
"glib2-2.72.3 | repo: updates",
"glibc-2.35 | repo: updates",
"glibc-common-2.35 | repo: updates",
"glibc-minimal-langpack-2.35 | repo: updates",
"gnupg2-2.3.7 | repo: updates",
"gnutls-3.7.7 | repo: updates",
"krb5-libs-1.19.2 | repo: updates",
"libarchive-3.5.3 | repo: updates",
"libblkid-2.38 | repo: updates",
"libcap-ng-0.8.3 | repo: updates",
"libcurl-7.82.0 | repo: updates",
"libdnf-0.67.0 | repo: updates",
"libgcc-12.1.1 | repo: updates",
"libgcrypt-1.10.1 | repo: updates",
"libgomp-12.1.1 | repo: updates",
"libgpg-error-1.45 | repo: updates",
"libidn2-2.3.3 | repo: updates",
"libmount-2.38 | repo: updates",
"librepo-1.14.3 | repo: updates",
"libsmartcols-2.38 | repo: updates",
"libsolv-0.7.22 | repo: updates",
"libstdc++-12.1.1 | repo: updates",
"libtirpc-1.3.2 | repo: updates",
"libuuid-2.38 | repo: updates",
"libxml2-2.9.14 | repo: updates",
"libzstd-1.5.2 | repo: updates",
"lua-libs-5.4.4 | repo: updates",
"nettle-3.8 | repo: updates",
"openldap-2.6.2 | repo: updates",
"openldap-compat-2.6.2 | repo: updates",
"openssl-libs-3.0.5 | repo: updates",
"pam-1.5.2 | repo: updates",
"pam-libs-1.5.2 | repo: updates",
"pcre2-10.40 | repo: updates",
"pcre2-syntax-10.40 | repo: updates",
"python3-3.10.5 | repo: updates",
"python3-dnf-4.13.0 | repo: updates",
"python3-hawkey-0.67.0 | repo: updates",
"python3-libdnf-0.67.0 | repo: updates",
"python3-libs-3.10.5 | repo: updates",
"python3-rpm-4.17.1 | repo: updates",
"rpm-4.17.1 | repo: updates",
"rpm-build-libs-4.17.1 | repo: updates",
"rpm-libs-4.17.1 | repo: updates",
"rpm-sign-libs-4.17.1 | repo: updates",
"setup-2.14.1 | repo: updates",
"systemd-libs-250.8 | repo: updates",
"tpm2-tss-3.2.0 | repo: updates",
"tzdata-2022a | repo: updates",
"util-linux-core-2.38 | repo: updates",
"vim-data-9.0.137 | repo: updates",
"vim-minimal-9.0.137 | repo: updates",
"yum-4.13.0 | repo: updates",
"zchunk-libs-1.2.2 | repo: updates"
]
}
}
PLAY RECAP *******************************************************************************************************************************
testfed : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0