Trouble writing to Bucket with Curl in Ansible play

I’m running the latest InfluxDB trying to use the Curl command from within an Ansible play. It works fine as long as I use hard coded values as such:

curl --request POST “myURLapi/v2/write?org=myOrg&bucket=myBucket&precision=ms” --header “Authorization: Token myToken” --data-binary “imageTransfer,source=myHost,fileName=myFile.txt startTime=1615468803714,endTime =1615468804714 1615468804714”

However, as soon as I Introduce Ansible variables in the data like this, it fails

curl --request POST “myURL/api/v2/write?org=myOrg&bucket=myBucket&precision=ms” --header “Authorization: Token myToken” --data-binary “imageTransfer,source={{ ansible_facts.hostname }},fileName={{ item }} processStart={{ startTime.stdout }},processEnd={{ endTime.stdout }} {{ endTime.stdout }}”

Oddly enough, it seems it the first instance of a space that is the trouble since the error from Ansible reads like this:

FAILED! => {“reason”: "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n mapping values are not allowed in this context\n\nThe error appears to be in roles/image-transfer/tasks/processFile.yml’: line 27, column 136, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- name: post to db\n command: curl --request POST “myURL/api/v2/write?org=myOrg&bucket=myBucket&precision=ms” --header “Authorization: Token myToken " --data-binary “imageTransfer,source={{ ansible_facts.hostname }},fileName={{ item }} processStart={{ startTime.stdout }},processEnd={{ endTime.stdout }} {{ endTime.stdout }}”\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - “{{ foo }}”\n”}

Thoughts anyone? I have spent two days trying to find a work around and I’m out of ideas.

How do you use curl in Ansible?
With the shell or the uri module?

Please post your full curl request here again in markdown format so that it is formatted correctly:

```shell
put the curl request here
```

Alternatively, please post the Ansible task here that is responsible for the curl request:

```yaml
put the ansible task here
```

Also, please post the contents of the --data-binary blob as it is populated by Ansible:

```txt
put the payload blob here
```

This can be done in Ansible with a debug task:

- name: Print debug output
  debug:
    msg: "imageTransfer,source={{ ansible_facts.hostname }},fileName={{ item }} processStart={{ startTime.stdout }},processEnd={{ endTime.stdout }} {{ endTime.stdout }}"

I have yet to try the uri module - i will try that next - thanks for the suggestion.

Here is the output of the debug task

TASK [image-transfer : Print debug output] ****************************************************************************************************************************************************************************************
ok: [host1] => {
“msg”: “imageTransfer,source=host1,fileName=myFile.txt processStart=1615552047414,processEnd=1615552049047 1615552049047”

Here are the two ways I have tried this - using the shell and command modules

   - name: post to db
     command: curl --request POST "http://xxx.xx.xx.xx:8086/api/v2/write?org=Demo&bucket=Dimensioner&precision=ms" --header "Authorization: Token myToken" --data-binary "imageTransfer,source={{ ansible_facts.hostname }},fileName={{ item }} processStart={{ startTime.stdout }},processEnd={{ endTime.stdout }} {{ endTime.stdout }}"

- name: post to db
  shell: curl --request POST "http://xxx.xx.xx.xx:8086/api/v2/write?org=Demo&bucket=Dimensioner&precision=ms" --header "Authorization: Token myToken" --data-binary '"imageTransfer,source="{{ ansible_facts.hostname }}",fileName="{{ item }}" processStart="{{ startTime.stdout }}",processEnd="{{ endTime.stdout }}" {{ endTime.stdout }}"'

Solved: While I’d still like to know from al learning perspective what the issue was with the curl format, I have a working task thanks to the suggestion of using the uri module. I posted the working version here for anyone else who finds themselves in my position.

- name: post to db
  uri:
    url: http://xxx.xx.xx.xx:8086/api/v2/write?org=Demo&bucket=Dimensioner&precision=ms
    method: POST
    headers:
      Authorization: Token xxx.xx.xx.xx
    body: imageTransfer,source={{ ansible_facts.hostname }},fileName={{ item }} processStart={{ startTime.stdout }},processEnd={{ endTime.stdout }} {{ endTime.stdout }}
    body_format: raw
    status_code: 204
1 Like