[英]Rolling restart with ansible handlers
I want to run an ansible playbook that installs a service and restarts it if anything has changed since the last run (more or less the canonical use-case for ansible handlers).我想运行一个安装服务的 ansible playbook,如果自上次运行以来发生任何变化,则重新启动它(或多或少是 ansible 处理程序的规范用例)。
But I want a different parallelism for installing than for restarting: I want to install on all the hosts at a time but, if the "service-restart" handler gets invoked I want that to run on X hosts at a time.但是我想要一个与重新启动不同的并行安装:我想一次安装在所有主机上,但是,如果调用了“service-restart”处理程序,我希望它一次在 X 主机上运行。
I know this is possible with different plays that have different serial
values.我知道这对于具有不同
serial
值的不同剧本是可能的。 But I can't see how I could make use of handlers if I go this route.但是如果我走这条路,我看不到如何使用处理程序。 And I can't afford to have a single playbook with a
serial
value like 2
, as most of the time nothing will change for that service.而且我无法负担像
2
这样的serial
值的单一剧本,因为大多数情况下该服务不会发生任何变化。
Can the handlers span multiple plays?处理程序可以跨多个播放吗? Or is there any other way to do this without hacks?
或者有没有其他方法可以在没有黑客的情况下做到这一点?
Handlers are just tasks that Ansible will run at the end of a play if necessary.处理程序只是 Ansible 将在必要时在播放结束时运行的任务。 Given that they're implicitly added to the end of your play, they're going to be treated the same as any other tasks as far as parameters like
serial
go.鉴于它们被隐式添加到您的游戏末尾,就
serial
go 之类的参数而言,它们将被视为与任何其他任务相同。 Unfortunately this means that without a feature request that the Ansible developers accept you're unlikely to see a change in the behavior of serial
to support what you're trying to do.不幸的是,这意味着如果没有 Ansible 开发人员接受的功能请求,您不太可能看到
serial
行为的变化以支持您正在尝试做的事情。
I know you mentioned wanting to avoid hacks, but that's going to be the only way you can do something like this at this point.我知道您提到要避免黑客攻击,但这将是您目前可以执行此类操作的唯一方法。 It shouldn't be too difficult to set up something that's not a major hack, like creating a temporary file to flag the restart:
设置一些不是主要黑客的东西应该不会太难,比如创建一个临时文件来标记重新启动:
- hosts: some_hosts
name: install service
serial: 10
- handlers:
- name: schedule restart
command: touch /tmp/restart_flag
- tasks:
- name: install service
action: whatever...
notify: schedule restart
- hosts: some_hosts
name: restart service
serial: 2
- handlers:
- name: perform restart
service: name=foo state=restarted
- tasks:
- name: Delete /tmp/restart_flag. Restart service if file is deleted.
file: path=/tmp/restart_flag state=absent
notify: perform restart
Currently it's not possible.目前是不可能的。 There is an issue oppened for this.
为此存在一个问题。
Ansible 2.9.0 introduced the throttle
keyword, which can be used at the task, block, or play level to limit the number of workers (up to the specified forks or serial setting) allowed. Ansible 2.9.0推出了
throttle
关键字,它可以在任务,块,或打出水平来工人的数量限制(最多到指定的叉或串行设置)允许的。
For example, this can be used to restart nodes in a database cluster one by one:例如,这可以用于一一重启数据库集群中的节点:
- name: Restart MySQL
throttle: 1
service:
name: mysql
state: restarted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.