debianansibleidempotent

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?


Solution

  • 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 }}"