# creating a csv for a package existing or not existing on remotes

**URL:** <https://forum.ansible.com/t/creating-a-csv-for-a-package-existing-or-not-existing-on-remotes/38067>\
**Category:** Ansible Project\
**Created:** [June 6, 2023, 2:17pm UTC](https://forum.ansible.com/t/creating-a-csv-for-a-package-existing-or-not-existing-on-remotes/38067 "2023-06-06T14:17:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [June 6, 2023, 2:17pm UTC](https://forum.ansible.com/t/creating-a-csv-for-a-package-existing-or-not-existing-on-remotes/38067/1 "2023-06-06T14:17:47Z")

</div>

I want to check whether a certain package is installed on a coupld of servers and create a csv-file from it

&nbsp;&nbsp;&nbsp;sub1-rd1533.agrar.portal.local, python3-psycopg2 is missing  
&nbsp;&nbsp;&nbsp;sub1-rd5194.prod.eakte.rz-abc.local, python3-psycopg2 exists  
&nbsp;&nbsp;&nbsp;vm-414001-0251.step.zrz.abc.local, python3-psycopg2 exists

I though of using 'rpm -q \<package\_name\>' and based on it's return code 0 or 1 print the corresponding message into my csv.

However the rpm -q \<package\_name\> tasks simply fails on targets who do not have the package installed rather then registering the rc 1 and making it avaiable further down the playbook.

So I guess I am following the wrong approach. Can anybody kindly push be into the right direction?

==== THE Playbook =================================================

- name: check wheter packages exist  
&nbsp;&nbsp;hosts: "{{ targets | default ('postgres') }}"  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;package\_names:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- python3-psycopg2  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: "Check if listed package is installed or not"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: rpm -q "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ package\_names }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: package\_check

&nbsp;&nbsp;&nbsp;&nbsp;- name: "Print success"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ package\_names }} is installed"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: package\_check is succeeded

&nbsp;&nbsp;&nbsp;&nbsp;- name: "Print failure"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ package\_names }} is not installed"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: package\_check is failed

---

<div class="post-metadata">

**Author:** ![shearn](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shearn](https://forum.ansible.com/u/shearn)\
**Post date:** [June 6, 2023, 2:23pm UTC](https://forum.ansible.com/t/creating-a-csv-for-a-package-existing-or-not-existing-on-remotes/38067/2 "2023-06-06T14:23:19Z")

</div>

You can use package facts.

- name: Gather the rpm package\_facts

package\_facts:

manager: auto

- name: do something if package not installed

debug:

msg: ‘hey package is not installed’

when: “‘‘MyPackage’’ not in ansible\_facts.packages”

- name: do something if package is installed

debug:

msg: ‘Hey package is installed’

when: “‘‘MyPackage’’ in ansible\_facts.packages”

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [June 6, 2023, 2:43pm UTC](https://forum.ansible.com/t/creating-a-csv-for-a-package-existing-or-not-existing-on-remotes/38067/3 "2023-06-06T14:43:36Z")

</div>

You can use package facts.

- name: Gather the rpm package\_facts

package\_facts:

manager: auto

- name: do something if package not installed

debug:

msg: ‘hey package is not installed’

when: “‘‘MyPackage’’ not in ansible\_facts.packages”

- name: do something if package is installed

debug:

msg: ‘Hey package is installed’

when: “‘‘MyPackage’’ in ansible\_facts.packages”
