I am trying to upload a CSV file to a Record in an Incident Table of ServiceNow through Ansible URI module. The CSV file is in the Ansible Controller from where the playbook is getting executed. Everytime, different types of error are thrown.

Below is the code.

        - block:
            - name: "Attach output CSV file to incident {{ new_incident['json']['result']['number'] }}"
              ansible.builtin.uri:
                      url: "{{ SN_HOST }}api/now/attachment/file/?table_name=incident&table_sys_id={{ new_incident['json']['result']['sys_id'] }}&file_name={{ csv_filename }}"
                      user: "{{ SN_USERNAME }}"
                      password: "{{ SN_PASSWORD }}"
                      method: POST
                      return_content: false
                      validate_certs: false
                      force_basic_auth: yes
                      status_code: 201
                      src: "../../win_playbooks/reports/{{csv_filename}}"
                      headers:
                          Accept: '*/*' 
                          Content-Type: '*/*'
                      mode: '0755'   
              register: attach_file

            - debug:
                msg: 
                    - "File name uploaded is: {{ attach_file.json.result.file_name }}"
                    - "Uploaded file can be downloaded from this URL at {{ attach_file.json.result.download_link}}" 
          tags: attach_file
          delegate_to: localhost
          run_once: yes

I have changed the body_format to JSON and vice versa with multipart. I have used headers as above and removed the headers section also.

I got this error

    },
    "json": {
        "error": {
            "detail": null,
            "message": "Requested URI does not represent any resource"
        },
        "status": "failure"
    },
    "msg": "Status code was 400 and not [201]: HTTP Error 400: Bad Request",

Based on the error message, I had tried various body_format options but nothing seems to work here. Need help on this...

1

There are 1 best solutions below

2
Vijayanand A On

I finally found this piece which solved my problem. The code I used is:

                      url: "{{ SN_HOST }}api/now/attachment/upload" 
                      user: "{{ SN_USERNAME }}"
                      password: "{{ SN_PASSWORD }}"
                      method: POST
                      return_content: true
                      validate_certs: false
                      force_basic_auth: no
                      status_code: 201
                      body_format: form-multipart
                      headers:
                          Accept: 'application/json'
                          Content-Type: 'multipart/form-data'
                      body:
                        table_name:
                            content: incident
                        table_sys_id:
                            content: " {{ new_incident['json']['result']['sys_id'] }} "
                        uploadFile:
                            filename: "../../win_playbooks/reports/{{ csv_filename }}"