cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
116
Views
0
Helpful
1
Replies

Unable to set a tag on a edge in vManage via API

vishesh
Level 1
Level 1

We are trying to configure a TAG to some of the edges in our vManage using API.

 

vishesh_0-1721048302879.png

 

TAGs are already created in vManage and we are just looking to attach a respective TAG to an edge :

 

vishesh_1-1721048302881.png

 

Below is the API that we are trying to use :

 

url: "https://{{ vmanage_host }}:{{ vmanage_port }}/dataservice/device/1.1.1.51/action/tag"

 

Complete playbook looks like below :

 

- name: Attach Device Template to multiple devices on vManage

  hosts: localhost

  gather_facts: false

  vars_files:

    - azure_credentials.yaml

  tasks:

    - name: Login to vManage

      uri:

        url: "https://{{ vmanage_host }}:{{ vmanage_port }}/j_security_check"

        method: POST

        body: "j_username={{ vmanage_username }}&j_password={{ vmanage_password }}"

        status_code: 200

        body_format: form-urlencoded

        validate_certs: no

      register: login_response

 

    - name: Get token for POST tasks

      uri:

        url: "https://{{ vmanage_host }}:{{ vmanage_port }}/dataservice/client/token"

        validate_certs: false

        method: GET

        headers:

          Cookie: "{{ login_response.set_cookie }}"

        return_content: true

      register: login_token



    - name: Tagging request

      uri:

        url: "https://{{ vmanage_host }}:{{ vmanage_port }}/dataservice/device/1.1.1.51/action/tag"

        method: POST

        headers:

          Cookie: "{{ login_response.set_cookie }}"

          X-XSRF-TOKEN: "{{ login_token.content }}"

          Content-Type: application/json

        body:

          tags: Hub

        status_code: 200

        validate_certs: no

      register: tagging_result

      failed_when: tagging_result.status != 200

      ignore_errors: yes

 

    - name: Debug tagging result

      debug:

        var: tagging_result

 

vManage : vmanage-1962156.sdwan.cisco.com

vManage version : 20.12.1

Edge version : 17.12.2

Error received while using the API :

-------------------------------------Q

The full traceback is:

Traceback (most recent call last):

  File "/tmp/ansible_ansible.legacy.uri_payload_pa1ddbmf/ansible_ansible.legacy.uri_payload.zip/ansible/module_utils/urls.py", line 1919, in fetch_url

    r = open_url(url, data=data, headers=headers, method=method,

  File "/tmp/ansible_ansible.legacy.uri_payload_pa1ddbmf/ansible_ansible.legacy.uri_payload.zip/ansible/module_utils/urls.py", line 1686, in open_url

    return Request().open(method, url, data=data, headers=headers, use_proxy=use_proxy,

  File "/tmp/ansible_ansible.legacy.uri_payload_pa1ddbmf/ansible_ansible.legacy.uri_payload.zip/ansible/module_utils/urls.py", line 1578, in open

    r = urllib_request.urlopen(request, None, timeout)

  File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen

    return opener.open(url, data, timeout)

  File "/usr/lib/python3.10/urllib/request.py", line 519, in open

    response = self._open(req, data)

  File "/usr/lib/python3.10/urllib/request.py", line 536, in _open

    result = self._call_chain(self.handle_open, protocol, protocol +

  File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain

    result = func(*args)

  File "/tmp/ansible_ansible.legacy.uri_payload_pa1ddbmf/ansible_ansible.legacy.uri_payload.zip/ansible/module_utils/urls.py", line 605, in https_open

    return self.do_open(self._build_https_connection, req)

  File "/usr/lib/python3.10/urllib/request.py", line 1348, in do_open

    h.request(req.get_method(), req.selector, req.data, headers,

  File "/usr/lib/python3.10/http/client.py", line 1283, in request

    self._send_request(method, url, body, headers, encode_chunked)

  File "/usr/lib/python3.10/http/client.py", line 1329, in _send_request

    self.endheaders(body, encode_chunked=encode_chunked)

  File "/usr/lib/python3.10/http/client.py", line 1278, in endheaders

    self._send_output(message_body, encode_chunked=encode_chunked)

  File "/usr/lib/python3.10/http/client.py", line 1075, in _send_output

    chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \

TypeError: can't concat str to bytes

fatal: [localhost]: FAILED! => {

    "changed": false,

    "elapsed": 0,

    "failed_when_result": true,

    "invocation": {

        "module_args": {

            "attributes": null,

            "body": {

                "tags": "Hub"

            },

            "body_format": "raw",

            "ca_path": null,

            "ciphers": null,

            "client_cert": null,

            "client_key": null,

            "creates": null,

            "decompress": true,

            "dest": null,

            "follow_redirects": "safe",

            "force": false,

            "force_basic_auth": false,

            "group": null,

            "headers": {

                "Content-Type": "application/json",

                "Cookie": "JSESSIONID=************aamcD0V49i.f2315f3f-97b6-4fc3-8748-a2d7ae951335; path=/; secure; HttpOnly",

                "X-XSRF-TOKEN": "*************AF9D5671CC1290D6C2761ED7272715E1D667E961F6BCFDE"

            },

            "http_agent": "ansible-httpget",

            "method": "POST",

            "mode": null,

            "owner": null,

            "remote_src": false,

            "removes": null,

            "return_content": false,

            "selevel": null,

            "serole": null,

            "setype": null,

            "seuser": null,

            "src": null,

            "status_code": [

                200

            ],

            "timeout": 30,

            "unix_socket": null,

            "unredirected_headers": [],

            "unsafe_writes": false,

            "url": "https://54.162.33.224:11443/dataservice/device/1.1.1.51/action/tag",

            "url_password": null,

            "url_username": null,

            "use_gssapi": false,

            "use_netrc": true,

            "use_proxy": true,

            "validate_certs": false

        }

    },

    "msg": "Status code was -1 and not [200]: An unknown error occurred: can't concat str to bytes",

    "redirected": false,

    "status": -1,

    "url": "https://54.162.33.224:11443/dataservice/device/1.1.1.51/action/tag"

}

 

-------------------------------------UnQ

 

Cisco API document referred:

https://developer.cisco.com/docs/sdwan/20-12/sd-wan-vmanage-v20-12/

 

vishesh_2-1721048302885.png

 

 

Please assist on the matter

1 Reply 1

@vishesh from your error,  the SD-WAN REST API expects the request body to be in JSON format,  in your playbook, the chunk variable is being encoded as bytes, while the len(chunk):X}\r\n part is a string, this mismatch causes the TypeError.

In such cases, i normal use the developer tools in Chrome and check the console when doing tasks in the UI, you can also use tools such as fiddler which like dev tools can help you troubleshoot HTTP(S) / REST API  Web requests very easily, they allow you to inspect the HTTP requests and responses in the raw format that in which is it sent and received. I found some inconsistency in the API documentation in a few cases and had to test this via the UI and check the responces.

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io