ansible:将变量传递给处理程序 [英] ansible: pass variable to a handler

查看:31
本文介绍了ansible:将变量传递给处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用眼睛"作为主管,并且模板中的更改必须运行如下:

I use an "eye" as a supervisor and on changes in templates have to runs something like this:

eye load service.rb
eye restart service.rb

我想将它定义为所有应用程序的单个处理程序,并像这样调用

I want to define this as a single handler for all the apps and call it like

eye reload appname

在处理程序中操作如下:

And in a handler operate like this:

- name: reload eye service
command: eye load /path/{{ service }}.rb && eye restart {{ service }}

但是我找不到将变量传递给处理程序的方法.可能吗?

But I can't find a way to pass variable to a handler. Is it possible?

推荐答案

不要这样做.我理解您希望将 Ansible 用作编程工具,其中处理程序"是您调用"的函数",但事实并非如此.

Don't do this. I understand your desire to use Ansible as a programming tool, where 'handler' is a 'function' you 'call', but it's not.

你可以发明十几种技巧来做你想做的事,但结果将是一团糟,难以阅读,甚至更难调试.

You can invent a dozen of tricks to do what you want, but result would be a total mess, hard to read and even harder to debug.

关键问题是 ansible 不支持参数传递"到任何东西(模块除外).您阅读或自己发明的所有技巧都会改变全局变量.如果您曾经用任何语言编写过至少一点,您就会知道,每个函数都使用全局变量(用于读取和写入以及传递参数)的程序存在根本性缺陷.

The key issue is that ansible does not support 'argument passing' to anything (except for modules). All tricks you read about or invent by yourself will change global variable. If you ever wrote at least bit in any language , you know, that program where every function is using global variables (for read and write, and to pass arguments) is fundamentally flawed.

那么,如何在一个非常好且可读的 Ansible 中做到这一点?

So, how to do this in a very good and readable Ansible?

是的,只是为每个服务编写了一个单独的处理程序.它是最干净、最简单的 Ansible.易于阅读,易于更改.

Yes, just wrote a separate handler for each service. It's the cleanest and simplest Ansible. Easy to read, easy to change.

顺便说一句:如果您必须在链中执行操作,请不要使用&&"加入它们.

BTW: if you have to actions in a chain, do not join them with '&&'.

使用两个单独的处理程序:

Use two separate handlers:

- foo:
  notify:
    - eye reload
    - eye restart foo

(注意,处理程序的顺序是在处理程序列表中定义的,而不是在通知"列表中).

(note, that order of handlers is defined in the handlers list, not the 'notify' list).

顺便说一句,如果您的服务很少,您将节省多次重新加载操作 - 'eye reload' 将被调用一次.

Btw, if you have few services you will save on multiple reload operations - 'eye reload' would be called once.

这篇关于ansible:将变量传递给处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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