我有一个用于部署Java应用程序作为init.d守护进程的可执行的手册。
作为一个初学者在可可和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
当然我也可以检查/etc/init.d中是否存在包装器脚本。所以这就是我最终的结论:
- name: Check if Service Exists stat: path=/etc/init.d/{{service_name}} register: service_status - name: Stop Service service: name={{service_name}} state=stopped when: service_status.stat.exists register: service_stopped