Since ansible 2.5 there is an option update_only for yum (and since ansible 2.1 only_upgrade for apt) which installs latest version only if it was already installed on the system. So, instead of collecting a list of packages in another task, you can add the option.

- name: Update subset of packages.
  yum:
    name: "{{ item }}"
    state: latest
    update_only: yes
  with_items:
    - package1
    - package2

I am myself was searching the web and this article was found before I got to the official documentation. So I think it worse to be added here.

Answer from Dennis M. on serverfault.com
Discussions

yum module refuses to install package, saying it's already installed
I don't know if that would work on 1.9.1 or not. Details: Ansible version 1.9.2, target OS RedHat 6.0, internal repositories that mirror official ones. I don't know how they are set up exactly. The playbook, which is in roles/smbclient/tasks/main.yml, contains just: --- - name: "package" yum: name=samba-client state=present... More on github.com
🌐 github.com
49
November 26, 2015
php - Unable to install package in Ansible - Stack Overflow
Does Ansible's yum module install dependencies as well? ... So where does the claim "However, this package hasn't been installed on the server at all." come from? ... Save this answer. Show activity on this post. I suspect your issue is related to the difference in state: present vs state: latest. It looks like the yum module with state: present checks the rpmdb to see if the pkg exists and does nothing ... More on stackoverflow.com
🌐 stackoverflow.com
yum state=latest doesn't install if not already installed
If I try to install a package which is not already installed using yum with state=latest: ansible -m yum -a 'pkg=hadoop-hdfs-namenode state=latest' box -Ksu$USER It does a yum update, which (stupid... More on github.com
🌐 github.com
9
April 27, 2012
Check via yum module if a particular package is installed
... gather_facts: true ... tasks: - name: "Check if PHP is installed" package_facts: manager: "auto" - name: "PHP is installed check" debug: msg: "PHP is installed" when: "'php' is in ansible_facts.packages" - name: "PHP is not installed check" debug: msg: "PHP is not installed" when: "'php' not in ansible_facts.packages" More on reddit.com
🌐 r/ansible
9
2
May 20, 2020
People also ask

Can I disable a repository when using the yum module?
Yes, you can use the disablerepo parameter to disable a repository during package installation.
🌐
linuxbuz.com
linuxbuz.com › devops › ansible-yum-module-examples
Ansible yum module: Install RHEL/CentOS Packages
How do I handle failed installations with the yum module?
You can use the ignore_errors: yes parameter in your task to continue playbook execution even if the yum module fails for a package installation.
🌐
linuxbuz.com
linuxbuz.com › devops › ansible-yum-module-examples
Ansible yum module: Install RHEL/CentOS Packages
Is there a way to use yum with custom GPG keys?
Yes, use the disable_gpg_check parameter to skip GPG checks.
🌐
linuxbuz.com
linuxbuz.com › devops › ansible-yum-module-examples
Ansible yum module: Install RHEL/CentOS Packages
🌐
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 "{{ package }}" is installed yum: list="{{ package }}" register: is_installed - name: install "{{ package }}" if not exist yum: name: "{{ package }}" state: latest when: (is_installed.results|length == 1) or (is_install...
🌐
Spacelift
spacelift.io › blog › ansible-yum-module
Ansible Yum Module : Installing & Removing Packages
- name: Check if multiple packages are installed debug: msg: "{{ item }} is already installed" loop: - httpd - vim - git when: item in ansible_facts.packages · Conditionals allow you to take action based on the outcome of a conditional statement. In the example below, the yum module will install nginx only if it is not already installed on the machine: - name: Install nginx only if is not installed yum: name: nginx state: present when: "'nginx' not in ansible_facts.packages"
Published   October 17, 2025
🌐
GitHub
github.com › ansible › ansible-modules-core › issues › 2559
yum module refuses to install package, saying it's already installed · Issue #2559 · ansible/ansible-modules-core
November 26, 2015 - I don't know if that would work on 1.9.1 or not. Details: Ansible version 1.9.2, target OS RedHat 6.0, internal repositories that mirror official ones. I don't know how they are set up exactly. The playbook, which is in roles/smbclient/tasks/main.yml, contains just: --- - name: "package" yum: name=samba-client state=present...
Author   ansible
🌐
DEV Community
dev.to › spacelift › ansible-yum-module-installing-removing-packages-4g04
Ansible Yum Module : Installing & Removing Packages - DEV Community
December 9, 2024 - In the example below, we use a conditional statement to check if the file exists in the directory before proceeding with the package installation: - name: Install custom-software if config file exists yum: name: custom-software state: present when: ansible.builtin.stat(path='/etc/custom-config.conf').stat.exists
Find elsewhere
🌐
LinuxBuz
linuxbuz.com › devops › ansible-yum-module-examples
Ansible yum module: Install RHEL/CentOS Packages
July 11, 2025 - --- - name: Install Elasticsearch ... playbook configures all target hosts to use the Elasticsearch YUM repository by adding its URL and GPG key....
🌐
Toptechskills
toptechskills.com › ansible-tutorials-courses › ansible-yum-module-tutorial-examples
Ansible yum Module Tutorial + Examples | TopTechSkills.com
May 18, 2024 - - name: ensure wget and ruby are installed yum: name: - wget - ruby state: present update_cache: true become: true · Set state: absent to remove a package. I suggest also setting autoremove: true to remove any dependencies that were installed originally, but are no longer required. - name: ensure wget is not installed yum: name: wget state: absent autoremove: true become: true
🌐
Linux Handbook
linuxhandbook.com › ansible-yum-module
Ansible Yum Module: Manage Red Hat Packages
July 21, 2025 - This is useful when installing custom software or vendor packages not found in public repositories. - name: Install a local RPM ansible.builtin.yum: name: /tmp/package-name.rpm state: present · If the RPM has unmet dependencies, yum will attempt to resolve them using the system’s configured repositories.
🌐
Ansible
docs.ansible.com › ansible › 2.9 › modules › yum_module.html
yum – Manages packages with the yum package manager — Ansible Documentation
- name: install the latest version ... state: present - name: Download the nginx package but do not install it yum: name: - nginx state: latest download_only: true · This module is guaranteed to have backward compatible interface changes going forward. [stableinterface] This module is maintained by the Ansible Core Team. [core] More information about Red Hat’s support of this module is available from this Red Hat Knowledge Base article. ... If you notice ...
🌐
Ansible
docs.ansible.com › ansible › latest › collections › ansible › builtin › yum_module.html
ansible.builtin.yum module — Ansible Community Documentation
This redirect is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name yum even without specifying the collections keyword.
🌐
GitHub
github.com › ansible › ansible › issues › 269
yum state=latest doesn't install if not already installed · Issue #269 · ansible/ansible
April 27, 2012 - Does contain only the package I'm trying to install. But then the logic goes · if not updates: cmd = "yum -c %s -d1 -y install '%s'" % (yumconf, pkgspec) else: cmd = "yum -c %s -d1 -y update '%s'" % (yumconf, pkgspec)
Author   ansible
🌐
Ansible
docs.ansible.com › ansible › 2.3 › yum_module.html
Ansible Documentation — Ansible Community Documentation
June 29, 2022 - Ansible getting started · Installation, Upgrade & Configuration
🌐
Ansible
docs.ansible.com › ansible › 9 › collections › ansible › builtin › yum_module.html
ansible.builtin.yum module – Manages packages with the yum package manager — Ansible Community Documentation
December 3, 2024 - The yum module does not support clearing yum cache in an idempotent way, so it was decided not to implement it, the only method is to use command and call the yum command directly, namely “command: yum clean all” https://github.com/ansible/ansible/pull/31450#issuecomment-352889579 · - name: Install the latest version of Apache ansible.builtin.yum: name: httpd state: latest - name: Install Apache >= 2.4 ansible.builtin.yum: name: httpd>=2.4 state: present - name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning) ansible.builtin.yum: name: - nginx - postgre
🌐
Readthedocs
ansible-tips-and-tricks.readthedocs.io › en › latest › os-dependent-tasks › installing_packages
Installing Software and Other Packages - Ansible Tips and Tricks
- name: install basic packages action: > {{ ansible_pkg_mgr }} name={{ item }} state=present update_cache=yes with_items: - vim - tmux - mosh · Please note that the modules for apt and yum have different options that make it impossible to use the method above of using the action: In order to do this, one must use the when: to perform their associated module per OS. yum does not have a update module option, so it basically check to see if ...
🌐
4sysops
4sysops.com › home › blog › articles › ansible yum module: install rhel/centos packages
Ansible yum module: Install RHEL/CentOS packages – 4sysops
August 30, 2023 - yum defines the name of the package manager module. name=vim is the name of the package you want to install · state=present will install the package if it is not installed · update_cache=true will update the package cache if it is out of date ...