使用regex_search

时间:2019-10-17 23:57:13

标签: ansible-template

我已经关注了许多与此主题相关的话题,并且可能已经结束,但还没有。

将URI GET发送到Infoblox IPAM工具以查找要删除的IP地址。 IPAM工具发回参考对象代码,该代码用于进行回叫删除。我必须从stdout捕获该代码,然后放入PUT URL中以完成该过程。

我在寄存器api_response_raw中注册了第一个GET响应。 正则表达式已通过验证。

我在正则表达式中转义了字符,将引号从单引号更改为双引号,检查了我的寄存器,并且set_facts变量无济于事。

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

根据尝试的小变化,我会得到不同的错误: 1)发生意外的模板错误...预期的字符串或缓冲区 2)...看起来可能是缺少引号的问题...

在“提取_Ref对象”任务上失败。

我需要将此行从stdout转到PUT URL: 记录:host / ZG5zLmhvc3QkLm5vbl9ETlNfaG9zdF9yb290LjAuMTU3MTM0NTE2MTE2OS41Ny43LjIxMy5vY2ktcGVvcGxlc29mdC12cG4tMTI5:oci-peoplesoft-vpn-159.211.17。

1 个答案:

答案 0 :(得分:0)

I learned that the values weren't in stdout, and therefore the value needed couldn't be captured. Instead of api_response_raw.stdout, I used below 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') }}"