如何使用 SSH 连接 RouterOS 设备
该集合提供两个模块来使用 SSH 连接 RouterOS 设备
community.routeros.facts 模块 收集关于 RouterOS 设备的信息;
community.routeros.command 模块 在 RouterOS 设备上执行命令。
这些模块需要 ansible.netcommon.network_cli 连接插件。
重要说明
基于 SSH 的模块不支持路由器标识中的任意符号。如果您连接到您的设备时遇到问题,请确保您的 MikroTik 标识仅包含字母数字字符和短划线。同时确保标识符字符串不超过 19 个字符 (查看问题详情)。用户名中不支持的字符也会导致类似的问题。
community.routeros.command 模块 不支持嵌套命令,并期望每个命令都以正斜杠开头 (
/
)。运行以下命令将产生错误- community.routeros.command: commands: - /ip - print
使用 community.routeros.command 模块 时,请确保不要指定过长的命令。或者,在用户名中添加类似
+cet512w
的内容(将admin
替换为admin+cet512w
)以告诉 RouterOS 不要在一行中换行超过 512 个字符 (查看问题详情)。ansible.netcommon.network_cli 连接插件 默认使用 paramiko 通过 SSH 连接到设备。您可以将其
ssh_type
选项设置为libssh
以改用 ansible-pylibssh,它提供 Python 绑定到 libssh。查看其文档了解详情。如果为用户添加了 SSH 密钥,则**不允许**用户通过 SSH 密码登录现代 Mikrotik!
设置清单
RouterOS 设备的示例清单 hosts
文件如下所示
[routers]
router ansible_host=192.168.2.1
[routers:vars]
ansible_connection=ansible.netcommon.network_cli
ansible_network_os=community.routeros.routeros
ansible_user=admin
ansible_ssh_pass=test1234
这告诉 Ansible 您有一个名为 router
、IP 为 192.168.2.1
的 RouterOS 设备。Ansible 应该将 ansible.netcommon.network_cli 连接插件 与 community.routeros.routeros cliconf 插件 一起使用。凭据存储为清单中的 ansible_user
和 ansible_ssh_pass
。
连接到设备
使用上述清单,您可以使用以下剧本在设备上执行 /system resource print
---
- name: RouterOS test with network_cli connection
hosts: routers
gather_facts: false
tasks:
- name: Gather system resources
community.routeros.command:
commands:
- /system resource print
register: system_resource_print
- name: Show system resources
debug:
var: system_resource_print.stdout_lines
- name: Gather facts
community.routeros.facts:
- name: Show a fact
debug:
msg: "First IP address: {{ ansible_net_all_ipv4_addresses[0] }}"
这将产生以下输出
PLAY [RouterOS test with network_cli connection] *****************************************************************
TASK [Gather system resources] ***********************************************************************************
ok: [router]
TASK [Show system resources] *************************************************************************************
ok: [router] => {
"system_resource_print.stdout_lines": [
[
"uptime: 3d10h28m51s",
" version: 6.48.3 (stable)",
" build-time: May/25/2021 06:09:45",
" free-memory: 31.2MiB",
" total-memory: 64.0MiB",
" cpu: MIPS 24Kc V7.4",
" cpu-count: 1",
" cpu-frequency: 400MHz",
" cpu-load: 1%",
" free-hdd-space: 54.2MiB",
" total-hdd-space: 128.0MiB",
" write-sect-since-reboot: 927",
" write-sect-total: 51572981",
" bad-blocks: 1%",
" architecture-name: mipsbe",
" board-name: RB750GL",
" platform: MikroTik"
]
]
}
TASK [Gather facts] **********************************************************************************************
ok: [router]
TASK [Show a fact] ***********************************************************************************************
ok: [router] => {
"msg": "First IP address: 192.168.2.1"
}
PLAY RECAP *******************************************************************************************************
router : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0