Skip to content

Instantly share code, notes, and snippets.

@goldyfruit
Last active December 16, 2020 09:40
Show Gist options
  • Save goldyfruit/f4f274be3144e6afca69 to your computer and use it in GitHub Desktop.
Save goldyfruit/f4f274be3144e6afca69 to your computer and use it in GitHub Desktop.
[ansible] Check via the yum module and a registered value if a package is installed or not
---
- name: Ansible tests playbook
hosts: all
remote_user: root
tasks:
- name: Check if mariadb-libs-5.5.44-2.el7.centos.x86_64 package is installed
yum:
list=mariadb-libs-5.5*x86_64
register: pkg
- name: Removing old mariadb-libs package
package:
name=mariadb-libs
state=absent
when: ansible_os_family == 'RedHat' and
pkg.results[0].yumstate == 'installed'
@svenXY
Copy link

svenXY commented Feb 22, 2017

hi, found this nice snippet, but for me it does not work under all circumstances.

I'm using the oracle-mysql-community repositories and it keeps listing the first found, not the last one.
This always reports as "available", not "installed".

Even trying pkg.results[-1].yumstate == 'installed' does not work.

@CrusaderX
Copy link

Hi, you can use this code:

- 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_installed.results[1].yumstate != 'installed') 

Works for me

@beebird
Copy link

beebird commented Jul 12, 2017

This works better:

pkg.results|selectattr("yumstate", "match", "installed")|list|length != 0

@bhagya7893
Copy link

I am using the code suggested by CrusaderX and I am getting the below error:
The error was: error while evaluating conditional ((is_installed.results|length == 1) or (is_installed.results[1].yumstate != 'installed')): list object has no element 1

I am running on Redhat 7.1

Copy link

ghost commented Mar 28, 2018

disable SELINUX and firewall and try

@nccurry
Copy link

nccurry commented Jul 16, 2018

@beebird solution is cleanest

- 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

@bhyde
Copy link

bhyde commented Apr 6, 2019

the package_facts module maybe better for this.

  - 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
    - .... etc ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment