如何在没有安装(rpm)软件包的情况下让Ansible执行shell脚本?是否有可能利用yum模块?如何让Ansible在未安装软件包的情况下执行shell脚本
回答
我不认为yum模块在这种情况下会有所帮助。它目前有3个州:缺席,现在和最新。由于听起来你不想实际安装或移除软件包(至少在这一点上),那么你需要通过两个手动步骤来完成。第一个任务将检查包是否存在,然后第二个任务将根据第一个命令的输出调用一个命令。
如果用“转-q”检查,如果再包存在输出应该是这样的一个存在的包:
# rpm -q httpd
httpd-2.2.15-15.el6.centos.1.x86_64
像这样如果包不存在:
# rpm -q httpdfoo
package httpdfoo is not installed
所以你ansible任务将是这个样子:
- 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出口如果没有找到该包,因此另一种可能性是使用:
when: rpm_check.rc == 1
基于上述布鲁斯P答复,对易于/ DEB文件类似的方法是
- name: Check if foo is installed
command: dpkg-query -l foo
register: deb_check
- name: Execute script if foo is not installed
command: somescript
when: deb_check.stdout.find('no packages found') != -1
与此相关。
将所有内容放在一起,完整的剧本为Debian
(Ubuntu的),这只有当它已经安装更新包:
---
- name: Update package only if already installed (Debian)
hosts: all
sudo: yes
tasks:
- name: Check if Package is installed
shell: dpkg-query -W -f='${Status}' {{ package }} | grep 'install ok installed'
register: is_installed
failed_when: no
changed_when: no
- name: Update Package only if installed
apt:
name: {{ package }}
state: latest
update_cache: yes
when: is_installed.rc == 0
可悲的是Ansible仍然没有内置的支持,使简单的包更新,见例如: https://github.com/ansible/ansible/issues/10856
正如[注意下文](https://stackoverflow.com/a/36405647/2192488)所示,使用'shell:dpkg --status {{package}} | grep'install ok installed''比使用'command :dpkg-query -l {{package}}'。 –
您不应该使用dpkg -l package
,因为它不知道您的软件包是否已被删除或仍然安装。
相反,它可能更好使用dpkg -s package
。
要检查是否安装了软件包:
- shell: dpkg -s package | grep 'install ok installed'
或者,如果你不介意搁置或其他国家的包装:
- shell: dpkg -s package | grep 'installed'
这回安装时0和1,如果不是。
(使用shell是很重要的,因为我们使用的是管道|
)
如果包是通过系统的包管理器安装(YUM,APT等)本身,那么你可以使用支票模式标志可以注册安装状态而无需实际安装软件包。
- name: check if package is installed
package:
name: mypackage
state: present
check_mode: true
register: mypackage_check
- name: run script if package installed
shell: myscript.sh
when: not mypackage_check.changed
我发现使用shell或命令模块不是“ansiblic”。
我更愿意使用yum模块和json_query过滤器来检查包是否已安装。例如。 httpd包:
- yum:
list: httpd
register: apache_service
- assert:
that:
- "'installed' in apache_service|json_query('results[*].yumstate')"
msg: 'httpd is not installed'
- 1. 如何在不“安装”的情况下下载Anaconda软件包?
- 2. 如何在不安装R的情况下加载软件包?
- 3. 如何在没有安静标志的情况下运行软件包安装
- 4. 如何在Ansible中定义要安装的软件包版本
- 5. 在Ansible有条件安装软件包
- 6. 如何在没有CronJob的情况下安排Unix Shell脚本
- 7. 在不依赖的情况下安装python软件包
- 8. 在没有setup.py的情况下安装github软件包?
- 9. Jenkins在构建失败的情况下不执行shell脚本
- 10. 只有在尚未安装的情况下,如何全局安装npm软件包?
- 11. 如何让vcredist_x86只在尚未安装的情况下重新安装?
- 12. 如何在没有Internet连接的情况下在ActivePerl中安装软件包?
- 13. 如何在不依赖的情况下在作曲家中安装软件包
- 14. 软件包安装过程中执行R脚本
- 15. python如何让bash脚本在不可执行的情况下运行?
- 16. Intellij如何在未安装任何JRE的情况下运行?
- 17. rpmbuild:如何在不安装%install的情况下构建软件包?
- 18. 如何在没有Cabal或Cabal-Install的情况下安装软件包/库?
- 19. NuGET问题。如何在没有Nuget的情况下安装软件包
- 20. 如何从已安装的NPM软件包运行脚本?
- 21. 在没有安装Robot的情况下运行Rational Robot脚本
- 22. 无法使用Ansible安装软件包
- 23. 如何在本地安装软件包?
- 24. 如何在本地安装软件包?
- 25. 如何在不移除本地安装的npm软件包的情况下对模块进行压缩
- 26. Nix shell:如何列出已安装的Haskell软件包版本
- 27. 如何在不执行的情况下加载脚本内容?
- 28. 如何在不访问页面的情况下执行脚本
- 29. OPAM:如何在不重新下载的情况下在本地重新安装软件包?
- 30. 如何在不被调用的情况下执行包含的脚本?
该命令的问题是,如果'rpm -q'退出0(没有找到包),该剧本将失败。为了防止在任务中附加:'ignore_errors:True',以便在后续任务中使用输出来安装rpm。 – romanofski
此外,您可能想要添加'changed_when:no'来强制命令模块不要说某些事情已经改变。 – Alicia
另外,为避免找不到软件包时的失败消息,您可以设置'failed_when:rpm_check.rc> 1'。 – Alicia