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"





-----Original Message-----
From: dulhaver via Ansible Project <[email protected]>
Sent: Tuesday, June 6, 2023 10:18 AM
To: [email protected]
Subject: [EXTERNAL] [ansible-project] creating a csv for a package existing or 
not existing on remotes



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



   sub1-rd1533.agrar.portal.local, python3-psycopg2 is missing

   sub1-rd5194.prod.eakte.rz-abc.local, python3-psycopg2 exists

   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

  hosts: "{{ targets | default ('postgres') }}"

  vars:

    package_names:

      - python3-psycopg2

  tasks:

    - name: "Check if listed package is installed or not"

      command: rpm -q "{{ item }}"

      loop: "{{ package_names }}"

      register: package_check



    - name: "Print success"

      debug:

        msg: "{{ package_names }} is installed"

      when: package_check is succeeded



    - name: "Print failure"

      debug:

        msg: "{{ package_names }} is not installed"

      when: package_check is failed

======================================================================





==== the output ======================================================



[me@sub1-rd1985 ad_hoc_playbooks]$ play get_package.yml -l @temp/limit



PLAY [check wheter packages exist] 
*************************************************************************************************************************************************************



TASK [Gathering Facts] 
*************************************************************************************************************************************************************************

ok: [sub1-rd1533.agrar.portal.local]

ok: [sub1-rd5194.prod.eakte.rz-abc.local]

ok: [vm-414001-0251.step.zrz.abc.local]



TASK [Check if listed package is installed or not] 
*********************************************************************************************************************************************

changed: [sub1-rd5194.prod.eakte.rz-abc.local] => (item=python3-psycopg2)

failed: [sub1-rd1533.agrar.portal.local] (item=python3-psycopg2) => 
{"ansible_loop_var": "item", "changed": true, "cmd": ["rpm", "-q", 
"python3-psycopg2"], "delta": "0:00:00.031946", "end": "2023-06-06 
16:05:12.622331", "item": "python3-psycopg2", "msg": "non-zero return code", 
"rc": 1, "start": "2023-06-06 16:05:12.590385", "stderr": "", "stderr_lines": 
[], "stdout": "package python3-psycopg2 is not installed", "stdout_lines": 
["package python3-psycopg2 is not installed"]}

changed: [vm-414001-0251.step.zrz.abc.local] => (item=python3-psycopg2)



TASK [Print success] 
***************************************************************************************************************************************************************************

ok: [sub1-rd5194.prod.eakte.rz-abc.local] => {

    "msg": "['python3-psycopg2'] is installed"

}

ok: [vm-414001-0251.step.zrz.abc.local] => {

    "msg": "['python3-psycopg2'] is installed"

}



TASK [Print failure] 
***************************************************************************************************************************************************************************

skipping: [sub1-rd5194.prod.eakte.rz-abc.local]

skipping: [vm-414001-0251.step.zrz.abc.local]



PLAY RECAP 
*************************************************************************************************************************************************************************************

sub1-rd1533.agrar.portal.local :ok=1 changed=0    unreachable=0failed=1 
skipped=0    rescued=0    ignored=0

sub1-rd5194.prod.eakte.rz-abc.local :ok=3changed=1 unreachable=0    
failed=0skipped=1 rescued=0    ignored=0

vm-414001-0251.step.zrz.abc.local :ok=3changed=1 unreachable=0    
failed=0skipped=1 rescued=0    ignored=0



=================================================================================================================================



--

You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.

To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.

To view this discussion on the web visit 
https://urldefense.com/v3/__https://groups.google.com/d/msgid/ansible-project/382504686.709476.1686061067008*40office.mailbox.org__;JQ!!Fto3Xw!tNskoVOu0CM9Khjn0WESCZQM9PqeutSnAW9PH2KIdIuGDL83qjjh7sQWjbUydtawNGL-XxIZ8YkZbpnmH66tgjkwnzDDiLk$<https://urldefense.com/v3/__https:/groups.google.com/d/msgid/ansible-project/382504686.709476.1686061067008*40office.mailbox.org__;JQ!!Fto3Xw!tNskoVOu0CM9Khjn0WESCZQM9PqeutSnAW9PH2KIdIuGDL83qjjh7sQWjbUydtawNGL-XxIZ8YkZbpnmH66tgjkwnzDDiLk$>
 .

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/PH0PR10MB559314C85CD8C05F6524341EF052A%40PH0PR10MB5593.namprd10.prod.outlook.com.

Reply via email to