2014-10-20 80 views
6

我使用的「眼睛」作爲監督者和模板的修改都運行是這樣的:ansible:傳遞變量的處理程序

eye load service.rb 
eye restart service.rb 

我想將其定義爲對所有的單處理器應用程序並調用它像

eye reload appname 

而且在處理這樣操作的:

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

但我不能找到一種方法來傳遞變量到處理程序。可能嗎?

+0

這看起來像一個重複http://stackoverflow.com/questions/25694249/ansible-using-with-items-with-notify-handler – Rachel 2014-11-02 14:04:52

+0

使用shell模塊[http://docs.ansible.com/ansible/shell_module.html] – 2016-03-09 04:10:59

+1

@va leriy-solovyov,除了'''&&'''以外沒有任何區別。參數化的處理程序只能在2.0中工作,所以propper方式是在處理程序名稱中使用服務名稱: '' - name:reload eye {{service}} shell:eye load/path/{{service}}。 rb && eye restart {{service}}'' – hryamzik 2016-03-10 07:43:58

回答

0

處理程序/ main.yml:

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

以便可以通過默認 默認/ main.yml設置變量:

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 
+1

這將工作,但只適用於需要重新啓動的單個應用程序。一個處理程序只會在劇本結束時被觸發一次。如果處理程序循環遍歷一個列表,並且需要重新啓動的每個服務都會通過'set_fact'將其添加到該列表,那麼它將工作。 – udondan 2016-03-10 08:31:27

+0

我在下一個任務之前刷新處理程序時添加了示例。 PS:也許你需要重新組織你的劇本? – 2016-03-10 08:45:58