Help with REST API

Hi folks,

I’m trying to use Ansible to automate the configuration of new switches on the network. So far I’ve had a pretty good understanding of the resources available for routeros but I’m stuck in how to simulate the “set” cli keyword in the Ansible playbook. I’m testing stuff out using the following Playbook where I’m looking to create the same effect as cli command “interface/bridge/set nameofbridge vlan-filtering=yes”:

---
- name: New switch configuration
  hosts: test_routers
  gather_facts: false
  vars_prompt:
    - name: username
      prompt: "username"
      private: false
    - name: password
      prompt: "password"
  vars:
     ansible_connection: ansible.netcommon.network_cli
     ansible_network_os: community.network.routeros
     ansible_user: '{{ username }}'
     ansible_password: '{{ password }}'
  tasks:
##    - debug:
##        msg: '{{ password }}'
    - name: "Enable vlan filtering on bridge"
      ansible.builtin.uri:
        url: https://192.168.94.1/rest/interface/bridge
        method: PATCH
        url_username: '{{ username }}'
        url_password: '{{ password }}'
        force_basic_auth: yes
        validate_certs: false
        body_format: json
        body: {}
      register: results
      loop:
        - {url: 'https://192.168.94.1/rest/interface/bridge/set/*17', body: '{"vlan-filtering":"yes"}'}
    - debug:
        var: results

For testing purposes I’m trying to enable vlan filtering on a bridge that is already up and running but I cannot get it to work. I’ve tried:

  1. Methods: PUT (creates a new bridge interface and throws an error about failed task), POST (just throws an error and does nothing), PATCH (ansible reports that all is well but the log file just says that the following cli commands were input from api (/interface set bridge; /interface bridge set bridge; /queue interface set bridge). I think PATCH is the one to use for “set” based on what I’ve read but I’m not sure
  2. URL changes in the loop segment of the playbook e.g., https://192.168.94.1/rest/interface/bridge/set/*17 (with interface .id) https://192.168.94.1/rest/interface/bridge (without set cli key word) https://192.168.94.1/rest/interface/bridge/*17 (with interface .id after bridge)

I’m just getting my feet wet with Ansible and I can’t find a lot of resources for Routeros. Please give me a hand :slight_smile: If you need any other info (e.g., ansible errors) please let me know.

Not directly responding to your question, but this maybe an alternative for your Ansible approach: https://registry.terraform.io/providers/terraform-routeros/routeros/latest
At least - if you dont like to use Terraform - you could scan their source-code and see how they implemented “set”.

Configuring network devices using Ansible can be tricky, especially when it involves less common platforms like MikroTik RouterOS. Your approach is correct in using Ansible with the ansible.netcommon.network_cli connection and community.network.routeros modules. However, the REST API of RouterOS can sometimes be non-intuitive. scratch geometry dash


To achieve the desired configuration (interface/bridge/set nameofbridge vlan-filtering=yes), you should focus on using the correct module and URL endpoint. Here’s a more refined version of your playbook:

  • name: New switch configuration
    hosts: test_routers
    gather_facts: false
    vars_prompt:

  • name: username
    prompt: “username”
    private: false

  • name: password
    prompt: “password”
    private: true
    vars:
    ansible_connection: ansible.netcommon.network_cli
    ansible_network_os: community.network.routeros
    ansible_user: ‘{{ username }}’
    ansible_password: ‘{{ password }}’
    tasks:

  • name: Enable vlan filtering on bridge
    community.network.routeros_command:
    commands:

  • /interface bridge set [find name=“nameofbridge”] vlan-filtering=yes
    register: results

  • debug:
    var: results

I will check Terraform out, it seems that it’s better suited for routeros (I can find extensive documentation at least). @inspiringsinger I might take this approach because I can’t seem to find anything extensive about REST APIs on routeros. I actually started out using this approach (this is the traditional API right?) and managed to get it to work (added comments and deleted some interfaces with a playbook). It is indeed way clearer because basically you load modules and just write regular cli commands but I figured I should use something more modern.