QuestionQ89

VerifiedController-Based Network Automation

An engineer needs to authenticate to Cisco SD-WAN vManage and export the current device inventory with Ansible. The engineer must sign in, retrieve the XSRF token, query the inventory endpoint, and save the JSON to disk. Drag the code snippets into the boxes to complete the artifact. Not every option is used.

Drag & Drop
- name: Login to vManage and collect inventory
  : localhost
  gather_facts: false
  vars:
    vmanage_host: 198.51.100.10
    username: netops
    password: C1sco12345
    base: "https://{{vmanage_host}}:8443"

  :
  - name: Authenticate to vManage
    :
      : "{{base}}/j_security_check"
      method: POST
      headers:
        Content-Type: "application/x-www-form-urlencoded"
      body: "j_username={{username}}&j_password={{password}}"
      status_code: 200
      validate_certs: false
      register: login

  - name: Get XSRF token
    uri:
      url: "{{base}}/dataservice/client/token"
      method: GET
      headers:
        Cookie: "{{ login.set_cookie }}"
      return_content: true
      validate_certs: false
      register: token

  - name: Fetch device inventory
    uri:
      url: "{{base}}/dataservice/device"
      method: GET
      headers:
        Cookie: "{{ login.set_cookie }}"
        X-XSRF-TOKEN: "{{ token.content }}"
      return_content: true
      validate_certs: false
      register: inventory

  - name: Save inventory to file
    copy:
      dest: "inventory.json"
      content: "{{ inventory.content | to_nice_json }}"
Explanation

An Ansible play uses hosts to identify the target and tasks to contain the task list. The built-in ansible.builtin.uri module performs HTTP requests, and its url parameter identifies each vManage endpoint. The subsequent requests preserve the login cookie and supply the retrieved XSRF token for the inventory request.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!