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

查看:135
本文介绍了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?

推荐答案

handlers/main.yml:

handlers/main.yml:

- name: restart my service
  shell: eye load /path/{{ service }}.rb && eye restart {{ service }}

因此您可以通过默认设置变量 默认值/main.yml:

So you can setup variable through default defaults/main.yml:

service : "service"

或者您可以通过命令行定义{{service}}:

or you can define {{ service }} though command line:

ansible-playbook -i xxx path/to/playbook -e "service=service"

http://docs.ansible.com/ansible/playbooks_variables.html

PS: http://docs.ansible.com/ansible/playbooks_intro .html#playbook-language-

example
---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

http://docs.ansible.com/ansible/playbooks_intro.html#handlers-running-operations-on-change

如果您希望在1.2及更高版本中立即刷新所有处理程序命令,则可以:

If you ever want to flush all the handler commands immediately though, in 1.2 and later, you can:

tasks:
   - shell: some tasks go here
   - meta: flush_handlers
   - shell: some other tasks

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

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