1

我正在尝试将自定义网络命令模块实现为 Ansible 角色。该模块将在远程设备上运行命令。

Ansible 连接类型是network_cli

使用以下命令创建角色

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

这给了我下面的默认目录结构

  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

下面是如何在 Ansible 剧本中使用

  - 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

从角色使用时,失败并显示以下消息

  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

在调试模式下运行 Ansible 时,在日志中看到以下内容

unable to load cliconf for network_os myos

此外,它试图在默认 Ansible 位置而不是我的 Ansible 角色(test-command-mod)下查找plugins/cliconf/myos.py 。

我希望它查看角色/test-command-mod/cliconf_plugins/myos.py

这是一个错误还是设计使然?

此外,如果角色有效

  • ansible.cfg 更新为

    cliconf_plugins     = ./roles/test-command-mod/cliconf_plugins
    terminal_plugins   =  ./roles/test-command-mod/terminal_plugins
    
  • 或设置以下环境变量

    export ANSIBLE_TERMINAL_PLUGINS=./roles/test-command-mod/terminal_plugins
    export ANSIBLE_CLICONF_PLUGINS=./roles/test-command-mod/cliconf_plugins
    
  • 或者如果复制文件(我的文件);在ansible安装中到他们各自的目录。

请问有什么想法吗?

编辑:ansible.cfg 如下

[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
4

1 回答 1

0

我们有完全相同的问题,我们已经调查了一周。

我们尝试了与您所做的相同的事情,但结果相同。

我们想出的解决方案是使用任务目录中的 install.yml,以便将终端和 cliconf 添加到各自的系统安装目录中。

---
- 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

然后,您可以在 main.yml 中添加对 install.yml 的调用,以便在您使用角色时执行副本:

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

问候,

停止邮政

于 2019-03-05T12:46:32.340 回答