How can I make Ansible execute a shell script if a (rpm) package is not installed? Is it somehow possible to leverage the yum module?
相关问题
- Access ansible.cfg variable in task
- Unable to get “exclude” option working with unarch
- Ansible: find file and loop over paths
- Generating tuples variables in ansible templates
- Ansible: setting user on dynamic ec2
相关文章
- Ansible create postgresql user with access to all
- Difference between shell and command in ansible
- How to make Ansible run one certain task only on o
- clone a specific branch from git through ansible p
- Check if a list contains an item in Ansible
- Defining host as variable in Ansible hosts file
- ansible/jinja2 get unique subelements
- Custom credentials in Ansible Tower with Custom Py
Since Ansible 2.5, you can use the package_facts module:
Note: you need the python bindings for apt/rpm installed on the target, e.g.
python-apt
for Debian.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:
and like this if the package doesn't exist:
So your ansible tasks would look something like this:
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:
Based on the Bruce P answer above, a similar approach for apt/deb files is
Related to this.
Putting everything together, complete playbook for
Debian
(Ubuntu) which Updates package only if it's already installed:Sadly Ansible still hasn't built-in support for making simple package updating, see ex: https://github.com/ansible/ansible/issues/10856
If the package is installable through the system package manager (yum, apt, etc) itself, then you can make use of the check mode flag of ansible to register installation status without actually installing the package.
I find using shell or command module is not "ansiblic".
I prefer to use yum module and json_query filter to check if a package is already installed. E.g. httpd package :