Riccardo79 Riccardo79 - 2 months ago 30
Bash Question

Ansible shell and with_items

I would like to know if

- shell: >
hostname;
whoami;


and

- 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": ""}


--ansible 2.1.1.0

Riccardo

Answer

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.

Comments