1

I am trying to implement a custom network command module as an Ansible role. This module will run commands on remote devices.

Ansible connection type is network_cli

Created the role by using below command

ansible-galaxy init --type=network test-command-mod

which gave me below default directory structure

  roles
  └── test-command-mod
    ├── cliconf_plugins
    │   ├── myos.py           (my file)
    ├── defaults
    │   └── main.yml          
    ├── files
    ├── library
    │   ├── __init__.py
    │   └── myos_command.py   (my file - the command module)
    ├── meta
    │   └── main.yml
    ├── module_utils
    │   └── myos.py           (my file)
    ├── README.md
    ├── tasks
    │   ├── execute-commands.yml ( a test task to run commands )
    │   └── main.yml
    ├── templates
    ├── terminal_plugins
    │   ├── myos.py           (my file - emulating the myos terminal)
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

Below it is how used in an Ansible playbook

  - hosts: my_os_cli
    gather_facts: False
    roles:
      - role: test-command-mod
    tasks:
    - name: run some commands on the device
      myos_command:
        commands:
          - command: 'show version'
          - command: 'show ntp status'
      vars:
        ansible_connection: network_cli
        ansible_network_os: myos

When using from a role, it fails with below message

  The full traceback is:
  Traceback (most recent call last):
  File "/ansible/bin/ansible-connection", line 102, in start
    self.connection._connect()
  File "/ansible/lib/ansible/plugins/connection/network_cli.py", line 338, in _connect
    raise AnsibleConnectionFailure('network os %s is not supported' % self._network_os)
  AnsibleConnectionFailure: network os myos is not supported

When running Ansible in debug mode, saw below in the log

unable to load cliconf for network_os myos

Moreover, it is trying to look for plugins/cliconf/myos.py under default Ansible location instead of the my Ansible role (test-command-mod).

I expect it to look in roles/test-command-mod/cliconf_plugins/myos.py

Is this a bug or by design?

Also, role works if

  • ansible.cfg is updated with

    cliconf_plugins     = ./roles/test-command-mod/cliconf_plugins
    terminal_plugins   =  ./roles/test-command-mod/terminal_plugins
    
  • OR set the below environment variables

    export ANSIBLE_TERMINAL_PLUGINS=./roles/test-command-mod/terminal_plugins
    export ANSIBLE_CLICONF_PLUGINS=./roles/test-command-mod/cliconf_plugins
    
  • OR if copy files (my file); to their respective directories in ansible installation.

Thoughts please?

EDIT: ansible.cfg as follows

[defaults]
# after suggestion
#cliconf_plugins     = cliconf_plugins:./roles/test-command-mod/cliconf_plugins
#terminal_plugins   =  terminal_plugins:../roles/test-command-mod/terminal_plugins

# before suggestion
cliconf_plugins     = ./roles/test-command-mod/cliconf_plugins
terminal_plugins   =  ./roles/test-command-mod/terminal_plugins

[paramiko_connection]
look_for_keys = False
rawat
  • 165
  • 1
  • 2
  • 15
  • Can you share your ansible.cfg? Also add the following: `cliconf_plugins: cliconf_plugins` and `terminal_plugins: terminal_plugins` – imjoseangel Feb 28 '19 at 09:48
  • @imjoseangel: Updated the post with ansible.cfg. Tried it but still not loading from roles cliconf_plugins and terminal_plugins directories – rawat Feb 28 '19 at 22:08
  • 1
    opened the issue in Ansible community [issue opened](https://github.com/ansible/ansible/issues/53308#issue-417107493) – rawat Mar 05 '19 at 04:15

1 Answers1

0

We have the exact same issue and we have been investigating for a week.

We tried the same things as you did with the same results.

The solution we came up with is to use the install.yml in the tasks directory in order to add the terminal and cliconf into their respectives system install directories.

---
- block: 
    - name: Create terminal target directory
      file: 
        path: /usr/share/ansible/plugins/terminal/ 
        state: directory 
        mode: 0755
      connection: local

    - name: install terminal plugin
      copy: 
        src: terminal_plugins/myos.py
        dest: /usr/share/ansible/plugins/terminal/myos.py
      connection: local

    - name: Create cliconf target directory
      file: 
        path: /usr/share/ansible/plugins/cliconf/ 
        state: directory 
        mode: 0755
      connection: local

    - name: install cliconf plugin
      copy: 
        src: cliconf_plugins/myos.py
        dest: /usr/share/ansible/plugins/cliconf/myos.py
      connection: local

  run_once: true

You may then add a call to install.yml in main.yml so that it executes the copy when you use the role:

---
- name: install/update driver
  include: install.yml

Regards,

Stopostit

Stopostit
  • 1
  • 1