ansible:在通知处理程序中使用with_items [英] ansible: using with_items with notify handler

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

问题描述

我想将一个变量传递给通知处理程序,但是在SO,文档或github存储库中的问题上找不到它的位置,怎么做.我正在做的是部署多个Web应用程序,并且当其中一个Web应用程序的代码更改时,它应重新启动该Web应用程序的服务.

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.

来自此SO问题,我可以用它来工作了,

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.

那这里的智慧是什么?

推荐答案

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

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

在角色/事物/任务/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

在角色/事物/处理程序/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天全站免登陆