简体   繁体   English

如何处理Ansible中的初始化脚本更改?

[英]How to handle changes to init scripts in Ansible?

I'm relatively new to Ansible and I've created a playbook that can install a Tomcat configuration on a 'bare' server. 我是Ansible的新手,我创建了一个剧本,可以在“裸机”服务器上安装Tomcat配置。 I'm wondering how to solve the problem of being able to update the init.d script while avoiding the stop the service at the start of the playbook when there is no change in the script. 我想知道如何解决能够更新init.d脚本的问题,同时避免在脚本没有更改的情况下避免在剧本开始时停止服务。 Here's the basic playbook: 这是基本的剧本:

- name: stop tomcat service
  service: name=my_service state=stopped

- name: copy init.d script
  template: src=script.j2 dest=/etc/init.d/my_service

- name: do other tasks here

- name: start tomcat service
  service: name=my_service state=restarted

This playbook will always stop and start the service, even if there are no changes. 即使没有更改,此剧本也将始终停止并启动服务。 What I want the playbook to do is only stop and start the service when there are actual changes. 我希望剧本仅在实际更改时停止并启动服务。

I know I could use handlers (need to look into that more), but I need to stop the service using the OLD init.d script, before copying the NEW script. 我知道我可以使用处理程序(需要进一步研究),但是在复制新脚本之前,我需要使用OLD init.d脚本停止服务。 AFAIK the handlers respond to the result of a task AFTER the action has taken place, which would mean the new script is already copied over the old one and might prevent the service from stopping and restarting. 在执行操作后,处理程序会自动响应任务的结果,这意味着新脚本已被复制到旧脚本上,并且可能阻止服务停止和重新启动。

How do I handle that? 我该如何处理?

Any task that is set to notify a handler will do exactly that at the end of the play. 设置为通知处理程序的任何任务都将在播放结束时完全执行该操作。

http://docs.ansible.com/playbooks_best_practices.html#task-and-handler-organization-for-a-role http://docs.ansible.com/playbooks_best_practices.html#task-and-handler-organization-for-a-role

 - name: Copy init.d script
   template: src=script.j2 dest=/etc/init.d/my_service
   notify: start tomcat service

handlers:
 - name: start tomcat service
   service: name=my_service state=restarted

You may want to have a play to work with the old script with a handler that stops the services with the old script, and a different play copying the new script with handlers. 您可能需要一个戏剧与一个带有处理程序的旧脚本一起使用,以停止使用该旧脚本的服务,而另一个戏剧则使用该处理程序来复制新脚本。

From what I've learned from the comments above, I guess the best configuration of this playbook should be something like the one below. 从上面的评论中学到的东西,我猜这本书的最佳配置应该类似于下面的配置。 I still don't get how to stop the service in time for the copy init script task to run, but only when the task will run. 我仍然不知道如何在复制初始化脚本任务运行时及时停止服务,而仅在任务运行时停止。

- tasks:
   - name: do various tasks here
     notify: restart tomcat service

   - name: stop tomcat service
     service: name=tomcat state=stopped
     when: {{ indicator_init_script_task_will_fire }}

   - name: copy init.d script
     notify: restart tomcat service

  handlers:
    - name: restart tomcat service
      service: name=my_service state=restarted

I haven't found what the indicator should be. 我还没有找到指标。 So feel free to update. 因此,随时进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM