Last active
December 16, 2020 09:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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' |
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
This works better:
pkg.results|selectattr("yumstate", "match", "installed")|list|length != 0
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
disable SELINUX and firewall and try
@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
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
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.