3

I am trying to list all installed packages on my Debian 7 (Wheezy), 8 (Jessie), and 9 (Stretch) machines. There are easy ways dealing with it using APT or dpkg, but I could not find a proper way to do this with Ansible out of the box.

Is there a nice and smooth way to do this?

For RHEL machines I found this post: How can I get the installed YUM packages with Ansible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nicowde
  • 33
  • 1
  • 4

2 Answers2

6

Since Ansible 2.5 you can use the package_facts module: ansible.builtin.package_facts module – Package information as facts

- name: Gather package facts
  package_facts:
    manager: auto

- name: Debug if package is present
  debug:
    msg: 'yes, mypackage is present'
  when: '"mypackage" in ansible_facts.packages'

- name: Debug if package is absent
  debug:
    msg: 'no, mypackage is absent'
  when: '"mypackage" not in ansible_facts.packages'

Mind you, you need the ansible-apt module on Debian for that (which is kindly provided by bootstrap).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vkersten
  • 131
  • 1
  • 5
2

It doesn't look like Ansible provides any modules that would support this. You'll have to use shell or command.

- name: Get packages
  shell: dpkg-query -f '${binary:Package}\n' -W
  register: packages

- name: Print packages
  debug:
    msg: "{{ packages.stdout_lines }}" 
kfreezy
  • 1,499
  • 12
  • 16