我使用“眼睛”作为主管,并且在模板更改上必须运行以下内容:

eye load service.rb
eye restart service.rb

我想将其定义为所有应用程序的单个处理程序,并将其命名为
eye reload appname

并在处理程序中这样操作:
- name: reload eye service
command: eye load /path/{{ service }}.rb && eye restart {{ service }}

但是我找不到将变量传递给处理程序的方法。是否可以?

最佳答案

处理程序/main.yml:

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

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

或者您可以通过命令行定义{{service}}:
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及更高版本中立即刷新所有处理程序命令,则可以:
tasks:
   - shell: some tasks go here
   - meta: flush_handlers
   - shell: some other tasks

关于ansible:将变量传递给处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26475761/

10-10 17:48
查看更多