-1

I tried to find a way to print both an API request and response to a log file. But Ansible URI module does not provide the request data.

Does anyone know any mechanism to print the request and response to file in Ansible?

example:

 name: update cms
  uri:
    url:  '{{ cms_api.msg }}'
    method: POST
    body_format: json
    body:
      api_password: "{{ api_password  }}"
      name: "{{ item.Name }}"
      ip_address: "{{ item.IPAddress }}"
      subnet_mask: "{{ item.SubnetMask }}"
      router: "{{ item.Router }}"
      ip_range: "{{ item.IPAddresses }}"
      options: "{{ item.Options }}"
      pool_name: "{{ item.PoolName}}" 
    validate_certs: no
    return_content: yes
  register: dhcp_subnet_response
  loop: "{{ ip_addrs_1.list }}"
  delegate_to: localhost

I need to print above request and response both to a file. Since this api calling as a loop, I need to print request|response both for each request.

gebris
  • 11
  • 3
  • 2
    `I tried to find a way to print api request and response...`. please [edit] your question and add your code. Describe the exact problem you're having. Before you proceed, you'll want to read [ask] paying a particular attention to the [mre] section. – Zeitounator Oct 18 '22 at 06:16
  • Even after an update it is still a requirement, but no description or question about what or where is a problem. – U880D Oct 19 '22 at 06:17

1 Answers1

0

Since your post contains several different questions you may have a look into the general documentation first

After reading and regarding

But Ansible URI module not providing request data.

you may then have a look into the following example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Get response with 'uri' module
    uri:
      url: "https://example.com"
      method: GET
      body_format: raw     # default
      return_content: true # default 'false'
      status_code: 200
    register: result

  - name: Show result
    debug:
      var: result

resulting into the content of https://example.com, an IANA-managed Reserved Domain.

Does anyone know any mechanism to print ...

The page content itself is accessible via

  - name: Show content
    debug:
      msg: "{{ result.content }}"

print ... to file in Ansible?

and you can Write variable to a file in Ansible or Ansible - Save registered variable to file.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 1
    Yes. I know and with this example, you may able to get only the response data. My requirement is to print request and response both in to file. – gebris Oct 19 '22 at 05:16
  • That might be your requirement, but what is your question? With the information provided all necessary parts are available to achieve that. – U880D Oct 19 '22 at 05:18
  • I updated my question with code. I just need to print request data first and response data for perticular request. – gebris Oct 19 '22 at 05:29