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 OverflowWhat 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
How to make Ansible execute a shell script if a package is not installed - Stack Overflow
ansible - How can I check for an installed packaged version on multiple linux hosts? - DevOps Stack Exchange
Best way to check for installed yum package/rpm version in Ansible and use it - Stack Overflow
use ansible to check if application is installed - Ansible Project - Ansible
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
I just want to update this old discussion to point out that there is now a package module that makes this more straightforward
- name: get the rpm or apt package facts
package_facts:
manager: "auto"
- name: show apache2 version
debug: var=ansible_facts.packages.apache2[0].version
I think more native ansible way would be:
- name: get package version
yum:
list: package_name
register: package_name_version
- name: set package version
set_fact:
package_name_version: "{{ package_name_version.results|selectattr('yumstate','equalto','installed')|map(attribute='version')|list|first }}"
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.9Now 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 definedbut 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?
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.
I need a playbook that checks to see if PHP is installed.
So, the equivalent of yum list installed | grep php in Linux.