Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible shell and with_items

Tags:

shell

ansible

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

like image 646
Riccardo79 Avatar asked Dec 22 '25 17:12

Riccardo79


1 Answers

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.

like image 50
Konstantin Suvorov Avatar answered Dec 24 '25 07:12

Konstantin Suvorov