问题
I am trying to list all installed packages on my Debian 7/8/9 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 to get the installed yum packages with Ansible?
回答1:
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 }}"
回答2:
Since ansible 2.5 you can use the package_facts module: https://docs.ansible.com/ansible/latest/modules/package_facts_module.html
- 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 https://galaxy.ansible.com/robertdebock/bootstrap).
来源:https://stackoverflow.com/questions/45434449/how-to-get-the-installed-apt-packages-with-ansible