0

I have the following Ansible code for checking available updates on a Rocky linux server and showing them to user:

- name: Check updates
  ansible.builtin.command:
    cmd: dnf check-update
  register: updates

- name: Show result
  ansible.builtin.debug:
    msg: '{{ updates }}'

If I run the same command directly in the target machine shell I get the expected result:

[user@host ~]$ dnf check-update
Last metadata expiration check: 0:19:37 ago on 2022-08-03.

bash.x86_64                                                    4.4.20-4.el8_6                                                      baseos   
conmon.x86_64                                                  2:2.1.2-2.module+el8.6.0+997+05c9d812                               appstream
container-selinux.noarch                                       2:2.188.0-1.module+el8.6.0+997+05c9d812                             appstream
containernetworking-plugins.x86_64                             1:1.1.1-3.module+el8.6.0+997+05c9d812                               appstream
...

But when running Ansible the package list is missing:

TASK [example : Show result] *********************************************************************************
ok: [127.0.0.1] => 
  msg:
    changed: true
    cmd: dnf check-update
    delta: '0:00:01.438742'
    end: '2022-08-03 12:02:39.027193'
    failed: false
    rc: 0
    start: '2022-08-03 12:02:37.588451'
    stderr: ''
    stderr_lines: []
    stdout: 'Last metadata expiration check: 0:13:15 ago on 2022-08-03.'
stdout_lines:
- 'Last metadata expiration check: 0:13:15 ago on 2022-08-03.'

Verbose mode wont show package list either.

I am using Ansible 2.11.1. Same behaviour is also on 2.13.2. I tried using the shell module instead of the command module but it made no difference. How can I make available update list visible during Ansible run?

Zeitounator
  • 1,090
  • 4
  • 12
Madoc Comadrin
  • 540
  • 3
  • 11
  • 28

1 Answers1

2

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   
Zeitounator
  • 1,090
  • 4
  • 12
  • This solution works well for me. Can you explain what `%-40s` does in Jinja2 filter? – Madoc Comadrin Aug 08 '22 at 10:52
  • 1
    `%` is the Python string format operator (i.e. ` % `). The format I used here asks to right padd the value with spaces up to 40 characters. For an example see: https://pyformat.info/#string_pad_align – Zeitounator Aug 08 '22 at 12:19