'How do I implement install yum package if is available using ansible?
I want to implement this logic in ansible: install a named yum package if is available, do not fail if the package does not exist in enabled repos, fail only if installation fails.
Solution 1:[1]
Just do a search first, and only install if it returns any results:
- hosts: host
tasks:
- name: Search for package
yum:
list: <package_name>
register: package_list
- name: Install package
yum:
name: <package_name>
state: present
become: yes
when: 'package_list.results | length != 0'
replace <package_name> with the package you are interested in.
Here is an alternative with failed_when, but it's quite hacky as it relies on the message returned by yum to match:
- hosts: host
tasks:
- name: Install package
yum:
name: <package_name>
state: present
register: result
become: yes
failed_when: '(result.msg | regex_replace("No package matching .* found available, installed or updated","")) != ""'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
