我可以在一个剧本执行中两次使用相同的 Ansible 处理程序吗?

Can I use the same Ansible handler twice in one playbook execution?

我正在尝试创建一个文件来表示脚本/命令的执行已经完成

我知道我们应该避免脚本/命令,但在这里我不能

所以为了防止重新执行我正在创建一个文件来表示它已经被执行

这适用于后一项任务,但不适用于前者!

我想知道同一个处理程序是否可以使用两次

任务..

- set_fact: donefile=nodesource_setup

- name: Ensure node install script executed
  shell: ~/tmp/nodesource_setup.sh >> ~/tmp/nodesource_setup.log
  args:
    creates: ~/tmp/{{ donefile }}.done # Cannot one line this
  notify: Done

- set_fact: donefile=ensure-npm-does-not-use-unicode

- name: Ensure npm does not use unicode
  command: npm config set unicode false
  args:
    creates: ~/tmp/{{ donefile }}.done
  notify: Done

处理程序..

- name: Done
  copy:
    content: "done"
    dest: ~/tmp/{{ donefile }}.done
    force: no

处理程序是 运行 一次的任务:

  • 所有其他任务完成后 运行(默认)
  • meta: flush_handlers 任务上("notify" 标志被重置,如果再次通知处理程序,它将再次 运行)

您的代码的问题在于,在执行 Done 处理程序时,您的 donefile 变量的最后一个值为 donefile=ensure-npm-does-not-use-unicode


您可以添加任务:

- meta: flush_handlers

在更改 donefile 的值之前,因此对于给定的值,处理程序将为 运行。唯一的问题是它没有多大意义 - 它可以是常规任务而不是处理程序。

处理程序存在的理由是防止它被多次执行,即使有多个任务可能会请求它。

如果你想运行一次,你可以只做一个常规任务或一个有when条件的任务。