0

Given the following playbook:

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

or

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

The latter playbook run 5 seconds.

Using a bash loop is obviously much (1000 times) faster and takes 5 ms:

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

Now it is clear that Ansible has some overhead, but is there any possibility to speed this up?

user140547
  • 7,750
  • 3
  • 28
  • 80
  • 2
    Ansible is not supposed to be lightning fast, it is supposed to be easy and comfortable to use. And you usually use Ansible to speed up infrastructure deployment from days to minutes and not `echo`-loops from minutes to seconds. – Konstantin Suvorov Jun 29 '17 at 13:53
  • @KonstantinSuvorov: Well sure, but a complex playbook may consist of many `lineinfile` entries, and if turnaround time can be reduced by a few minutes, it would probably be useful. If this loop took, say, 100 ms or 500ms... – user140547 Jun 29 '17 at 15:25
  • I expected to see the keyword `local_action` in your example – Jonathan May 01 '18 at 19:06

1 Answers1

1

Instead of shell module, use raw module. It will be as quick as the bash loop.

---
- 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
...

Anyway, if you want performance, maybe write your code in C.

GMaster
  • 1,431
  • 1
  • 16
  • 27