Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the installed apt packages with Ansible?

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?

like image 376
nicowde Avatar asked Oct 18 '25 13:10

nicowde


1 Answers

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).

like image 50
vkersten Avatar answered Oct 21 '25 03:10

vkersten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!