Sindu Kovi
Sindu Kovi

Reputation: 1

Is it possible to use existing SSH connection (which is created at Ansible playbook level) in custom module to execute a different set of action

The use case is as follows.

---
- name: Playbook to run custom module
  hosts: "10.28.10.1"
  connection: network_cli
  gather_facts: false

  vars:
      ansible_network_os: nokia.sros.classic

  tasks:
    - name: Run Show Version
      cli_command: 
          command: "show version"
      register: result

    - name: Execute Custom Module
      cli_command_for_secondary_device:
          command: "show version"
          secondary_device: "10.28.10.2"
      register: result

Here devices are from nokia, I wanted to execute a command on secondary device "10.28.10.2" from a server using ansible-playbook but we can't do direct SSH to this device hence we need to first connect to primary device "10.28.10.1" then again do SSH to secondary device and execute the command.

To achieve this I may have to write a custom module like below which accepts primary ip, secondary ip and command.

    - name: Execute Custom Module
      cli_command_for_secondary_device:
          command: "show version"
          primary_device: "10.28.10.1"
          secondary_device: "10.28.10.2"
      register: result

Internally custom module will do SSH to primary first "10.28.10.1" then SSH to 10.28.10.2 and executes the command without using Ansible created SSH connection. So this process will increase our run time of playbook.

Hence I wanted to use existing ansible created SSH connection (internal Connection obj in Python) in my custom module so that it will do direct SSH to secondary ip 10.28.10.2 without creating explicit connection again. This improves the performance too.

Upvotes: 0

Views: 284

Answers (0)

Related Questions