Community discussions

MikroTik App
 
murrayis
just joined
Topic Author
Posts: 21
Joined: Tue Sep 29, 2020 11:57 pm

v7 Rest API - ANSIBLE - PUT/Post

Thu Nov 03, 2022 8:08 am

Hi All,

I am trying to add a couple of v7 devices into Ansible and as the standard commands appear to fail with v7 I'm looking to replicate the following playbook tasks into a Rest API query.

Task 1:
   - name: Adding VLAN to parent interface
     routeros_command:
      commands:
       - /interface/vlan/add vlan-id=123 name=123 interface=[/interface/get [/interface/find comment~"{{HE_FNN}}"] name]
Task 2:
   - name: Adding IP address to parent VLAN
     routeros_command:
      commands:
       - /ip address add address $Svc_IP interface=$vlan_id comment='Cust: {{Cust_Name}} ['{{Svc_BW}}'] ('{{Svc_Car}}') {'{{Svc_ID}}'}'
The above examples do not contain working variables; that's another issue however the concept is all the matters.

Looking through the limited documentation of the REST API i'm striking out in trying to complete the first task of creating a VLAN without searching for a specific interface based on a unique identifier in the comment.

Playbook:
---
- hosts: all
  connection: network_cli
  gather_facts: false

  vars:
   ansible_network_os: routeros

  tasks:
    - name: Adding VLAN to parent interface
      uri:
        url: "{{item.url}}"
        method: PUT
        url_username: username
        url_password: password_is_secure_goes_brrr
        force_basic_auth: yes
        validate_certs: false
        body_format: json
        body: {}
      register: results
      loop:
        - {url: 'https://X.X.X.X/rest/interface/vlan', body: '{vlan-id: "69",name: "69",interface: "sfp-sfpplus8"}' }
Error:
    "item": {
        "body": "{vlan-id: \"69\",name: \"69\",interface: \"sfp-sfpplus8\"}",
        "url": "https://X.X.X.X/rest/interface/vlan"
    },
    "json": {
        "detail": "failure: vlan-id bad",
        "error": 400,
        "message": "Bad Request"
    },
    "msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request",
    "redirected": false,
    "status": 400,
    "url": "https://X.X.X.X/rest/interface/vlan",
    "x_frame_options": "sameorigin"
Any assistance is greatly appreciated.
 
murrayis
just joined
Topic Author
Posts: 21
Joined: Tue Sep 29, 2020 11:57 pm

Re: v7 Rest API - ANSIBLE - PUT/Post

Fri Nov 04, 2022 12:55 am

Quick Update:

I managed to push through and get most of the requests working however I am stuck now on the
.query
function and how to use it with regex?

Working Interface Creation:
    - name: Adding VLAN to parent interface
      uri:
        url: "{{item.url}}"
        method: POST
        url_username: user
        url_password: password
        force_basic_auth: yes
        validate_certs: false
        body_format: json
        body: "{{item.body}}"
        status_code: 200

      register: results
      loop:
        - {url: 'https://x.x.x.x/rest/interface/vlan/add', body: '{"interface":"sfp-sfpplus8","name":"69","vlan-id":"69","comment":"Cust: {{Cust_Name}} [{{Svc_BW}}] ({{Svc_Carrier}}) { {{Svc_FNN}} } "}' }
        - {url: 'https://x.x.x.x/rest/ip/address/add', body: '{"interface":"{{Svc_VLAN}}","address":"{{Svc_IP}}","comment":"Cust: {{Cust_Name}} { {{Svc_FNN}} } "}' }
Regex Query Issue:
        - {url: 'https://x.x.x.x/rest/interface/', body: '{".proplist": ["name"], ".query": ["comment~69"]}' }
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: v7 Rest API - ANSIBLE - PUT/Post

Fri Nov 04, 2022 10:13 am

~ in api is not valid, you cannot use regexps that way.
Any parsing or filtering with regexps must be done on the APP side.
 
murrayis
just joined
Topic Author
Posts: 21
Joined: Tue Sep 29, 2020 11:57 pm

Re: v7 Rest API - ANSIBLE - PUT/Post

Tue Nov 08, 2022 12:55 am

Thanks for the confirmation MRZ.
 
colin
Frequent Visitor
Frequent Visitor
Posts: 74
Joined: Mon May 11, 2015 11:11 am

Re: v7 Rest API - ANSIBLE - PUT/Post

Thu Aug 24, 2023 1:04 pm

~ in api is not valid, you cannot use regexps that way.
Any parsing or filtering with regexps must be done on the APP side.
rest api still doesn't support regex query, right?

Who is online

Users browsing this forum: wirelesslywired and 15 guests