2016-10-04 52 views
0
# "Run application specific configuration scripts" 
- include: "app_cfg/{{ app_name }}.yml" 
    when: "{{ app_conf[app_name].app_cfg }}" 
    ignore_errors: no 
    tags: 
    - conf 

我認爲我可以有條件地包括專用劇本簡單地通過一個變量設置爲true/false值,像這樣:不得不創建文件事先ansible條件包括:

app_conf: 
    my_app_1: 
    app_cfg: no 
    my_app_2: 
    app_cfg: yes 

不幸Ansible是逼我預先創建文件:

ERROR: file could not read: <...>/tasks/app_cfg/app_config.yml 

有沒有辦法可以避免創建一堆空文件?

# ansible --version 
ansible 1.9.2 

回答

2

includewhen不是常識條件。
它實際上包含了包含文件中的每個任務,並將給定的when語句添加到每個任務包含的任務中。
所以它期望包含文件存在。

您可以嘗試使用with_first_foundskip: true來處理此問題。
事情是這樣的:

# warning, code not tested 
- include: "app_cfg/{{ app_name }}.yml" 
    with_first_found: 
    - files: 
     - "{{ app_conf[app_name].app_cfg | ternary('app_cfg/'+app_name+'.yml', 'unexisting.file') }}" 
     skip: true 
    tags: conf 

它應該提供有效的配置名稱,如果app_cfg是真實的,unexisting.file(這將是跳過),否則。

請參閱this關於跳過選項的答案。

+0

雖然有點難看,但仍然是一個很棒的功能,我完全錯過了安全的文檔叢林。謝謝! –

+0

我不得不接受你的答案,因爲它會顯示錯誤: '錯誤:[DEPRECATED]:include + with_items是一個已刪除的棄用功能<...>' –

+0

@NarūnasK考慮升級到Ansible 2.x:_In 2.0再次能夠使用with_ loops和任務includes_。 –