DEV Community

Discussion on: Ansible: check if a package is installed on a remote system

Collapse
 
pfuntner profile image
John Pfuntner • Edited

Nice! I came up with a playbook that has a single debug task:

$ cat is-package-installed.yml
- name: Check to see if a package is installed
  hosts: "{{ hosts | default('localhost') }}"
  tasks:

  - name: Gather the packager facts
    package_facts:

  - name: Package status
    debug:
      msg: "{{ item }} {{ 'installed' if item in ansible_facts.packages else 'not installed' }}"
    loop: "{{ pkgs | default([]) }}"
$ ansible-playbook -e '{ "pkgs": ["foo", "zip"] }' is-package-installed.yml

PLAY [Check to see if a package is installed] ***********************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Gather the packager facts] ************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Package status] ***********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=foo) => {
    "msg": "foo not installed"
}
ok: [localhost] => (item=zip) => {
    "msg": "zip installed"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$
Collapse
 
pfuntner profile image
John Pfuntner

Be aware of this requirement for the the package_facts module:

For Debian-based systems python-apt package must be installed on targeted hosts.

Otherwise, ansible_facts.packages is an empty dictionary. Reminder: Ubuntu is Debian-based.