# multiple commands in single ssh connection

**URL:** <https://forum.ansible.com/t/multiple-commands-in-single-ssh-connection/23482>\
**Category:** Ansible Developer\
**Created:** [January 17, 2017, 8:48pm UTC](https://forum.ansible.com/t/multiple-commands-in-single-ssh-connection/23482 "2017-01-17T20:48:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Dasari](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Anand\_Dasari](https://forum.ansible.com/u/Anand_Dasari)\
**Post date:** [January 17, 2017, 8:48pm UTC](https://forum.ansible.com/t/multiple-commands-in-single-ssh-connection/23482/1 "2017-01-17T20:48:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [January 17, 2017, 10:55pm UTC](https://forum.ansible.com/t/multiple-commands-in-single-ssh-connection/23482/2 "2017-01-17T22:55:36Z")

</div>

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.
