# Ansible custom module - DIY Connection to remote host

**URL:** <https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418>\
**Category:** Ansible Project\
**Created:** [December 5, 2022, 8:23am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418 "2022-12-05T08:23:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_Wu](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@Phillip\_Wu](https://forum.ansible.com/u/Phillip_Wu)\
**Post date:** [December 5, 2022, 8:23am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/1 "2022-12-05T08:23:33Z")

</div>

Hi,  
When writing Ansible custom module, how is the code (python) connect to the remote host?

Is it the code of the Ansible custom module responsible for ssh to the remote host?  
If so how do I get the host I’m supposed to connect to?  
If so, how do I get the required credentials for the host?

An example would be great!

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [December 5, 2022, 9:43am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/2 "2022-12-05T09:43:49Z")

</div>

I have no experience with writing modules, but I would look at existing modules that are as similar as possible, and then use those for information and inspiration.

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [December 5, 2022, 11:47am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/3 "2022-12-05T11:47:00Z")

</div>

No, for most modules.

I'd recommend that you describe what you're trying to achieve.

Ciao, Michael.

---

<div class="post-metadata">

**Author:** ![Phillip\_Wu](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@Phillip\_Wu](https://forum.ansible.com/u/Phillip_Wu)\
**Post date:** [December 5, 2022, 10:48pm UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/4 "2022-12-05T22:48:54Z")

</div>

Thanks to all.

This is what I’m doing:  
Playbook:

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [December 6, 2022, 9:59am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/5 "2022-12-06T09:59:26Z")

</div>

I'd rather use

&nbsp;&nbsp;&nbsp;delegate\_to: localhost

for this particular task.

AFAICS this won't result in a SSH connection to local host.

But of course you have to manage the DB connection inside your module.

Ciao, Michael.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 6, 2022, 5:23pm UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/6 "2022-12-06T17:23:42Z")

</div>

I was hoping someone with IRIS DB experience in Ansible would answer by now, because I don’t want to mislead you. However, it looks to me like everything you’re doing below could be done through the “expect” module.

---

<div class="post-metadata">

**Author:** ![Phillip\_Wu](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@Phillip\_Wu](https://forum.ansible.com/u/Phillip_Wu)\
**Post date:** [December 7, 2022, 3:58am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/7 "2022-12-07T03:58:37Z")

</div>

Thanks to All!

Yes you’re right…I have been using Expect part of Ansible to interact with IRIS on the remote host previously.

However there is a timing problem sometimes and with the Expect module of Ansible you cannot change the delay before sending as far as I can see.

Also in my case sometimes some of my IRIS servers ask me to sign in and some use OS authentication and Ansible Expect gets rather difficult when it’s conversation changes

If anyone is interested when using module you do not need to ssh into the remote host as this is done for you without adding any configuration or code changes

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 7, 2022, 10:59am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/8 "2022-12-07T10:59:52Z")

</div>

In that case, you should start with the existing expect module and add the desired delay functionality. That would be far simpler than creating a worthwhile module from scratch. And you could contribute your changes so we would all benefit from your efforts.

As for the different login scenarios, that’s a challenge I’m sure someone has risen to before. I still think expect is your best choice.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 8, 2022, 1:49am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/9 "2022-12-08T01:49:34Z")

</div>

After some digging today, I see that Ansible’s expect module is actually a hook into the relatively limited python pexpect module, which itself doesn’t support the additional features you need.

However, the doc for Ansible’s shell module includes an example of calling the “real” expect, and _that_ lets you do lots of things, like “sleep 5”. There’s also “-s” (slow mode) and “-h” (slow like a human mode). I _expect_ (ahem) that would be a better place to spend your time.

---

<div class="post-metadata">

**Author:** ![Phillip\_Wu](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@Phillip\_Wu](https://forum.ansible.com/u/Phillip_Wu)\
**Post date:** [December 9, 2022, 7:39am UTC](https://forum.ansible.com/t/ansible-custom-module-diy-connection-to-remote-host/37418/10 "2022-12-09T07:39:23Z")

</div>

Thanks for the info
