# Make an SSH connection and run a command from within the playbook

**URL:** <https://forum.ansible.com/t/make-an-ssh-connection-and-run-a-command-from-within-the-playbook/36256>\
**Category:** Ansible Developer\
**Created:** [January 27, 2022, 6:23pm UTC](https://forum.ansible.com/t/make-an-ssh-connection-and-run-a-command-from-within-the-playbook/36256 "2022-01-27T18:23:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Bianco](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@Daniel\_Bianco](https://forum.ansible.com/u/Daniel_Bianco)\
**Post date:** [January 27, 2022, 6:23pm UTC](https://forum.ansible.com/t/make-an-ssh-connection-and-run-a-command-from-within-the-playbook/36256/1 "2022-01-27T18:23:55Z")

</div>

I’m working on an automation to create/delete users on storage devices.  
I have been only provided with the commands that the storage admin use and users that can ssh to the storage devices.

My first attempt was to create a normal playbook that connects to the storage device and run the commands.  
The problem with this option is that the storage devices aren’t unix/linux, so its not possible to run the playbook normally. Basically is not able to create the temporary directory and it doesn’t have python to run the command.

So, my second attempt was to use existing modules/API.  
My problem this time is that some of the storage devices don’t have API available or the user doesn’t have API access :S

My last will be to try to use shell and use multiple commands.

- name: iterate user groups

shell: |

sshpass “password” ssh user@storage\_device

do\_some\_stuff\_here

and\_some\_other\_stuff

Is there any better option to do this kind of connection?

Thanks,  
Daniel

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 27, 2022, 7:55pm UTC](https://forum.ansible.com/t/make-an-ssh-connection-and-run-a-command-from-within-the-playbook/36256/2 "2022-01-27T19:55:38Z")

</div>

The cliconf/network\_cli plugins do this, they handle the connection  
internally in the module and use ssh/paramiko/libssh to interact with  
the target

---

<div class="post-metadata">

**Author:** ![Daniel\_Bianco](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@Daniel\_Bianco](https://forum.ansible.com/u/Daniel_Bianco)\
**Post date:** [January 27, 2022, 8:23pm UTC](https://forum.ansible.com/t/make-an-ssh-connection-and-run-a-command-from-within-the-playbook/36256/3 "2022-01-27T20:23:25Z")

</div>

Brian, thanks for the info.

As you mention, I found the netcommon collection ([https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/index.html](https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/index.html))

I understand the idea, but I don’t understand how to use the connection plugin. ([https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/libssh\_connection.html#ansible-collections-ansible-netcommon-libssh-connection](https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/libssh_connection.html#ansible-collections-ansible-netcommon-libssh-connection))

Do you have any example?

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 29, 2022, 12:08am UTC](https://forum.ansible.com/t/make-an-ssh-connection-and-run-a-command-from-within-the-playbook/36256/4 "2022-01-29T00:08:46Z")

</div>

Other than the modules themselves, not really, they use  
[https://github.com/ansible/ansible/blob/devel/lib/ansible/module\_utils/connection.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/connection.py)  
to establish a connection via the 'ansible-conneciton' tool (which is  
what actually uses the connection plugin), which exposes a socket to  
the module, the module then uses jsonrpc to send/read commands sent  
over that socket, which ansible-connection relays to the remote target  
(while not copying any code to it).

General docs to develop these types of plugins:  
[https://docs.ansible.com/ansible/latest/network/dev\_guide/developing\_plugins\_network.html](https://docs.ansible.com/ansible/latest/network/dev_guide/developing_plugins_network.html)

main dev guide  
[https://docs.ansible.com/ansible/latest/dev\_guide/index.html](https://docs.ansible.com/ansible/latest/dev_guide/index.html)

more communication channels (see network working group, they are most  
likely to have better answers about this type of development )  
[https://docs.ansible.com/ansible/latest/community/communication.html](https://docs.ansible.com/ansible/latest/community/communication.html)
