ansible:将变量传递给处理程序

hry*_*zik 10 ansible ansible-playbook

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

eye load service.rb
eye restart service.rb
Run Code Online (Sandbox Code Playgroud)

我想将其定义为所有应用程序的单个处理程序,并将其称为

eye reload appname
Run Code Online (Sandbox Code Playgroud)

并在处理程序中运行如下:

- name: reload eye service
command: eye load /path/{{ service }}.rb && eye restart {{ service }}
Run Code Online (Sandbox Code Playgroud)

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

Val*_*yov 0

处理程序/main.yml:

- name: restart my service
  shell: eye load /path/{{ service }}.rb && eye restart {{ service }}
Run Code Online (Sandbox Code Playgroud)

所以你可以通过默认的defaults/main.yml设置变量:

service : "service"
Run Code Online (Sandbox Code Playgroud)

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

ansible-playbook -i xxx path/to/playbook -e "service=service"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

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

tasks:
   - shell: some tasks go here
   - meta: flush_handlers
   - shell: some other tasks
Run Code Online (Sandbox Code Playgroud)

  • 这可行,但仅适用于需要重新启动的单个应用程序。处理程序只会在剧本结束时被触发一次。如果处理程序循环遍历一个列表,并且对于每个需要重新启动的服务,通过“set_fact”将一个项目添加到该列表中,那么它会起作用。 (4认同)