summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/routeros/docs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 16:03:42 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 16:03:42 +0000
commit66cec45960ce1d9c794e9399de15c138acb18aed (patch)
tree59cd19d69e9d56b7989b080da7c20ef1a3fe2a5a /ansible_collections/community/routeros/docs
parentInitial commit. (diff)
downloadansible-upstream.tar.xz
ansible-upstream.zip
Adding upstream version 7.3.0+dfsg.upstream/7.3.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/community/routeros/docs')
-rw-r--r--ansible_collections/community/routeros/docs/docsite/extra-docs.yml11
-rw-r--r--ansible_collections/community/routeros/docs/docsite/links.yml27
-rw-r--r--ansible_collections/community/routeros/docs/docsite/rst/api-guide.rst198
-rw-r--r--ansible_collections/community/routeros/docs/docsite/rst/quoting.rst19
-rw-r--r--ansible_collections/community/routeros/docs/docsite/rst/ssh-guide.rst127
5 files changed, 382 insertions, 0 deletions
diff --git a/ansible_collections/community/routeros/docs/docsite/extra-docs.yml b/ansible_collections/community/routeros/docs/docsite/extra-docs.yml
new file mode 100644
index 00000000..60915912
--- /dev/null
+++ b/ansible_collections/community/routeros/docs/docsite/extra-docs.yml
@@ -0,0 +1,11 @@
+---
+# Copyright (c) Ansible Project
+# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+sections:
+ - title: Guides
+ toctree:
+ - api-guide
+ - ssh-guide
+ - quoting
diff --git a/ansible_collections/community/routeros/docs/docsite/links.yml b/ansible_collections/community/routeros/docs/docsite/links.yml
new file mode 100644
index 00000000..9da799e7
--- /dev/null
+++ b/ansible_collections/community/routeros/docs/docsite/links.yml
@@ -0,0 +1,27 @@
+---
+# Copyright (c) Ansible Project
+# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+edit_on_github:
+ repository: ansible-collections/community.routeros
+ branch: main
+ path_prefix: ''
+
+extra_links:
+ - description: Submit a bug report
+ url: https://github.com/ansible-collections/community.routeros/issues/new?assignees=&labels=&template=bug_report.md
+ - description: Request a feature
+ url: https://github.com/ansible-collections/community.routeros/issues/new?assignees=&labels=&template=feature_request.md
+
+communication:
+ matrix_rooms:
+ - topic: General usage and support questions
+ room: '#users:ansible.im'
+ irc_channels:
+ - topic: General usage and support questions
+ network: Libera
+ channel: '#ansible'
+ mailing_lists:
+ - topic: Ansible Project List
+ url: https://groups.google.com/g/ansible-project
diff --git a/ansible_collections/community/routeros/docs/docsite/rst/api-guide.rst b/ansible_collections/community/routeros/docs/docsite/rst/api-guide.rst
new file mode 100644
index 00000000..f3bb6295
--- /dev/null
+++ b/ansible_collections/community/routeros/docs/docsite/rst/api-guide.rst
@@ -0,0 +1,198 @@
+..
+ Copyright (c) Ansible Project
+ GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+ SPDX-License-Identifier: GPL-3.0-or-later
+
+.. _ansible_collections.community.routeros.docsite.api-guide:
+
+How to connect to RouterOS devices with the RouterOS API
+========================================================
+
+You can use the :ref:`community.routeros.api module <ansible_collections.community.routeros.api_module>` to connect to a RouterOS device with the RouterOS API. More specific module to modify certain entries are the :ref:`community.routeros.api_modify <ansible_collections.community.routeros.api_modify_module>` and :ref:`community.routeros.api_find_and_modify <ansible_collections.community.routeros.api_find_and_modify_module>` modules. The :ref:`community.routeros.api_info module <ansible_collections.community.routeros.api_info_module>` allows to retrieve information on specific predefined paths that can be used as input for the ``community.routeros.api_modify`` module, and the :ref:`community.routeros.api_facts module <ansible_collections.community.routeros.api_facts_module>` allows to retrieve Ansible facts using the RouterOS API.
+
+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``:
+
+.. code-block:: yaml+jinja
+
+ ---
+ - name: RouterOS test with API
+ hosts: localhost
+ gather_facts: false
+ vars:
+ hostname: 192.168.1.1
+ username: admin
+ password: test1234
+ tasks:
+ - name: Get "ip address print"
+ community.routeros.api:
+ hostname: "{{ hostname }}"
+ password: "{{ password }}"
+ username: "{{ username }}"
+ path: "ip address"
+ # The following options configure TLS/SSL.
+ # Depending on your setup, these options need different values:
+ tls: true
+ validate_certs: true
+ validate_cert_hostname: true
+ # If you are using your own PKI, specify the path to your CA certificate here:
+ # ca_path: /path/to/ca-certificate.pem
+ register: print_path
+
+ - name: Show IP address of first interface
+ ansible.builtin.debug:
+ msg: "{{ print_path.msg[0].address }}"
+
+This results in the following output:
+
+.. code-block:: ansible-output
+
+ PLAY [RouterOS test] *********************************************************************************************
+
+ TASK [Get "ip address print"] ************************************************************************************
+ ok: [localhost]
+
+ TASK [Show IP address of first interface] ************************************************************************
+ ok: [localhost] => {
+ "msg": "192.168.2.1/24"
+ }
+
+ PLAY RECAP *******************************************************************************************************
+ localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
+
+Check out the documenation of the :ref:`community.routeros.api module <ansible_collections.community.routeros.api_module>` for details on the options.
+
+Using the ``community.routeros.api`` module defaults group
+----------------------------------------------------------
+
+To avoid having to specify common parameters for all the API based modules in every task, you can use the ``community.routeros.api`` module defaults group:
+
+.. code-block:: yaml+jinja
+
+ ---
+ - name: RouterOS test with API
+ hosts: localhost
+ gather_facts: false
+ module_defaults:
+ group/community.routeros.api
+ hostname: 192.168.1.1
+ password: admin
+ username: test1234
+ # The following options configure TLS/SSL.
+ # Depending on your setup, these options need different values:
+ tls: true
+ validate_certs: true
+ validate_cert_hostname: true
+ # If you are using your own PKI, specify the path to your CA certificate here:
+ # ca_path: /path/to/ca-certificate.pem
+ tasks:
+ - name: Gather facts"
+ community.routeros.api_facts:
+
+ - name: Get "ip address print"
+ community.routeros.api:
+ path: "ip address"
+
+ - name: Change IP address to 192.168.1.1 for interface bridge
+ community.routeros.api_find_and_modify:
+ path: ip address
+ find:
+ interface: bridge
+ values:
+ address: "192.168.1.1/24"
+
+Here all three tasks will use the options set for the module defaults group.
+
+Setting up encryption
+---------------------
+
+It is recommended to always use ``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:
+
+:force_no_cert: Setting to ``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 ``false``.
+:validate_certs: Setting to ``false`` disables any certificate validation. **This is discouraged to use in production**, but is needed when setting the device up. The default value is ``true``.
+:validate_cert_hostname: Setting to ``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 ``true``; the default ``false`` is chosen for backwards compatibility to an older version of the module.
+:ca_path: If you are not using a commerically 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.
+
+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.
+
+Setting up a PKI
+^^^^^^^^^^^^^^^^
+
+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.
+
+Installing a certificate on a MikroTik router
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+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.
+
+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 :ref:`community.crypto.openssl_pkcs12 <ansible_collections.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.
+
+.. code-block:: yaml+jinja
+
+ ---
+ - name: Install certificates on devices
+ hosts: routers
+ gather_facts: false
+ tasks:
+ - block:
+ - set_fact:
+ random_password: "{{ lookup('community.general.random_string', length=32, override_all='0123456789abcdefghijklmnopqrstuvwxyz') }}"
+
+ - name: Create PKCS#12 bundle
+ openssl_pkcs12:
+ path: keys/{{ inventory_hostname }}.p12
+ certificate_path: keys/{{ inventory_hostname }}.pem
+ privatekey_path: keys/{{ inventory_hostname }}.key
+ friendly_name: '{{ inventory_hostname }}'
+ passphrase: "{{ random_password }}"
+ mode: "0600"
+ changed_when: false
+ delegate_to: localhost
+
+ - name: Copy router certificate onto router
+ ansible.netcommon.net_put:
+ src: 'keys/{{ inventory_hostname }}.p12'
+ dest: '{{ inventory_hostname }}.p12'
+
+ - name: Install router certificate and clean up
+ community.routeros.command:
+ commands:
+ # Import certificate:
+ - /certificate import name={{ inventory_hostname }} file-name={{ inventory_hostname }}.p12 passphrase="{{ random_password }}"
+ # Remove PKCS12 bundle:
+ - /file remove {{ inventory_hostname }}.p12
+ # Show certificates
+ - /certificate print
+ register: output
+
+ - name: Show result of certificate import
+ debug:
+ var: output.stdout_lines[0]
+
+ - name: Show certificates
+ debug:
+ var: output.stdout_lines[2]
+
+ always:
+ - name: Wipe PKCS12 bundle
+ command: wipe keys/{{ inventory_hostname }}.p12
+ changed_when: false
+ delegate_to: localhost
+
+ - name: Use certificate
+ community.routeros.command:
+ commands:
+ - /ip service set www-ssl address={{ admin_network }} certificate={{ inventory_hostname }} disabled=no tls-version=only-1.2
+ - /ip service set api-ssl address={{ admin_network }} certificate={{ inventory_hostname }} tls-version=only-1.2
+
+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``.
+
+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 :ref:`community.routeros.api module <ansible_collections.community.routeros.api_module>` module with TLS and certificate validation enabled:
+
+.. code-block:: yaml+jinja
+
+ - community.routeros.api:
+ ...
+ tls: true
+ validate_certs: true
+ validate_cert_hostname: true
+ ca_path: /path/to/ca-certificate.pem
diff --git a/ansible_collections/community/routeros/docs/docsite/rst/quoting.rst b/ansible_collections/community/routeros/docs/docsite/rst/quoting.rst
new file mode 100644
index 00000000..3091fc85
--- /dev/null
+++ b/ansible_collections/community/routeros/docs/docsite/rst/quoting.rst
@@ -0,0 +1,19 @@
+..
+ Copyright (c) Ansible Project
+ GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+ SPDX-License-Identifier: GPL-3.0-or-later
+
+.. _ansible_collections.community.routeros.docsite.quoting:
+
+How to quote and unquote commands and arguments
+===============================================
+
+When using the :ref:`community.routeros.command module <ansible_collections.community.routeros.command_module>` or the :ref:`community.routeros.api module <ansible_collections.community.routeros.api_module>` modules, you need to pass text data in quoted form. While in some cases quoting is not needed (when passing IP addresses or names without spaces, for example), in other cases it is required, like when passing a comment which contains a space.
+
+The community.routeros collection provides a set of Jinja2 filter plugins which helps you with these tasks:
+
+- The :ref:`community.routeros.quote_argument_value filter <ansible_collections.community.routeros.quote_argument_value_filter>` quotes an argument value: ``'this is a "comment"' | community.routeros.quote_argument_value == '"this is a \\"comment\\""'``.
+- The :ref:`community.routeros.quote_argument filter <ansible_collections.community.routeros.quote_argument_filter>` quotes an argument with or without a value: ``'comment=this is a "comment"' | community.routeros.quote_argument == 'comment="this is a \\"comment\\""'``.
+- The :ref:`community.routeros.join filter <ansible_collections.community.routeros.join_filter>` quotes a list of arguments and joins them to one string: ``['foo=bar', 'comment=foo is bar'] | community.routeros.join == 'foo=bar comment="foo is bar"'``.
+- The :ref:`community.routeros.split filter <ansible_collections.community.routeros.split_filter>` splits a command into a list of arguments (with or without values): ``'foo=bar comment="foo is bar"' | community.routeros.split == ['foo=bar', 'comment=foo is bar']``
+- The :ref:`community.routeros.list_to_dict filter <ansible_collections.community.routeros.list_to_dict_filter>` splits a list of arguments with values into a dictionary: ``['foo=bar', 'comment=foo is bar'] | community.routeros.list_to_dict == {'foo': 'bar', 'comment': 'foo is bar'}``. It has two optional arguments: ``require_assignment`` (default value ``true``) allows to accept arguments without values when set to ``false``; and ``skip_empty_values`` (default value ``false``) allows to skip arguments whose value is empty.
diff --git a/ansible_collections/community/routeros/docs/docsite/rst/ssh-guide.rst b/ansible_collections/community/routeros/docs/docsite/rst/ssh-guide.rst
new file mode 100644
index 00000000..bdbdbfe8
--- /dev/null
+++ b/ansible_collections/community/routeros/docs/docsite/rst/ssh-guide.rst
@@ -0,0 +1,127 @@
+..
+ Copyright (c) Ansible Project
+ GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+ SPDX-License-Identifier: GPL-3.0-or-later
+
+.. _ansible_collections.community.routeros.docsite.ssh-guide:
+
+How to connect to RouterOS devices with SSH
+===========================================
+
+The collection offers two modules to connect to RouterOS devies with SSH:
+
+- The :ref:`community.routeros.facts module <ansible_collections.community.routeros.facts_module>` gathers facts about a RouterOS device;
+- The :ref:`community.routeros.command module <ansible_collections.community.routeros.command_module>` executes commands on a RouterOS device.
+
+The modules need the :ref:`ansible.netcommon.network_cli connection plugin <ansible_collections.ansible.netcommon.network_cli_connection>` for this.
+
+Important notes
+---------------
+
+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.
+
+2. The :ref:`community.routeros.command module <ansible_collections.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:
+
+ .. code-block:: yaml+jinja
+
+ - community.routeros.command:
+ commands:
+ - /ip
+ - print
+
+3. When using the :ref:`community.routeros.command module <ansible_collections.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>`__).
+
+4. Finally, the :ref:`ansible.netcommon.network_cli connection plugin <ansible_collections.ansible.netcommon.network_cli_connection>` uses `paramiko <https://pypi.org/project/paramiko/>`_ by default to connect to devices with SSH. You can set its ``ssh_type`` option to ``libssh`` to use `ansible-pylibssh <https://pypi.org/project/ansible-pylibssh/>`_ instead, which offers Python bindings to libssh. See its documentation for details.
+
+Setting up an inventory
+-----------------------
+
+An example inventory ``hosts`` file for a RouterOS device is as follows:
+
+.. code-block:: ini
+
+ [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
+
+This tells Ansible that you have a RouterOS device called ``router`` with IP ``192.168.2.1``. Ansible should use the :ref:`ansible.netcommon.network_cli connection plugin <ansible_collections.ansible.netcommon.network_cli_connection>` together with the the :ref:`community.routeros.routeros cliconf plugin <ansible_collections.community.routeros.routeros_cliconf>`. The credentials are stored as ``ansible_user`` and ``ansible_ssh_pass`` in the inventory.
+
+Connecting to the device
+------------------------
+
+With the above inventory, you can use the following playbook to execute ``/system resource print`` on the device
+
+.. code-block:: yaml+jinja
+
+ ---
+ - 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] }}"
+
+This results in the following output:
+
+.. code-block:: ansible-output
+
+ 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