2017-06-29 36 views
0

地方行動:如何加快與考慮下面的劇本環路(with_items或with_sequence)

--- 
- name: test local_action with_items 
    hosts: localhost 
    gather_facts: false 
    tasks: 
    - name: "add something to a file" 
     shell: echo '{{item}}' >> foo.txt 
     with_items: 
     - "aaaaa" 
     - "aaaaa" 
# using 40 items 

--- 
- name: test local_action with_items 
    hosts: localhost 
    gather_facts: false 
    tasks: 
    - name: "add something to a file" 
     shell: echo '{{item}}' >> foo.txt 
     with_sequence: count=40 

後者劇本運行5秒鐘。

Using a bash loop is obviously much(1000次)更快,耗時5毫秒:

time for i in $(seq 1 40); do echo $i >> foo.txt;  done 

現在很明顯,Ansible具有一些開銷,但是否有可能加快這?

+2

Ansible不應該是快如閃電,它應該是很容易和舒適的使用。而且您通常使用Ansible將基礎設施部署從幾分鐘加速到幾分鐘,而不是從幾分鐘到幾秒鐘「回聲」循環。 –

+0

@KonstantinSuvorov:好的,但是一個複雜的劇本可能包含許多'lineinfile'條目,並且如果週轉時間可以減少幾分鐘,它可能會有用。如果這個循環需要,比如100毫秒或500毫秒...... – user140547

回答

1

而不是shell模塊,使用raw模塊。它將和bash循環一樣快。

--- 
- name: test local_action with_items 
    hosts: localhost 
    gather_facts: false 
    tasks: 
    - name: "add something to a file" 
     raw: echo '{{item}}' >> foo.txt 
     with_sequence: count=40 
... 

無論如何,如果你想表現的,也許寫代碼C.