我正在使用 Ansible2.6.1
.
我试图确保某些服务没有在目标主机上运行。
问题是该服务可能在某些主机上根本不存在。如果是这种情况,Ansible 将因缺少服务而失败并出现错误。服务由Systemd
.
使用服务模块:
- name: Stop service
service:
name: '{{ target_service }}'
state: stopped
因错误而失败Could not find the requested service SERVICE: host
尝试使用命令模块:
- name: Stop service
command: service {{ target_service }} stop
给出错误:Failed to stop SERVICE.service: Unit SERVICE.service not loaded.
我知道我可以使用ignore_errors: yes
但它也可能隐藏真正的错误。
另一种解决方案是有两个任务。一个检查服务是否存在,另一个仅在第一个任务找到服务时运行,但感觉很复杂。
是否有更简单的方法来确保服务停止并避免在服务不存在时出现错误?
我正在使用以下步骤:
- name: Get the list of services
service_facts:
- name: Stop service
systemd:
name: <service_name_here>
state: stopped
when: "'<service_name_here>.service' in services"
service_facts
可以在收集事实阶段调用一次。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)