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

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.

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

    

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 ?

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.

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

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

  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