ansible:将 with_items 与通知处理程序一起使用 [英] ansible: using with_items with notify handler

查看:29
本文介绍了ansible:将 with_items 与通知处理程序一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个变量传递给通知处理程序,但在 SO、文档或 github 存储库中的问题、如何做的任何地方都找不到它.我正在做的是部署多个 webapps,当这些 webapps 之一的代码发生更改时,它应该重新启动该 webapp 的服务.

I want to pass a variable to a notification handler, but can't find anywhere be it here on SO, the docs or the issues in the github repo, how to do it. What I'm doing is deploying multiple webapps, and when the code for one of those webapps is changed, it should restart the service for that webapp.

来自这个问题,我让这个工作,有点:

From this SO question, I got this to work, somewhat:

- hosts: localhost
  tasks:
  - name: "task 1"
    shell: "echo {{ item }}"
    register: "task_1_output"
    with_items: [a,b]
  - name: "task 2"
    debug:
      msg: "{{ item.item }}"
    when: item.changed
    with_items: task_1_output.results

(将其放入 test.yml 并使用 ansible-playbook test.yml -c local 运行.)

(Put it in test.yml and run it with ansible-playbook test.yml -c local.)

但这会注册第一个任务的结果,并有条件地在第二个任务中循环.我的问题是,当您有两个或多个任务需要通知第二个任务时,它会变得混乱!例如,如果代码已更新或配置已更改,则重新启动 Web 服务.

But this registers the result of the first task and conditionally loops over that in the second task. My problem is that it gets messy when you have two or more tasks that need to notify the second task! For example, restart the web service if either the code was updated or the configuration was changed.

AFAICT,无法将变量传递给处理程序.那将干净地为我修复它.我在 github 上发现了一些问题,其他人也遇到了同样的问题,并且提出了一些语法,但没有一个真正起作用.

AFAICT, there's no way to pass a variable to a handler. That would cleanly fix it for me. I found some issues on github where other people run into the same problem, and some syntaxes are proposed, but none of them actually work.

包含子剧本也不起作用,因为不推荐将 with_itemsinclude 一起使用.

Including a sub-playbook won't work either, because using with_items together with include was deprecated.

在我的剧本中,我有一个 site.yml 列出了一个组的角色,然后在该组的 group_vars 中我定义了 web 应用程序的列表(包括应安装的版本).这对我来说似乎是正确的,因为这样我可以使用相同的剧本来进行分期和制作.但也许唯一的解决方案是多次定义角色,并复制用于登台和生产的角色列表.

In my playbooks, I have a site.yml that lists the roles of a group, then in the group_vars for that group I define the list of webapps (including the versions) that should be installed. This seems correct to me, because this way I can use the same playbook for staging and production. But maybe the only solution is to define the role multiple times, and duplicate the list of roles for staging and production.

那么这里的智慧是什么?

So what is the wisdom here?

推荐答案

我最终通过将应用程序拆分到同一角色的多个实例中解决了这个问题.这样,角色中的处理程序就可以引用定义为角色变量的变量.

I finally solved it by splitting the apps out over multiple instances of the same role. This way, the handler in the role can refer to variables that are defined as role variable.

在 site.yml 中:

In site.yml:

- hosts: localhost
  roles:
  - role: something
    name: a
  - role: something
    name: b

在roles/something/tasks/main.yml:

In roles/something/tasks/main.yml:

- name: do something
  shell: "echo {{ name }}"
  notify: something happened

- name: do something else
  shell: "echo {{ name }}"
  notify: something happened

在roles/something/handlers/main.yml:

In roles/something/handlers/main.yml:

- name: something happened
  debug:
    msg: "{{ name }}"

看起来比第一个解决方案要少得多!

Seems a lot less hackish than the first solution!

这篇关于ansible:将 with_items 与通知处理程序一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