Debian Codegraph @

ansible_collections/community/routeros/docs/docsite/rst/ssh-guide.rst

raw
1 ..
2 Copyright (c) Ansible Project
3 GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 SPDX-License-Identifier: GPL-3.0-or-later
5
6 .. _ansible_collections.community.routeros.docsite.ssh-guide:
7
8 How to connect to RouterOS devices with SSH
9 ===========================================
10
11 The collection offers two modules to connect to RouterOS devies with SSH:
12
13 - The :ansplugin:`community.routeros.facts module <community.routeros.facts#module>` gathers facts about a RouterOS device;
14 - The :ansplugin:`community.routeros.command module <community.routeros.command#module>` executes commands on a RouterOS device.
15
16 The modules need the :ansplugin:`ansible.netcommon.network_cli connection plugin <ansible.netcommon.network_cli#connection>` for this.
17
18 Important notes
19 ---------------
20
21 1. The SSH-based modules do not support arbitrary symbols in the router's identity. If you are having trouble connecting to your device, please make sure that your MikroTik's identity contains only alphanumeric characters and dashes. Also make sure that the identity string is not longer than 19 characters (`see issue for details <https://github.com/ansible-collections/community.routeros/issues/31>`__). Similar problems can happen for unsupported characters in your username.
22
23 2. The :ansplugin:`community.routeros.command module <community.routeros.command#module>` does not support nesting commands and expects every command to start with a forward slash (``/``). Running the following command will produce an error:
24
25 .. code-block:: yaml+jinja
26
27 - community.routeros.command:
28 commands:
29 - /ip
30 - print
31
32 3. When using the :ansplugin:`community.routeros.command module <community.routeros.command#module>` module, make sure to not specify too long commands. Alternatively, add something like ``+cet512w`` to the username (replace ``admin`` with ``admin+cet512w``) to tell RouterOS to not wrap before 512 characters in a line (`see issue for details <https://github.com/ansible-collections/community.routeros/issues/6>`__).
33
34 4. The :ansplugin:`ansible.netcommon.network_cli connection plugin <ansible.netcommon.network_cli#connection>` uses `paramiko <https://pypi.org/project/paramiko/>`_ by default to connect to devices with SSH. You can set its :ansopt:`ansible.netcommon.network_cli#connection:ssh_type` option to :ansval:`libssh` to use `ansible-pylibssh <https://pypi.org/project/ansible-pylibssh/>`_ instead, which offers Python bindings to libssh. See its documentation for details.
35
36 5. User is **not allowed** to login via SSH by password to modern Mikrotik if SSH key for the user is added!
37
38 Setting up an inventory
39 -----------------------
40
41 An example inventory ``hosts`` file for a RouterOS device is as follows:
42
43 .. code-block:: ini
44
45 [routers]
46 router ansible_host=192.168.2.1
47
48 [routers:vars]
49 ansible_connection=ansible.netcommon.network_cli
50 ansible_network_os=community.routeros.routeros
51 ansible_user=admin
52 ansible_ssh_pass=test1234
53
54 This tells Ansible that you have a RouterOS device called ``router`` with IP ``192.168.2.1``. Ansible should use the :ansplugin:`ansible.netcommon.network_cli connection plugin <ansible.netcommon.network_cli#connection>` together with the the :ansplugin:`community.routeros.routeros cliconf plugin <community.routeros.routeros#cliconf>`. The credentials are stored as ``ansible_user`` and ``ansible_ssh_pass`` in the inventory.
55
56 Connecting to the device
57 ------------------------
58
59 With the above inventory, you can use the following playbook to execute ``/system resource print`` on the device
60
61 .. code-block:: yaml+jinja
62
63 ---
64 - name: RouterOS test with network_cli connection
65 hosts: routers
66 gather_facts: false
67 tasks:
68
69 - name: Gather system resources
70 community.routeros.command:
71 commands:
72 - /system resource print
73 register: system_resource_print
74
75 - name: Show system resources
76 debug:
77 var: system_resource_print.stdout_lines
78
79 - name: Gather facts
80 community.routeros.facts:
81
82 - name: Show a fact
83 debug:
84 msg: "First IP address: {{ ansible_net_all_ipv4_addresses[0] }}"
85
86 This results in the following output:
87
88 .. code-block:: ansible-output
89
90 PLAY [RouterOS test with network_cli connection] *****************************************************************
91
92 TASK [Gather system resources] ***********************************************************************************
93 ok: [router]
94
95 TASK [Show system resources] *************************************************************************************
96 ok: [router] => {
97 "system_resource_print.stdout_lines": [
98 [
99 "uptime: 3d10h28m51s",
100 " version: 6.48.3 (stable)",
101 " build-time: May/25/2021 06:09:45",
102 " free-memory: 31.2MiB",
103 " total-memory: 64.0MiB",
104 " cpu: MIPS 24Kc V7.4",
105 " cpu-count: 1",
106 " cpu-frequency: 400MHz",
107 " cpu-load: 1%",
108 " free-hdd-space: 54.2MiB",
109 " total-hdd-space: 128.0MiB",
110 " write-sect-since-reboot: 927",
111 " write-sect-total: 51572981",
112 " bad-blocks: 1%",
113 " architecture-name: mipsbe",
114 " board-name: RB750GL",
115 " platform: MikroTik"
116 ]
117 ]
118 }
119
120 TASK [Gather facts] **********************************************************************************************
121 ok: [router]
122
123 TASK [Show a fact] ***********************************************************************************************
124 ok: [router] => {
125 "msg": "First IP address: 192.168.2.1"
126 }
127
128 PLAY RECAP *******************************************************************************************************
129 router : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0