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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/382504686.709476.1686061067008%40office.mailbox.org.

Reply via email to