How to put an array in a json object.

19 views
Skip to first unread message

VenomMsk

unread,
Jul 10, 2017, 12:05:44 PM7/10/17
to Ansible Project, i.ya...@fpcgmbh.de
Hi,

I have a varible that contains an array:

              "AuthenticationHTTPheader": [
                    "iv-user",
                    "iv-groups"
                ],
 

I need to pass this walue as a JSON-element in a body of a Request sent by URI module. So,

        body:
            '{
                ...
                "remote_http_header": "{{ item.AuthenticationHTTPheader|default (d_remhed) }}",
                ...
            }'

in the output I see that URI module sends the following to the server:

                "remote_http_header": "[u'iv-user', u'iv-groups']",

here -> https://stackoverflow.com/questions/41521138/ansible-template-adds-u-to-array-in-template <- I read, that " | to_json" could solve the problem, but in my case using this breaks down the whole json of the body, it's no more a json, but a string with escaped quotes:

..., \"remote_http_header\": \"[\"iv-user\", \"iv-groups\"]\",...

how can I send to the server this array? I need this to be sent to the server:


                "remote_http_header": [ "iv-user", "iv-groups"]


Thanks to everybody!

VenomMsk

unread,
Jul 13, 2017, 5:57:15 AM7/13/17
to Ansible Project, i.ya...@fpcgmbh.de
My solution was:

1. Convert array variable into string:

 {"ansible_facts": {"junction_list": [{"AuthenticationHTTPheader": "[iv_user, iv_groups]", ...

2. Get rid of quotes in a body:


        body:
            '{
                ...
                "remote_http_header": {{ item.AuthenticationHTTPheader|default (d_remhed) }},
                ...
            }'





понедельник, 10 июля 2017 г., 18:05:44 UTC+2 пользователь VenomMsk написал:
Reply all
Reply to author
Forward
0 new messages