Difficult task if missing Apt package

I want to perform a number of tasks if a specific apt package is missing.

eg:

if carbon graphite is NOT installed:

- 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:

 - 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 do I start hacking this?

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

+7
apt conditional-statements ansible ansible-playbook
source share
3 answers

It looks like my solution works.

This is an example of how I work:

 - shell: dpkg-query -W 'statsd' ignore_errors: True register: is_statd - name: create build dir file: path=/tmp/build state=directory when: is_statd|failed - name: install dev packages for statd build apt: name={{ item }} with_items: - git - devscripts - debhelper when: is_statd|failed - shell: cd /tmp/build ; git clone https://github.com/etsy/statsd.git ; cd statsd ; dpkg-buildpackage when: is_statd|failed .... 

Here is another example:

  - name: test if create_superuser.sh exists stat: path=/tmp/create_superuser.sh ignore_errors: True register: f - name: create graphite superuser command: /tmp/create_superuser.sh when: f.stat.exists == True 

... and one more

 - stat: path=/tmp/build ignore_errors: True register: build_dir - name: destroy build dir shell: rm -fvR /tmp/build when: build_dir.stat.isdir is defined and build_dir.stat.isdir 
+6
source share

I think you are on the right track with dpkg | grep dpkg | grep , only if the return code is 0 . But you can just check the output.

 - shell: dpkg-query -l '<package name>' register: dpkg_result - do_something: when: dpkg_result.stdout != "" 
+2
source share

I was a bit late for this party, but here is another example that uses exit codes - make sure that you explicitly match the desired status text with the results of the dpkg request:

 - name: Check if SystemD is installed command: dpkg-query -s systemd | grep 'install ok installed' register: dpkg_check tags: ntp - name: Update repositories cache & install SystemD if it is not installed apt: name: systemd update_cache: yes when: dpkg_check.rc == 1 tags: ntp 
+1
source share

All Articles