v7 Rest API - ANSIBLE - PUT/Post

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.

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"]}' }

~ in api is not valid, you cannot use regexps that way.
Any parsing or filtering with regexps must be done on the APP side.

Thanks for the confirmation MRZ.

rest api still doesn’t support regex query, right?