# Is there any better way to find if a list of packes installed

**URL:** <https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423>\
**Category:** Ansible Project\
**Created:** [May 9, 2017, 3:05am UTC](https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423 "2017-05-09T03:05:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiran\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Kiran\_Kumar1](https://forum.ansible.com/u/Kiran_Kumar1)\
**Post date:** [May 9, 2017, 3:05am UTC](https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423/1 "2017-05-09T03:05:30Z")

</div>

Hi Friends,

Is there any better way to find if a list of packes is installed or not on a linux machine. It needs to return something may be a single variable to decide if all packages are installed or any one of them missed.

Assuming i need to check if below packages are installed or not and it should return true if all the listed packages are installed and false if anything is missed.

```auto
  - name: Check if packages are installed
    command: rpm -q {{ item }}
    with_items:
       - kernel
       - perf

```

```auto
       - perl

```

```auto
       - python

```

```auto
    

```

---

<div class="post-metadata">

**Author:** ![Tai\_Kedzierski](https://avatars.discourse-cdn.com/v4/letter/t/b77776/32.png) [@Tai\_Kedzierski](https://forum.ansible.com/u/Tai_Kedzierski)\
**Post date:** [May 9, 2017, 3:00pm UTC](https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423/2 "2017-05-09T15:00:51Z")

</div>

Hello Kiran

As I understand it, the way Ansible is intended to be used, you should not need this ?

If you write a task specifying packageX , and specify a state of installed/uninstalled/latest then that state is enforced… no prior need to check.

If you want then to perform an action only if a package is present, it begs the question as to why you do not know already - since you are using Ansible to maintain the server in a known state ?

Could you describe what it is you are trying to enable by doing a package check ?

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Kiran\_Kumar1](https://forum.ansible.com/u/Kiran_Kumar1)\
**Post date:** [May 9, 2017, 7:09pm UTC](https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423/3 "2017-05-09T19:09:20Z")

</div>

Hi Tai Kedzierski ,

Thanks for your reply. Am intending only to validate if packages are installed or not and am not installing any packages as part of other task.  
Am building a validation playbook that would check few things and packages is one of them.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 9, 2017, 10:27pm UTC](https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423/4 "2017-05-09T22:27:47Z")

</div>

You'd probably get what you want from an Ansible dummy run then, just  
pass '--check' to ansible-playbook.

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Kiran\_Kumar1](https://forum.ansible.com/u/Kiran_Kumar1)\
**Post date:** [May 19, 2017, 12:45am UTC](https://forum.ansible.com/t/is-there-any-better-way-to-find-if-a-list-of-packes-installed/24423/5 "2017-05-19T00:45:50Z")

</div>

I just figured it out. If a package is not installed it would return fail and if all packages are installed it would return pass

```auto
  vars:
    pkg: '0'
  tasks:
  - name: Check if packages are installed
    command: rpm -q {{ item }}
    with_items:
       - kernel
       - perf
    register: rpm_check
    failed_when: rpm_check.rc > 1

  - set_fact: pkg=1
    when: item.rc == 1
    with_items: "{{ rpm_check.results }}"
    failed_when: pkg != '1'

  - set_fact: pkg_test=fail
    when: pkg == '1'

  - set_fact: pkg_test=pass
    when: pkg == '0'
  - debug: var=pkg_test

```
