Complex api json calls

18 views
Skip to first unread message

Nicholas Britton

unread,
May 8, 2019, 5:49:56 PM5/8/19
to Ansible Project
I am looking to use the uri module and have been sucessfull with simpler calls, but i am unsure how to get something like the following to work correctly.    I have been provided what the json should look like but i am unsure how to build that out via the uri module in the body syntax.

I have also struggled to find how ansible handles nested levels in the body.  from examples it looks like its just the indents.   But i am not sure, if someone can help shed some light for me it would be much appreciated.  

Here is what the json should look like when sent as the payload:

POST https://fsunix005.pne.ven.veritas.com/netbackup/config/snapshotproviders/configuredplugins/aws/instances

 

{

 
"data": {

   
"id": "someid",    

   
"type": "plugininstance",    

   
"attributes": {

     
"configurationAttributes": [

       
{

         
"name": "accesskey",  

         
"singlevalue": "key1"  

       
},

       
{

         
"name": "secretkey",  

         
"singlevalue": "skey2"  

       
},

       
{

         
"name": "regions",  

         
"multivalue": [

           
"us-east-1",  

           
"us-east-2",

           
"us-west-1",

           
"us-west-2"

         
]

       
}

     
]

   
}

 
}

}


Matt Martz

unread,
May 8, 2019, 5:55:18 PM5/8/19
to ansible...@googlegroups.com
The `body` is typically easiest represented as a YAML representation of the JSON data format.  So in your case, this should be what you need:

- uri:
    method: POST
    body_format: json
    body:
      data:
        attributes:
          configurationAttributes:
            - name: accesskey
              singlevalue: key1
            - name: secretkey
              singlevalue: skey2
            - multivalue:
                - us-east-1
                - us-east-2
                - us-west-1
                - us-west-2
              name: regions
        id: someid
        type: plugininstance

--
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/dcea4d04-b6c7-4db9-98e7-b5d13c30b908%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Nicholas Britton

unread,
May 8, 2019, 6:46:50 PM5/8/19
to ansible...@googlegroups.com
So if the multivalue is driven from user input or an input file and will vary in what regions are to be used, how can that be dynamic?

Kai Stian Olstad

unread,
May 13, 2019, 6:09:36 PM5/13/19
to ansible...@googlegroups.com
On 09.05.2019 00:46, Nicholas Britton wrote:
> So if the multivalue is driven from user input or an input file and will
> vary in what regions are to be used, how can that be dynamic?

If you look in the documentation you'll see that you can use json in body by setting body_format.

File:
body: "{{ lookup('file', '/path/to/json/file.json' }}"

Variable
body: "{{ myvar.json }}"


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages