2017-02-27 49 views
1

Ansible剛入門,並且正在考慮以下動態創建服務以及如何最好地管理此服務。我在下面描述了這不起作用,但希望它足以描述問題。將服務/ init.d名稱傳遞給Ansible處理程序作爲變量

任何指針讚賞。謝謝。

我正在使用模板文件來部署一堆幾乎完全相同的應用程序服務器。應用服務器的部署過程中,相應的初始化腳本使用變量放在:`

/etc/init.d/{{ application_instance }}` 

接下來,我想啓用並確保其啓動:

name: be sure app_xyz is running and enabled 
    service: name={{ application_instance }} state=started enabled=yes 

而且對我想要喜歡叫應用程序重新啓動時的配置文件更新:

- name: be sure app_xyz is configured 
    template: src=xyz.conf dest=/opt/application/{{ application_server }}.conf 
    notify: 
    - restart {{ application_server }} 

隨着處理程序看起來像這樣:

- name: restart {{ application_server }} 
    service: name={{ application_server }} state=restarted 
+0

請問請指定什麼是不工作?處理程序沒有被調用?或者什麼是錯誤信息? – ProfHase85

回答

2

您不需要動態處理程序名稱。怎麼樣一個靜態處理程序名稱:

# handler 
- name: restart application server 
    service: name={{ application_server }} state=restarted 

# task 
- name: be sure app_xyz is configured 
    template: src=xyz.conf dest=/opt/application/{{ application_server }}.conf 
    notify: 
    - restart application server 
    service: name={{ application_server }} state=restarted 
+0

這對於任務處理程序名稱不使用變量是有意義的。我很近,但是雪茄向你走來!謝謝。 – user45097