Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Do Task If Apt Package Is Missing

I'm looking to do a series of tasks if a specific apt package is missing.

for example:

if graphite-carbon is NOT installed do:

- apt: name=debconf-utils state=present
- shell: echo 'graphite-carbon/postrm_remove_databases boolean false' | debconf-set-selections
- apt: name=debconf-utils state=absent

another example:

if statsd is NOT installed do:

- file: path=/tmp/build state=directory
- shell: cd /tmp/build ; git clone https://github.com/etsy/statsd.git ; cd statsd ; dpkg-buildpackage 
- shell: dpkg -i /tmp/build/statsd*.deb

How would I begin to crack this?

I'm thinking maybe I can do a -shell: dpkg -l|grep <package name> and capture the return code somehow.

like image 537
Simply Seth Avatar asked Feb 26 '15 22:02

Simply Seth


1 Answers

You can use the package_facts module (requires Ansible 2.5):

- name: Gather package facts
  package_facts:
    manager: apt

- name: Install debconf-utils if graphite-carbon is absent
  apt:
    name: debconf-utils
    state: present
  when: '"graphite-carbon" not in ansible_facts.packages'

...
like image 121
sduthil Avatar answered Sep 23 '22 05:09

sduthil