I am trying to post a variable that gets defined during my playbook as json 
to a webservice. Unfortunately I have not been able to get the URI module 
to post valid json. 

    - name: lookup act_transaction in database
      mongo_query: >
        host={{ams_ip}} user=user password={{pass}}
        database=db collection=transactions
        filter='{}' projection='{"_id":True}'
        sort='start_time' direction=-1 limit=1
      register: query

    - name: login to gui
      run_once: yes
      uri:
        url: "https://{{ ip }}/login"
        method: POST
        follow_redirects: all
        body: "username={{amg_username}}&password={{amg_password}}&next=/"
        HEADER_Origin: "https://{{ ams_ip }}"
        HEADER_Content-Type: application/x-www-form-urlencoded
      register: cookie

    - name: create request body
      set_fact:
        req:
          _id: "{{ query.results[0]._id['$oid'] }}"
          guids: [ "{{ guid }}" ]

    - debug: var=genconfig_req
    - debug: msg="{{ genconfig_req }}"

    - name: generate config
      when: ams_deploy and do_act
      uri: >
        url="https://{{ ams_ip }}/generate_config"
        method=POST
        body="{{ req|to_nice_json }}"
        follow_redirects=all
        HEADER_Origin="https://{{ ams_ip }}"
        HEADER_Content-Type=application/json
        HEADER_Cookie="{{ cookie.set_cookie }}"
 


when this request is made to the webserver, the webserver logs an error for 
invalid json ( SyntaxError: Unexpected token _ ). I have tried every 
combination I can think of to escape our the dictionary as a valid json 
string. I added a debug failure log to the URI module to see what the URI 
module was seeing.:

        if url == "https://192.168.141.42/generate_config": # so we only 
fail on the problem task
            module.fail_json(msg = "type = %s, body = %s" %( type( body ), 
str(body) ))
        resp, content = h.request(url, method=method, body=body, 
headers=headers)

This logs for me 
failed: [192.168.141.120] => {"failed": true}
msg: type = <type 'str'>, body = {_id: 5504a68130600c6b05691b93, guids: 
[0021FA000000000002000000000007D1]}

which i think confirms that the module is just not sending valid json, the 
keys and values should be quoted. I assume the problem has something to do 
with the order the yaml is processed, the json filter gets applied before 
the playbook is analyzed, and ansible maybe sees the json data as more yaml 
so it gets loaded as such. I'm just guessing.

Interesting when I used the yaml format ( body: "{{ req }}" ) I was getting 
a dictionary instead of a string. I have tried decomposing the variable ( 
both yaml and inline format ) so:
body='{ "_id": "{{ req._id }}", "guids": [ "{{ guid }}" ] }'

I have tried the filters: string, to_nice_json, and to_json

 About the only thing I haven't tried is writing the variable to a file 
using the copy module with content="{{ req|to_nice_json }}" and then using 
a file lookup like the example shows.

Anybody have any ideas?


-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/17597985-89a8-460d-aee2-7528bab6b5ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to