ansible: pass variable to a handler

假装没事ソ 提交于 2020-05-28 14:06:07

问题


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?


回答1:


handlers/main.yml:

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

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

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

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


来源:https://stackoverflow.com/questions/26475761/ansible-pass-variable-to-a-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!