multiple commands in single ssh connection

Hi
does any body know how to run multiple commands in single ssh connection

i have the code

command: “{{ item }}”
with_items:

  • /bin/sh test.sh
  • /bin/sh test2.sh

when run this i can see “ESTABLISH SSH CONNECTION for user: test” twice.
Can somebody help me how can i run those two commands in one SSH connection.

Thanks

This is more a user list question, unless you’re developing a module to do it.

With the command module, you cannot. What you should do instead is build a script of all the commands you want to execute in one connection, and then use that with the “script” module (action plugin).

If you want to build a full module, to where the provided list gets flattened and passed into the remote host on a single connection, that’ll take more work, as only specific modules are allowed to do that, such as the package (yum, dnf, apt) modules.