Ansible custom module - DIY Connection to remote host

67 views
Skip to first unread message

phillip.from.oz

unread,
Dec 5, 2022, 3:23:33 AM12/5/22
to Ansible Project
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!

Dick Visser

unread,
Dec 5, 2022, 4:44:21 AM12/5/22
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f0e6bf0f-5987-424e-8db0-d160065a28f7n%40googlegroups.com.
--
Sent from Gmail Mobile

Michael Ströder

unread,
Dec 5, 2022, 6:47:21 AM12/5/22
to ansible...@googlegroups.com
On 12/5/22 09:23, phillip.from.oz wrote:
> 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?

No, for most modules.

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

Ciao, Michael.

phillip.from.oz

unread,
Dec 5, 2022, 5:48:54 PM12/5/22
to Ansible Project
Thanks to all.

This is what I'm doing:
Playbook:
---
- name: Connect to remote IRIS DB
  hosts: localhost
  tasks:
    - name: using my custom module
      IRIS:                     <-- New Module
        name: superuser
        password: secret
        instance: SCR
        namespace: WORK
        commands:
          - w !,”Hello”
          - w !,”World”
      register: result
   - name: show output
      debug:
        var: result

This is the logic:
ssh localhost
iris session “{{instance}}” -U "{{namespace}}"   <- connecting to IRIS DB
username: "{{name}}"                                            <- send variable name when username prompt
password: "{{password}}"                                     < - send variable password when password prompt
>“{{commands[0]}}”                                               < - send variable command when > prompt
>“{{commands[1]}}”
H


Michael Ströder

unread,
Dec 6, 2022, 4:59:42 AM12/6/22
to ansible...@googlegroups.com
On 12/5/22 23:48, phillip.from.oz wrote:
> This is what I'm doing:
> Playbook:
> ---
> - name: Connect to remote IRIS DB
>   hosts: localhost
>   tasks:
>     - name: using my custom module
>       IRIS:                     <-- New Module
>         name: superuser
>         password: secret
>         instance: SCR
>         namespace: WORK
>         commands:
>           - w !,”Hello”
>           - w !,”World”
>       register: result
>    - name: show output
>       debug:
>         var: result
>
> This is the logic:
> ssh localhost

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.

Todd Lewis

unread,
Dec 6, 2022, 12:23:53 PM12/6/22
to ansible...@googlegroups.com, uto...@gmail.com
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.
--
Todd

Phillip Wu

unread,
Dec 6, 2022, 10:59:16 PM12/6/22
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/EH4VH52Ji0c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7b155294-9958-7316-d550-bff9b63474dc%40gmail.com.

Todd Lewis

unread,
Dec 7, 2022, 6:00:04 AM12/7/22
to ansible...@googlegroups.com, uto...@gmail.com
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.

Todd Lewis

unread,
Dec 7, 2022, 8:49:54 PM12/7/22
to ansible...@googlegroups.com, uto...@gmail.com
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.

phillip.from.oz

unread,
Dec 9, 2022, 2:39:23 AM12/9/22
to Ansible Project
Thanks for the info
Reply all
Reply to author
Forward
0 new messages