I don't think the yum module would help in this case. It currently has 3 states: absent, present, and latest. Since it sounds like you don't want to actually install or remove the package (at least at this point) then you would need to do this in two manual steps. The first task would check to see if the package exists, then the second task would invoke a command based on the output of the first command.

If you use "rpm -q" to check if a package exists then the output would look like this for a package that exists:

# rpm -q httpd
httpd-2.2.15-15.el6.centos.1.x86_64

and like this if the package doesn't exist:

# rpm -q httpdfoo
package httpdfoo is not installed

So your ansible tasks would look something like this:

- name: Check if foo.rpm is installed
  command: rpm -q foo.rpm
  register: rpm_check

- name: Execute script if foo.rpm is not installed
  command: somescript
  when: rpm_check.stdout.find('is not installed') != -1

The rpm command will also exit with a 0 if the package exists, or a 1 if the package isn't found, so another possibility is to use:

when: rpm_check.rc == 1
Answer from Bruce P on Stack Overflow
🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › ansible › builtin › package_facts_module.html
ansible.builtin.package_facts module – Package information as facts — Ansible Community Documentation
Return information about installed packages as facts. The below requirements are needed on the host that executes this module. See details per package manager in the manager option. - name: Gather the package facts ansible.builtin.package_facts: manager: auto - name: Print the package facts ansible.builtin.debug: var: ansible_facts.packages - name: Check whether a package called foobar is installed ansible.builtin.debug: msg: "{{ ansible_facts.packages['foobar'] | length }} versions of foobar are installed!" when: "'foobar' in ansible_facts.packages"
🌐
Reddit
reddit.com › r/ansible › check if package is installed
r/ansible on Reddit: Check if package is installed
June 5, 2023 -

What is the best way to have ansible check if cabon black package had been installed and if not run to few steps. Install dependencies instal lcabon black and activate it. Finaly star and enable service. This for linux os

Discussions

