Ansible自定义角色(带有自定义命令模块)

时间:2019-02-28 01:45:09

标签: ansible ansible-role

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

我希望它会出现在 roles / 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.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

1 个答案:

答案 0 :(得分: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

此致

Stopostit