registering variables from stdout using regex_search

54 Views Asked by At

I have followed numerous related thread on this subject, and am likely close, but not yet.

Sending URI GET to Infoblox IPAM tool to look up IP Address for deletion. IPAM tool sends back a reference object code used to make a put call back to delete. I have to snag that code from stdout and put into the PUT URL to complete the process.

I register the 1st GET response in register: api_response_raw. The regex was validated.

I have escaped characters in the regex, changed quotes from single to double, checked my register and set_facts variables to no avail.

tasks:
  - name: Get Iblox IPAddr Reference Object via RESTful API
    uri :
      url: https://iblox/wapi/v2.2/ipv4address?ip_address={{ vpn_source_public_ipaddr }}
      method: GET
      user: svc_Network_Automation
      password: 6d8@Vv
      return_content: yes
      Header_Content-Type: "application/json"
      body_format: json
      validate_certs: no
    register: api_response_raw

  - name: Extract _Ref Object from API Response
    set_fact:
      api_ref_object: "{{ api_response_raw.stdout | regex_search('record:host/(\w*):(.{1,})%20') }}"

  - name: show _ref object
    debug: var=api_ref_object

  - name: Delete Iblox IPAddr Reference Object via RESTful API
    uri :
      url: https://iblox/wapi/v2.2/ipv4address?ip_address={{ api_ref_object }}
      method: PUT
      user: svc_Network_Automation
      password: 6d8@Vv
      return_content: yes
      Header_Content-Type: "application/json"
      body_format: json
      validate_certs: no

I get different errors depending on what little changes I try: 1)Unexpected templating error occurred...expected string or buffer 2)...Looks like it might be an issue with missing quotes...

It fails on the "Extract _Ref Object" task.

I need to get this line from stdout to the PUT URL: record:host/ZG5zLmhvc3QkLm5vbl9ETlNfaG9zdF9yb290LjAuMTU3MTM0NTE2MTE2OS41Ny43LjIxMy5vY2ktcGVvcGxlc29mdC12cG4tMTI5:oci-peoplesoft-vpn-159.211.17.67/%20

1

There are 1 best solutions below

0
RobWieters On

I discovered that the values weren't in stdout; therefore I couldn't capture the value.

Instead of api_response_raw.stdout, I used api_response_raw.content, because the output in debug showed my values in a key:value format... the values being of the key called content.

  - name: Extract _Ref Object from API Response
    set_fact:
      api_ref_object: "{{ api_response_raw.content | regex_search('record:host/(\w*):(.{1,})%20') }}"