gpt4 book ai didi

ansible - Ansible 中的条件通知取决于角色

转载 作者:行者123 更新时间:2023-12-04 13:46:46 26 4
gpt4 key购买 nike

在 Ansible 中是否可以仅当处理程序出现在该播放中时才有条件地通知处理程序?

例如:我有一个多主机配置的剧本。有些主机使用 Apache 运行 PHP,有些使用 PHP-FPM。根据主机,修改 PHP 时需要运行不同的处理程序。

这是该问题的一个简单示例:

- name: copy php.ini
copy:
src=roles/php/templates/{{ php_template }}/php.ini
dest=/etc/
notify:
- reload php-fpm
- reload apache

显然,我想要做的是在 php.ini 文件更改时通知正确的软件重新加载。但是 Ansible 给出了一个错误,因为在任何时候都只有一个处理程序存在于游戏中,例如:
ERROR: change handler (reload php-fpm) is not defined

有没有办法在不产生错误或重复 Ansible 代码的情况下实现这一目标?
提前致谢

最佳答案

最后,我通过检测相关角色的存在并使用它来确定要运行哪些处理程序,找到了一个解决方案。如果您可以看到更短的方式,请发布答案。

roles/php/tasks/main.yml
----
- name: detect apache
shell:
'ls /etc/init.d/ | grep httpd | wc -l'
register:
httpd_exists
- name: detect php-fpm
shell:
'ls /etc/init.d/ | grep php-fpm | wc -l'
register:
phpfpm_exists

然后为每个相关处理程序创建本地通知程序:
 notify:
- php reload apache
- php reload php-fpm

然后仍然在相同的角色中,向本地处理程序添加条件。
roles/php/handlers/main.yml
---
- name: php reload apache
include: ../../apache/handlers/main.yml
when: (httpd_exists.stdout == '1')

- name: php reload php-fpm
include: ../../php-fpm/handlers/main.yml
when: (phpfpm_exists.stdout == '1')

关于ansible - Ansible 中的条件通知取决于角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com