# Check if a software package is installed

**URL:** <https://forum.ansible.com/t/check-if-a-software-package-is-installed/21656>\
**Category:** Ansible Project\
**Created:** [June 3, 2016, 9:06am UTC](https://forum.ansible.com/t/check-if-a-software-package-is-installed/21656 "2016-06-03T09:06:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [June 3, 2016, 9:06am UTC](https://forum.ansible.com/t/check-if-a-software-package-is-installed/21656/1 "2016-06-03T09:06:54Z")

</div>

Hello everyone,

I am quite new to Ansible, and I think it is much easier to use than e.  
g. Puppet.

I have to make sure that software package A is installed on a host, if  
software package B is not.

Since I use this role on SLES: is there any chance to use e.g. the  
zypper modukle for that purpose?

Currently, I invoke a command to check if B is installed, and if it is  
not, I call the zypper module to make sure A is installed:

=== snip ===  
- name: query rubygem-facter  
&nbsp;&nbsp;command: rpm -q rubygem2.1-rubygem-facter  
&nbsp;&nbsp;ignore\_errors: true  
&nbsp;&nbsp;register: ch\_rpm2  
&nbsp;&nbsp;failed\_when: ch\_rpm2.rc \> 1  
&nbsp;&nbsp;changed\_when: false

- name: install facter  
&nbsp;&nbsp;zypper: name=facter state=present  
&nbsp;&nbsp;when: ch\_rpm2|failed  
=== pins ===

Is there any more ansible-style way to get this done?

I can't simply install facter, since it is an older version. The attempt  
to install facter on a host where rubygem2.1-rubygem-facter is already  
installed causes an error, and I do not like avoidable errors in my  
roles, they are often misleading 😉  
OTOH, not each system can fulfill the dependencies for  
rubygem2.1-rubygem-facter - like Ruby 2.1. SLES 11 does not have this as  
standard, and its annoying to add it here, because it almost always  
causes troubles with the ruby-based software on the host.

Regards,  
Werner

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [June 3, 2016, 4:47pm UTC](https://forum.ansible.com/t/check-if-a-software-package-is-installed/21656/2 "2016-06-03T16:47:42Z")

</div>

> Is there any more ansible-style way to get this done?

I would be interested in this, too.

> OTOH, not each system can fulfill the dependencies for  
> rubygem2.1-rubygem-facter - like Ruby 2.1. SLES 11 does not have this as  
> standard, and its annoying to add it here, because it almost always  
> causes troubles with the ruby-based software on the host.

If this is somehow OS-version related (SLE11 no, SLE12 yes, ...), you  
could simply use a group\_vars file with a variable, and use that to  
check and/or install:

group\_vars/SLE11.yml:

facter\_package: facter

group\_vars/SLE12.yml:

facter\_package: rubygem2.1-rubygem-facter

Role/Playbook/Task/...:

- name: install whatever fits  
&nbsp;&nbsp;zypper: name="{{facter\_package }}" state=present

Johannes
