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'
@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