1

I want to get all installed and available packages using Ansible yum module. I tried below snippet but it return empty result.

- name: print
  yum:
    list: installed,available
  register: pkg
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Sam
  • 97
  • 1
  • 10
  • Maybe you can follow this link "[How to get the installed yum packages with ansible?](https://stackoverflow.com/questions/41551620/how-to-get-the-installed-yum-packages-with-ansible)" – Fuad Zein Apr 14 '22 at 07:02
  • I am able to get the list if i use either installed or available. My requirement is I should be able to use both option together :) – Sam Apr 14 '22 at 07:25

1 Answers1

0

The list parameter is a string, not a list, as pointed by the documentation, so, if you pass something else than the reserved words — installed, updates, available or repos — it will be considered as a packaged and will probably return your nothing.

We can also confirm this by Ansible code source, where the list value is considered in an if .. elif .. else:

if stuff == 'installed':
    return [ pkg_to_dict(p) for p in sorted(is_installed(module, repoq, '-a', conf_file, qf=is_installed_qf)) if p.strip() ]
elif stuff == 'updates':
    return [ pkg_to_dict(p) for p in sorted(is_update(module, repoq, '-a', conf_file, qf=qf)) if p.strip() ]
elif stuff == 'available':
    return [ pkg_to_dict(p) for p in sorted(is_available(module, repoq, '-a', conf_file, qf=qf)) if p.strip() ]
elif stuff == 'repos':
    return [ dict(repoid=name, state='enabled') for name in sorted(repolist(module, repoq)) if name.strip() ]
else:
    return [ pkg_to_dict(p) for p in sorted(is_installed(module, repoq, stuff, conf_file, qf=is_installed_qf) + is_available(module, repoq, stuff, conf_file, qf=qf)) if p.strip() ]

Source: https://github.com/ansible/ansible-modules-core/blob/00911a75ad6635834b6d28eef41f197b2f73c381/packaging/os/yum.py#L582

You should be able to make a combined list of all of them using list concatenation and two yum tasks:

- yum:
    list: installed
  register: installed_pkg

- yum:
    list: available
  register: available_pkg

- debug:
    var: installed_pkg.results + available_pkg
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83