How to make Ansible execute a shell script if a package is not installed - Stack Overflow
He/she doesn't state that they want to actually install anything. Cheers. 2017-12-06T22:21:46.13Z+00:00 ... And add changed_when: False if you want this to be idempotent. 2018-02-15T15:49:55.907Z+00:00 ... If the package is installable through the system package manager (yum, apt, etc) itself, then you can make use of the check mode flag of ansible ... More on stackoverflow.com
🌐 stackoverflow.com
ansible - How can I check for an installed packaged version on multiple linux hosts? - DevOps Stack Exchange
I searched all day for different options without luck. Attached is the output of ansible-playbook. Any hints guys? --- - hosts: centos become: true tasks: - name: Check Hostname command: /usr/bin/hostname - name: Check for package if is installed yum: list: snapd register: result More on devops.stackexchange.com
🌐 devops.stackexchange.com
Best way to check for installed yum package/rpm version in Ansible and use it - Stack Overflow
The yum module has a list argument that performs a repoquery on the specified package so you can find information about installed packages. This question is better answered stackoverflow.com/questions/41551620/… 2017-05-03T11:29:18.47Z+00:00 ... - name: Find if custom_rpm is installed yum: list: custom_rpm register: custom_rpm_yum_packages when: ansible... More on stackoverflow.com
🌐 stackoverflow.com
use ansible to check if application is installed - Ansible Project - Ansible
Hey everyone, writing a playbook to check if a package is installed on my windows system. if not run the installer from \Temp\software. and if already there, ignore. i have the command to run the installer but need to know how to check if the application is already installed name: install SCCM ... More on forum.ansible.com
🌐 forum.ansible.com
0
January 5, 2023
🌐
OneUptime
oneuptime.com › home › blog › how to use ansible package_facts to list installed packages
How to Use Ansible package_facts to List Installed Packages
February 21, 2026 - I have found this module incredibly useful in environments where you inherit servers with unknown configurations or need to enforce software compliance policies. The package_facts module populates the ansible_facts.packages dictionary with ...
🌐
DEV Community
dev.to › koh_sh › how-to-do-if-a-package-is-installed-do-something-with-ansible-3fhi
How to do "if a package is installed, do something" with Ansible - DEV Community
June 14, 2020 - tasks: - name: check if httpd is installed shell: rpm -qa | grep httpd register: httpd_installed ignore_errors: True check_mode: False changed_when: False - name: print debug: msg: "httpd is installed" when: httpd_installed.rc == 0 · But this is a little bit of trouble when you use it many times. Extra parameters like changed_when or ignore_errors should be set · Warnings may be thrown at running Playbook or ansible-lint
🌐
LabEx
labex.io › tutorials › ansible-how-to-verify-package-installation-using-ansible-apt-module-415811
How to verify package installation using Ansible Apt module | LabEx
The Ansible Apt module automatically handles package dependencies, ensuring that all required dependencies are installed. You can verify the installation of dependencies by inspecting the module's output.
🌐
DEV Community
dev.to › setevoy › ansible-check-if-a-package-installed-on-a-remote-system-4402
Ansible: check if a package is installed on a remote system - DEV Community
March 10, 2019 - $ cat is-package-installed.yml - name: Check to see if a package is installed hosts: "{{ hosts | default('localhost') }}" tasks: - name: Gather the packager facts package_facts: - name: Package status debug: msg: "{{ item }} {{ 'installed' if ...
🌐
Stack Exchange
devops.stackexchange.com › questions › 6404 › how-can-i-check-for-an-installed-packaged-version-on-multiple-linux-hosts
ansible - How can I check for an installed packaged version on multiple linux hosts? - DevOps Stack Exchange
tasks: - name: Gather the package facts ansible.builtin.package_facts: manager: auto - name: Check whether a package is installed ansible.builtin.debug: msg: "{{ ansible_facts.packages['google-chrome-stable'][0].version }}" when: "'google-c...
Find elsewhere
🌐
GitHub
gist.github.com › goldyfruit › f4f274be3144e6afca69
[ansible] Check via the yum module and a registered value if a package is installed or not · GitHub
- name: Check if rpm is already installed yum: list: my-rpm # If not installed yum_list.results[*].yumstate != installed register: yum_list - name: Conditionally do next thing debug: msg: "Not installed" when: yum_list.results | selectattr("yumstate", "match", "installed") | list | length == 0 ... - package_facts: {} - when: ansible_facts.packages["mysql57-community-release"] is undefined block: - yum: name: http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm state: present - yum: {name: mysql-server, state: present} - service: {name: mysqld, state: started, enabled: yes} - shell: grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'; register: result - ....
🌐
Ansible
forum.ansible.com › archives › ansible project
use ansible to check if application is installed - Ansible Project - Ansible
January 5, 2023 - Hey everyone, writing a playbook to check if a package is installed on my windows system. if not run the installer from \Temp\software. and if already there, ignore. i have the command to run the installer but need to know how to check if the application is already installed name: install SCCM win_package: path: c:\Temp\software\CLENT_2203\install.cmd state: present ingnor_errors: yes
🌐
Ansible
docs.ansible.com › ansible › 2.9 › modules › package_facts_module.html
package_facts – package information as facts — Ansible Documentation
The below requirements are needed on the host that executes this module. For ‘portage’ support it requires the qlist utility, which is part of ‘app-portage/portage-utils’. For Debian-based systems python-apt package must be installed on targeted hosts. - name: Gather the rpm package facts package_facts: manager: auto - name: Print the rpm package facts debug: var: ansible_facts.packages - name: Check whether a package called foobar is installed debug: msg: "{{ ansible_facts.packages['foobar'] | length }} versions of foobar are installed!" when: "'foobar' in ansible_facts.packages"
🌐
Wordpress
documentacoes.wordpress.com › 2021 › 12 › 22 › check-if-a-package-is-installed-with-ansible
check if a package is installed with ansible | documentacoes.wordpess.com
December 22, 2021 - Homecheck if a package is installed with ansible · 22/12/2021 nickollas Ansible · cat > check_package.yml <<EOF --- # check if a package is installed # ansible-playbook check_package.yml -e package=sssd-common -e target=all - name: 'set host' hosts: "{{ target }}" gather_facts: yes tasks: - name: "check if package is installed on Debian family" shell: dpkg -l "{{ package }}" changed_when: false register: package_check when: ansible_facts['os_family'] == "Debian" - name: "check if package is installed on Red Hat family" shell: rpm -q "{{ package }}" changed_when: false register: package_check when: ansible_facts['os_family'] == "RedHat" - name: "print execution results" debug: msg: "package is installed" when: package_check is succeeded EOF ·
🌐
Reddit
reddit.com › r/ansible › check if package is installed with package_facts
r/ansible on Reddit: check if package is installed with package_facts
December 4, 2018 -

