我正在尝试将自定义网络命令模块实现为 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