Debian Codegraph @

ansible_collections/community/routeros/docs/docsite/rst/api-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.api-guide:
7
8 How to connect to RouterOS devices with the RouterOS API
9 ========================================================
10
11 You can use the :ansplugin:`community.routeros.api module <community.routeros.api#module>` to connect to a RouterOS device with the RouterOS API. More specific module to modify certain entries are the :ansplugin:`community.routeros.api_modify <community.routeros.api_modify#module>` and :ansplugin:`community.routeros.api_find_and_modify <community.routeros.api_find_and_modify#module>` modules. The :ansplugin:`community.routeros.api_info module <community.routeros.api_info#module>` allows to retrieve information on specific predefined paths that can be used as input for the :ansplugin:`community.routeros.api_modify <community.routeros.api_modify#module>` module, and the :ansplugin:`community.routeros.api_facts module <community.routeros.api_facts#module>` allows to retrieve Ansible facts using the RouterOS API.
12
13 No special setup is needed; the module needs to be run on a host that can connect to the device's API. The most common case is that the module is run on ``localhost``, either by using ``hosts: localhost`` in the playbook, or by using ``delegate_to: localhost`` for the task. The following example shows how to run the equivalent of ``/ip address print``:
14
15 .. code-block:: yaml+jinja
16
17 ---
18 - name: RouterOS test with API
19 hosts: localhost
20 gather_facts: false
21 vars:
22 hostname: 192.168.1.1
23 username: admin
24 password: test1234
25 tasks:
26 - name: Get "ip address print"
27 community.routeros.api:
28 hostname: "{{ hostname }}"
29 password: "{{ password }}"
30 username: "{{ username }}"
31 path: "ip address"
32 # The following options configure TLS/SSL.
33 # Depending on your setup, these options need different values:
34 tls: true
35 validate_certs: true
36 validate_cert_hostname: true
37 # If you are using your own PKI, specify the path to your CA certificate here:
38 # ca_path: /path/to/ca-certificate.pem
39 register: print_path
40
41 - name: Show IP address of first interface
42 ansible.builtin.debug:
43 msg: "{{ print_path.msg[0].address }}"
44
45 This results in the following output:
46
47 .. code-block:: ansible-output
48
49 PLAY [RouterOS test] *********************************************************************************************
50
51 TASK [Get "ip address print"] ************************************************************************************
52 ok: [localhost]
53
54 TASK [Show IP address of first interface] ************************************************************************
55 ok: [localhost] => {
56 "msg": "192.168.2.1/24"
57 }
58
59 PLAY RECAP *******************************************************************************************************
60 localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
61
62 Check out the documentation of the :ansplugin:`community.routeros.api module <community.routeros.api#module>` for details on the options.
63
64 Using the ``community.routeros.api`` module defaults group
65 ----------------------------------------------------------
66
67 To avoid having to specify common parameters for all the API based modules in every task, you can use the ``community.routeros.api`` :ref:`module defaults group <module_defaults_groups>`:
68
69 .. code-block:: yaml+jinja
70
71 ---
72 - name: RouterOS test with API
73 hosts: localhost
74 gather_facts: false
75 module_defaults:
76 group/community.routeros.api:
77 hostname: 192.168.1.1
78 password: admin
79 username: test1234
80 # The following options configure TLS/SSL.
81 # Depending on your setup, these options need different values:
82 tls: true
83 validate_certs: true
84 validate_cert_hostname: true
85 # If you are using your own PKI, specify the path to your CA certificate here:
86 # ca_path: /path/to/ca-certificate.pem
87 tasks:
88 - name: Gather facts
89 community.routeros.api_facts:
90
91 - name: Get "ip address print"
92 community.routeros.api:
93 path: "ip address"
94
95 - name: Change IP address to 192.168.1.1 for interface bridge
96 community.routeros.api_find_and_modify:
97 path: ip address
98 find:
99 interface: bridge
100 values:
101 address: "192.168.1.1/24"
102
103 Here all three tasks will use the options set for the module defaults group.
104
105 Setting up encryption
106 ---------------------
107
108 It is recommended to always use :ansopt:`tls=true` when connecting with the API, even if you are only connecting to the device through a trusted network. The following options control how TLS/SSL is used:
109
110 :force_no_cert: Setting to :ansval:`true` connects to the device without a certificate. **This is discouraged to use in production and is susceptible to Man-in-the-Middle attacks**, but might be useful when setting the device up. The default value is :ansval:`false`.
111 :validate_certs: Setting to :ansval:`false` disables any certificate validation. **This is discouraged to use in production**, but is needed when setting the device up. The default value is :ansval:`true`.
112 :validate_cert_hostname: Setting to :ansval:`false` (default) disables hostname verification during certificate validation. This is needed if the hostnames specified in the certificate do not match the hostname used for connecting (usually the device's IP). It is recommended to set up the certificate correctly and set this to :ansval:`true`; the default :ansval:`false` is chosen for backwards compatibility to an older version of the module.
113 :ca_path: If you are not using a commercially trusted CA certificate to sign your device's certificate, or have not included your CA certificate in Python's truststore, you need to point this option to the CA certificate.
114
115 We recommend to create a CA certificate that is used to sign the certificates for your RouterOS devices, and have the certificates include the correct hostname(s), including the IP of the device. That way, you can fully enable TLS and be sure that you always talk to the correct device.
116
117 Setting up a PKI
118 ^^^^^^^^^^^^^^^^
119
120 Please follow the instructions in the ``community.crypto`` :ref:`ansible_collections.community.crypto.docsite.guide_ownca` guide to set up a CA certificate and sign a certificate for your router. You should add a Subject Alternative Name for the IP address (for example ``IP:192.168.1.1``) and - if available - for the DNS name (for example ``DNS:router.local``) to the certificate.
121
122 Installing a certificate on a MikroTik router
123 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
124
125 Installing the certificate is best done with the SSH connection. (See the :ref:`ansible_collections.community.routeros.docsite.ssh-guide` guide for more information.) Once the certificate has been installed, and the HTTPS API enabled, it's easier to work with the API, since it has a quite a few less problems, and returns data as JSON objects instead of text you first have to parse.
126
127 First you have to convert the certificate and its private key to a `PKCS #12 bundle <https://en.wikipedia.org/wiki/PKCS_12>`_. This can be done with the :ansplugin:`community.crypto.openssl_pkcs12 <community.crypto.openssl_pkcs12#module>`. The following playbook assumes that the certificate is available as ``keys/{{ inventory_hostname }}.pem``, and its private key is available as ``keys/{{ inventory_hostname }}.key``. It generates a random passphrase to protect the PKCS#12 file.
128
129 .. code-block:: yaml+jinja
130
131 ---
132 - name: Install certificates on devices
133 hosts: routers
134 gather_facts: false
135 tasks:
136 - block:
137 - set_fact:
138 random_password: "{{ lookup('community.general.random_string', length=32, override_all='0123456789abcdefghijklmnopqrstuvwxyz') }}"
139
140 - name: Create PKCS#12 bundle
141 openssl_pkcs12:
142 path: keys/{{ inventory_hostname }}.p12
143 certificate_path: keys/{{ inventory_hostname }}.pem
144 privatekey_path: keys/{{ inventory_hostname }}.key
145 friendly_name: '{{ inventory_hostname }}'
146 passphrase: "{{ random_password }}"
147 mode: "0600"
148 changed_when: false
149 delegate_to: localhost
150
151 - name: Copy router certificate onto router
152 ansible.netcommon.net_put:
153 src: 'keys/{{ inventory_hostname }}.p12'
154 dest: '{{ inventory_hostname }}.p12'
155
156 - name: Install router certificate and clean up
157 community.routeros.command:
158 commands:
159 # Import certificate:
160 - /certificate import name={{ inventory_hostname }} file-name={{ inventory_hostname }}.p12 passphrase="{{ random_password }}"
161 # Remove PKCS12 bundle:
162 - /file remove {{ inventory_hostname }}.p12
163 # Show certificates
164 - /certificate print
165 register: output
166
167 - name: Show result of certificate import
168 debug:
169 var: output.stdout_lines[0]
170
171 - name: Show certificates
172 debug:
173 var: output.stdout_lines[2]
174
175 always:
176 - name: Wipe PKCS12 bundle
177 command: wipe keys/{{ inventory_hostname }}.p12
178 changed_when: false
179 delegate_to: localhost
180
181 - name: Use certificate
182 community.routeros.command:
183 commands:
184 - /ip service set www-ssl address={{ admin_network }} certificate={{ inventory_hostname }} disabled=no tls-version=only-1.2
185 - /ip service set api-ssl address={{ admin_network }} certificate={{ inventory_hostname }} tls-version=only-1.2
186
187 The playbook also assumes that ``admin_network`` describes the network from which the HTTPS and API interface can be accessed. This can be for example ``192.168.1.0/24``.
188
189 When this playbook completed successfully, you should be able to use the HTTPS admin interface (reachable in a browser from ``https://192.168.1.1/``, with the correct IP inserted), as well as the :ansplugin:`community.routeros.api module <community.routeros.api#module>` module with TLS and certificate validation enabled:
190
191 .. code-block:: yaml+jinja
192
193 - community.routeros.api:
194 # ...
195 tls: true
196 validate_certs: true
197 validate_cert_hostname: true
198 ca_path: /path/to/ca-certificate.pem