Riccardo79 Riccardo79 - 10 months ago 126
Bash Question

Ansible shell and with_items

I would like to know if

- shell: >


- shell: "{{item}}"
with_items: ['hostname', 'whoami']

are equivalent? In the second example, Ansible will always use the same SSH connection for both commands (hostname, whoami)?

It seems to me that it is false...

- shell: "{{item}}"
with_items: ['export miavar=PIPPO', 'echo $miavar']

(item=export miavar=PIPPO) => {"changed": true, "cmd": "export miavar=PIPPO", "stdout": ""}
(item=echo $miavar) => {"changed": true, "cmd": "echo $miavar", "stdout": ""}



Answer Source

Ansible runs each loop iteration as separate run, so you end up with different ssh sessions.

There are some exceptions described in ANSIBLE_SQUASH_ACTIONS variable:

"apk, apt, dnf, package, pacman, pkgng, yum, zypper"

This modules are smart enough to squash all items into a single task call.