Instead of using command to get the list of installed packages , I've stumbled upon the package_facts module , which adds the list of the installed packages to the host vars.

ok: [my.host.net] => changed=false 
  ansible_facts:
    packages:
      acl:
      - arch: amd64
        name: acl
        source: apt
        version: 2.2.52-2

...

      zabbix-agent:
      - arch: x86_64
        epoch: null
        name: zabbix-agent
        release: 1.el7
        source: rpm
        version: 3.4.9

Now I'm gonna trying to perform some tasks only on the hosts where the (zabbix-agent) is installed .

I've tried with :

  - name: debug
    debug:
      msg: "{{ ansible_hostname }}"
    when: packages.zabbix-agent is defined

but I'm getting :

 msg: |-
    The conditional check 'packages.zabbix-agent is defined' failed. The error was: error while evaluating conditional (packages.zabbix-agent is defined): 'dict object' has no attribute 'zabbix'

any clue?

🌐
LabEx
labex.io › questions › how-to-check-if-a-package-is-successfully-installedupdatedremoved-using-ansible-289651
How to Verify Package Status in Ansible | LabEx
September 19, 2024 - This diagram shows that after performing the package operation (installation, update, or removal), you should check the changed attribute of the task result. If it's true, the operation was successful, and you can print a success message. If it's false, no change was detected, and you can handle the situation accordingly. By following this approach, you can reliably check the status of package management tasks in your Ansible playbooks, ensuring that your systems are properly maintained and updated.
🌐
ComputingForGeeks
computingforgeeks.com › home › ansible check if software package is installed on linux
Ansible check if software package is installed on Linux
March 21, 2026 - servers #connection: local # When running locally vars: package_names: - vim tasks: - name: "Check if listed package is installed or not on Debian Linux family" command: dpkg-query -l "{{ item }}" loop: "{{ package_names }}" register: package_check ...
🌐
Reddit
reddit.com › r/ansible › ansible check if package is installed on windows
r/ansible on Reddit: ansible check if package is installed on windows
January 5, 2023 -

i have a list of 5 applications which i am writing a playbook to check if they are installed on my windows system. i want to start by checking if the applications are installed then ignore but if not, then run the install from \Temp\software. not sure what is the best way to use ansible and check if an application is installed on a windows system.

🌐
OneUptime
oneuptime.com › home › blog › how to use ansible package_facts module to get installed packages
How to Use Ansible package_facts Module to Get Installed Packages
February 21, 2026 - A common use case is verifying that required packages are present. # check-required-packages.yml # Verifies that required packages are installed --- - name: Verify required packages hosts: appservers become: yes vars: required_packages: - python3 - git - curl - openssl - nginx tasks: - name: Gather package facts ansible.builtin.package_facts: manager: auto - name: Check each required package ansible.builtin.debug: msg: "{{ item }}: {{ 'INSTALLED (' + ansible_facts['packages'][item][0]['version'] + ')' if item in ansible_facts['packages'] else 'MISSING' }}" loop: "{{ required_packages }}" - name: Fail if any required packages are missing ansible.builtin.fail: msg: "Missing required packages: {{ missing_packages | join(', ') }}" vars: missing_packages: "{{ required_packages | difference(ansible_facts['packages'].keys() | list) }}" when: missing_packages | length > 0