uri_module - json post

172 views
Skip to first unread message

Darvin Denmian

unread,
Jun 22, 2015, 4:45:41 PM6/22/15
to ansible...@googlegroups.com
Hi guys,

this is the first time I ever use the "uri_module" and I'm facing a problem that I just can't solve.. here it is

    - name: Sending Stash to Sensu - {{ item }}
      uri:
        method=POST
        body_format=json
        body="{"path": "silence/{{ item }}","content": {"reason": "Automatic Scale Down"},"expire": 28800}"
        HEADER_Content-Type="application/json;charset=utf-8"
        status_code=200
      with_items:
        - c3_imagens_name
      tags:
        - stash

When I try to execute this piece of code I got the following error:

ERROR: Syntax Error while loading YAML script, ec2_rotate_imagens.yml
Note: The error may actually appear before this position: line 76, column 22

        body_format=json
        body="{"path": "silence/{{ item }}","content": {"reason": "Automatic Scale Down"},"expire": 28800}"
                     ^
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

I'm trying to POST something like this (using curl):

/usr/bin/curl -XPOST 'http://10.0.0.8:4567/stashes' -d '{
       "path": "silence/ip-10-0-4-217",
       "content": {
           "reason": "Automatic Scale Down"
       },
       "expire": 28800
}'


Could you please guys give me some help?

Thanks in advance 


Rob White

unread,
Jun 22, 2015, 7:44:04 PM6/22/15
to ansible...@googlegroups.com
Escape your double quotes

body="{\"path\": \"silence/{{ item }}\",\"content\": {\"reason\": \"Automatic Scale Down\"},\"expire\": 28800}"

Erick Vitor de Barros

unread,
Jun 23, 2015, 8:44:44 AM6/23/15
to ansible...@googlegroups.com
Hi Rob,

thanks for your reply. I'll try that.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/652cd914-f243-4007-998c-fbef23f831c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erick Vitor de Barros

unread,
Jun 26, 2015, 1:30:43 PM6/26/15
to ansible...@googlegroups.com
Hi guys, 

I solved the issue with the following adjustments:

    - name: Sending Stash to Sensu - {{ item }}
      uri:
        method=POST
        body_format=json
        body='{"path":"silence/{{ item }}","content":{"reason":"Automatic Scale Down"},"expire":28800}'
        status_code=201
        HEADER_Content-Type="application/json"
      with_items: c3_imagens_name
      tags:
        - stash

Thanks !

Barry Kaplan

unread,
Jun 26, 2015, 9:00:26 PM6/26/15
to ansible...@googlegroups.com
I simply copied the 2.0 uir.py into my ./modules. It supports body_format=json which will convert the body dict into json as well as set the appropriate headers.

Indeed I have done this for several modules that are scheduled for 2.0 or are pending PRs. Ansible is incredibly flexible in this manner. Just need be sure to clean up the hacks when the modules are actually released.

Brian Coca

unread,
Jun 30, 2015, 12:30:11 AM6/30/15
to ansible...@googlegroups.com
that will work for most modules, but some do depend on features only
in the version they were merged, so this will work most of the time
but not 100%.


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages