Ansible custom module - DIY Connection to remote host

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!

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.

No, for most modules.

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

Ciao, Michael.

Thanks to all.

This is what I’m doing:
Playbook:

I'd rather use

   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.

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.

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

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.

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.

Thanks for the info