### RouterOS API output example Source: https://github.com/ansible-collections/community.routeros/blob/main/docs/docsite/rst/api-guide.rst Shows the expected output format when running the RouterOS API tasks. ```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 ``` -------------------------------- ### RouterOS command module error example Source: https://github.com/ansible-collections/community.routeros/blob/main/docs/docsite/rst/ssh-guide.rst Demonstrates an incorrect usage of the command module where nested commands are provided. Ensure each command starts with a forward slash. ```yaml - community.routeros.command: commands: - /ip - print ``` -------------------------------- ### Ansible Inventory for RouterOS Source: https://github.com/ansible-collections/community.routeros/blob/main/README.md An example inventory file for connecting to RouterOS devices using Ansible, specifying the network OS and connection type. ```ini [routers] router ansible_host=192.168.1.1 [routers:vars] ansible_connection=ansible.netcommon.network_cli ansible_network_os=community.routeros.routeros ansible_user=admin ansible_ssh_pass=test1234 ``` -------------------------------- ### Ansible Playbook for RouterOS with API Source: https://github.com/ansible-collections/community.routeros/blob/main/README.md An example Ansible playbook for connecting to RouterOS devices via API, including tasks to get IP address information and modify settings. ```yaml --- - name: RouterOS test with API hosts: localhost gather_facts: false vars: hostname: 192.168.1.1 username: admin password: test1234 module_defaults: group/community.routeros.api: hostname: "{{ hostname }}" password: "{{ password }}" username: "{{ username }}" tls: true force_no_cert: false validate_certs: true validate_cert_hostname: true ca_path: /path/to/ca-certificate.pem tasks: - name: Get "ip address print" community.routeros.api: path: ip address register: print_path - name: Print the result ansible.builtin.debug: var: print_path.msg - 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" - name: Retrieve facts community.routeros.api_facts: - ansible.builtin.debug: msg: "First IP address: {{ ansible_net_all_ipv4_addresses[0] }}" ``` -------------------------------- ### Create PKCS#12 Bundle with Openssl Source: https://github.com/ansible-collections/community.routeros/blob/main/docs/docsite/rst/api-guide.rst This task uses the openssl_pkcs12 module to create a PKCS#12 bundle from a certificate and its private key. It requires the certificate and key files to be present locally and generates a random passphrase for protection. This is a prerequisite for installing certificates on MikroTik routers. ```yaml --- - 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 ``` -------------------------------- ### Ansible inventory for RouterOS device Source: https://github.com/ansible-collections/community.routeros/blob/main/docs/docsite/rst/ssh-guide.rst Example inventory file configuration for a RouterOS device. Specifies connection details, network OS, and user credentials. ```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 ``` -------------------------------- ### Ansible Playbook for RouterOS with network_cli Source: https://github.com/ansible-collections/community.routeros/blob/main/README.md A sample Ansible playbook demonstrating how to run commands and retrieve facts from RouterOS devices using the network_cli connection. ```yaml --- - name: RouterOS test with network_cli connection hosts: routers gather_facts: false tasks: - name: Run a command community.routeros.command: commands: - /system resource print register: system_resource_print - name: Print its output ansible.builtin.debug: var: system_resource_print.stdout_lines - name: Retrieve facts community.routeros.facts: - ansible.builtin.debug: msg: "First IP address: {{ ansible_net_all_ipv4_addresses[0] }}" ``` -------------------------------- ### Ansible playbook to gather RouterOS system resources Source: https://github.com/ansible-collections/community.routeros/blob/main/docs/docsite/rst/ssh-guide.rst A playbook that connects to RouterOS devices defined in the inventory and executes '/system resource print' to gather system information. ```yaml --- - 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] }}" ```