Ansible is a very simple, clean and elegant tool to provision your production or development machines. It can be also used to setup and configure your development desktop, for example from a plain Ubuntu 14.04 LTS installation. For this task, we will soon run into the need to check, download and install applications as .deb packages because there is no ppa repository available (e.g. for dropbox, vagrant, etc.). In this blogpost, I will discribe how I solved this problem using Ansible.

## Checking if .deb Package is already installed

We want to install the package my_package - so first, we need to make sure that it is not yet already installed. We will use the bash command dpkg-query -W my_package to check for the availability of the package. We also want to register the output of this command - which can be done with the register statement in Ansible - in order to use the output as a conditional statement for other tasks.

In the above code, I added the failed_when statement, to not let the command fail when it exits with an error code 1 - which means the package was not found. Additionally, we set the changed_when statement, to return the state changed only when the package is not found.