2016-08-19 25 views
6

是否可以在Ansible playbook中將項目列表應用於多個任務?舉個例子:在多個任務上應用with_items

- name: download and execute 
    hosts: server1 
    tasks: 
    - get_url: url="some-url/{{item}}" dest="/tmp/{{item}}" 
    with_items: 
    - "file1.sh" 
    - "file2.sh" 
    - shell: /tmp/{{item}} >> somelog.txt 
    with_items: 
    - "file1.sh" 
    - "file2.sh" 

是否有一些語法來避免重複項目列表?

回答

7

到今天爲止,你可以使用with_itemsinclude,所以你需要將劇本拆分成兩個文件:

- name: download and execute 
    hosts: server1 
    tasks: 
    - include: subtasks.yml file={{item}} 
    with_items: 
    - "file1.sh" 
    - "file2.sh" 

subtasks.yml

- get_url: url="some-url/{{file}}" dest="/tmp/{{file}}" 
- shell: /tmp/{{file}} >> somelog.txt 

有一個request,使with_items適用於block,但尚未實施。

4

你必須定義一個變量文件YAML名單的可能性:

--- 
myfiles: 
- "file1.sh" 
- "file2.sh" 
... 

然後你就可以在任務中使用

with_items: "{{ myfiles }}" 

相關問題