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.comSince 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.
If you only want to update a subset of the packages with available updates you might want to try @wurtel s attempt. You will need to register the installed packages like this:
- name: Get installed packages.
command: rpm -qa --qf "%{NAME}\n"
register: installed_packages
Then you can define a set theory filter and update all the packages defined in the list of packages which are allowed to update packages_to_update.
- name: Update subset of packages.
yum:
name: "{{ item }}"
state: latest
with_items:
- {{ installed_packages | intersect(packages_to_update) }}
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
Based on the Bruce P answer above, a similar approach for apt/deb files is
- name: Check if foo is installed
command: dpkg-query -l foo
register: deb_check
- name: Execute script if foo is not installed
command: somescript
when: deb_check.stdout.find('no packages found') != -1
yum module refuses to install package, saying it's already installed
php - Unable to install package in Ansible - Stack Overflow
yum state=latest doesn't install if not already installed
Check via yum module if a particular package is installed
Can I disable a repository when using the yum module?
How do I handle failed installations with the yum module?
Is there a way to use yum with custom GPG keys?
I need a playbook that checks to see if PHP is installed.
So, the equivalent of yum list installed | grep php in Linux.