【发布时间】:2015-07-31 10:49:48
【问题描述】:
我有一个 Ansible playbook,用于将 Java 应用程序部署为 init.d 守护进程。
作为 Ansible 和 Linux 的初学者,我无法根据主机的状态有条件地在主机上执行任务。
也就是说,我有一些主机已经存在服务并且正在运行,我想在做任何其他事情之前停止它。然后可能会有新的主机,它们还没有服务。所以我不能简单地使用service: name={{service_name}} state=stopped,因为这将在新主机上失败。
我怎样才能做到这一点?到目前为止,这是我所拥有的:
- name: Check if Service Exists
shell: "if chkconfig --list | grep -q my_service; then echo true; else echo false; fi;"
register: service_exists
# This should only execute on hosts where the service is present
- name: Stop Service
service: name={{service_name}} state=stopped
when: service_exists
register: service_stopped
# This too
- name: Remove Old App Folder
command: rm -rf {{app_target_folder}}
when: service_exists
# This should be executed on all hosts, but only after the service has stopped, if it was present
- name: Unpack App Archive
unarchive: src=../target/{{app_tar_name}} dest=/opt
【问题讨论】:
标签: centos ansible ansible-playbook