如果未安装软件包,如何使Ansible执行Shell脚本 [英] How to make Ansible execute a shell script if a package is not installed

查看:179
本文介绍了如果未安装软件包,如何使Ansible执行Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果未安装(rpm)软件包,如何使Ansible执行Shell脚本?可以利用yum模块吗?

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?

推荐答案

在这种情况下,我认为yum模块不会有所帮助.它当前具有3个状态:缺席,当前和最新.由于听起来好像您不想实际安装或卸下软件包(至少在此时),因此您需要分两个手动步骤进行操作.第一个任务将检查包是否存在,然后第二个任务将基于第一个命令的输出调用一个命令.

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.

如果您使用"rpm -q"检查某个软件包是否存在,那么对于已存在的软件包,输出将如下所示:

If you use "rpm -q" to check if a package exists then the output would look like this for a package that exists:

# rpm -q httpd
httpd-2.2.15-15.el6.centos.1.x86_64

,如果该软件包不存在,则如下所示:

and like this if the package doesn't exist:

# rpm -q httpdfoo
package httpdfoo is not installed

所以您的繁琐任务看起来像这样:

So your ansible tasks would look something like this:

- name: Check if foo.rpm is installed
  command: rpm -q foo.rpm
  register: rpm_check

- name: Execute script if foo.rpm is not installed
  command: somescript
  when: rpm_check.stdout.find('is not installed') != -1

如果该软件包存在,则rpm命令还将以0退出,如果找不到该软件包,则以1退出,因此另一种可能性是使用:

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:

when: rpm_check.rc == 1

这篇关于如果未安装软件包,如何使Ansible执行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