How to modify multiple objects with director API

api
#1

Hi everyone,
I’m trying to get myself familiar with the director API , and would really appreciate any help regarding te following usecase.

I want to modify a batch of hosts via the director API; my idea for a fast and easy workflow is:

  1. retrieve a list of all hosts via the API.
  2. modify that list to my needs, e.g. using sed.
  3. PUT that list via the director api.

For example, I export a list with all host objetcs matching the name “shiny”:

curl -u "$credentials" "https://my-icinga.server/icingaweb2/director/hosts" -H 'Accept: application/json' -X GET | jq '.objects[] | select(.object_name | contains("shiny")) ' > shiny_hosts.json

This gives me a file containing two json objects:

{ "address": "1.1.1.1", "display_name": "shiny1", "imports": [ "linux", "shiny" ], "object_name": "shiny1.example.org", "object_type": "object", "vars": { "my": "custom1" } } { "address": "2.2.2.2", "display_name": "shiny2" "imports": [ "linux", "shiny" ], "object_name": "shiny2.example.org", "object_type": "object", "vars": { "my": "custom2" } }

I want to modify the objects to my needs and just PUT them back using the director api.
I tried different combinations of api endpoints ( director/host and director/hosts ) with different json input ( where { objX } refers to the complete host object sa returned by the director API):

  • simple stream of objects like: { obj1 } { obj2}
  • array of objects like: [ {obj1 }, { obj2 } ]
  • object with array of object, mimicing the raw output of a GET call to the director API: `{ “object”: [ { obj1 }, { obj2 } ] }

Nevermind if I PUT or POST call the API, I cannot modify the hosts.
Does anyone know, if it is possible at all to modify a batch of hosts va the director API?
Or would I have to do something else, like making a seperate call for every object?
Would you suggest a better workflow for modifying batches of objects?

Any help is very appreciated!
Cheers,
Xi1pah7a

#2

As a workaround: looping over an array with json-objects and posting that via director api works nice:

# retrieving host objects whose name matches STRING:
curl -u "$icdir_credentials" "http://icinga-server/icingaweb2/director/hosts" -H 'Accept: application/json' -X GET | jq '[ .objects[] | select(.object_name | contains("STRING")) ]' > icinga_hosts.json

… doing some modification inside icinga_hosts.json

# modify hosts via POST request
jq -c '.[]' icinga_hosts.json | while read obj; do curl -u "$icdir_credentials" "http://icinga-server/icingaweb2/director/host?name=$(jq --raw-output '.object_name' <<< $obj)" -i -H 'Accept: application/json' -X POST -d ''"$obj"'' ; done

Nevertheless, I’m interested in your workflows for batch editing via director api.