Getting info from a REST API service

94 views
Skip to first unread message

Heinz Meier

unread,
Nov 16, 2021, 2:19:50 PM11/16/21
to salt-...@googlegroups.com
Hi,

we use netbox (cool tool!) for DCIM. After defining a virtual machine in netbox I want to use salt to deploy the machine. Therefor I want to retrieve information about the machine (# CPU, #RAM, # Disk, interfaces, IP addresses) from netbox.

On the command line it works:
salt 'admin' http.query https://netbox.local/api/virtualization/virtual-machines/?name=test01 method=GET verify_ssl=False header_dict="{'Authorization': 'Token xxx'}"

How can I use this in a sate file to set variables from the result of that request?

I tried

{% set host = salt['http.query']('https://netbox.local/api/virtualization/virtual-machines/?name=test0101 verify_ssl=False method=GET header_dict="\{''Authorization'': ''
Token XXX''\}"') %}

but it does not work, The error is that verify_ssl is not passed to the salt run that the cert verification fails. In other cases the authorization does not work, Perhaps escaping of the quators is wrong.

Any hint?

Michael

brent timothy saner

unread,
Nov 16, 2021, 2:35:31 PM11/16/21
to salt-...@googlegroups.com
On 11/16/21 14:19, Heinz Meier wrote:
> Hi,
>
> we use netbox (cool tool!) for DCIM. After defining a virtual machine in
> netbox I want to use salt to deploy the machine. Therefor I want to
> retrieve information about the machine (# CPU, #RAM, # Disk, interfaces,
> IP addresses) from netbox.

fellow netbox user here!

note that everything you mentioned above specifically (cpu count/core
count, ram amount, number of disks, net ifaces and their associated
addresses) are already available via grains!

salt 'minionname' grains.items

will show them all for you for a given minion with the minion ID
"minionname".

>
> On the command line it works:
> salt 'admin' http.query
> https://netbox.local/api/virtualization/virtual-machines/?name=test01
> <https://netbox.local/api/virtualization/virtual-machines/?name=test01>
> method=GET verify_ssl=False header_dict="{'Authorization': 'Token xxx'}"
>
> How can I use this in a sate file to set variables from the result of
> that request?
>

generally this would be done as a pillar, which are rendered on the
master and passed to the minion. specifically, you could probably do
this more efficiently if you did it via an external pillar:

https://docs.saltproject.io/en/latest/topics/development/modules/external_pillars.html

this has several benefits, most importantly the following:

- your netbox auth token isn't passed to minions and never leaves your
salt master
- you can be more restrictive with firewall rules on your netbox box,
only needing to allow the salt master instead of every single minion

BUT like i said, see if any of the grains would help you out here! if
you can keep it limited to grains usage, your states will compile and
execute MUCH faster (and you get the benefits i mentioned above re: not
needing to give netbox access to your minions).

> I tried
>
> {% set host =
> salt['http.query']('https://netbox.local/api/virtualization/virtual-machines/?name=test0101
> <https://netbox.local/api/virtualization/virtual-machines/?name=test0101> verify_ssl=False
> method=GET header_dict="\{''Authorization'': ''
> Token XXX''\}"') %}
>
> but it does not work, The error is that verify_ssl is not passed to the
> salt run that the cert verification fails. In other cases the
> authorization does not work, Perhaps escaping of the quators is wrong.
>
> Any hint?
>
> Michael

it's possible that verify_ssl is passed just fine, but the SSL/TLS
protocol version, cipher suite, etc. is incompatible with your minion so
it can't even handshake. that would lead to similar errors as to what
you're seeing.

but seriously, and i cannot stress this enough, check the grains first.
you'll thank me. :)

Phipps, Thomas

unread,
Nov 16, 2021, 2:35:54 PM11/16/21
to salt-...@googlegroups.com

{% set host = salt['http.query']('https://netbox.local/api/virtualization/virtual-machines/?name=test0101', verify_ssl=False, method="GET", header_dict={"Authorization": "Token XXX"}) %}

each option is it’s own item in the function call so needs to be separated by commas. as well as be treated individually.

also, you should check the output. as you most likely want the body sub item not just the raw output


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/CAFFTi_KH6%3DZPdN5kRkkYmA%3DXJW34hWkiX0kCkbsnjNbS3e8XrQ%40mail.gmail.com.

Heinz Meier

unread,
Nov 18, 2021, 3:29:52 PM11/18/21
to salt-...@googlegroups.com
Finally I found the time to check it.

{% set host = salt['http.query']('https://netbox.local/api/virtualization/virtual-machines/?name=test0101', method="GET", header_dict={"Authorization": "Token XXX"}, verify_ssl=false) %}

my_output:
 cmd.run:
   - name: echo "out {{ host }}"

Throws an error:
Rendering SLS 'base:mystate' failed: mapping values are not allowed in this context

Phipps, Thomas

unread,
Nov 18, 2021, 3:44:19 PM11/18/21
to salt-...@googlegroups.com

that is a yaml rendering error not a jinja rendering error. It means the output of host might not be what you are expecting. try "out {{ host | json}}" instead of "out {{ host }}"


Heinz Meier

unread,
Nov 18, 2021, 3:55:12 PM11/18/21
to salt-...@googlegroups.com
found it. The host variable contains ":", so it is interpreted wrong. How do I catch only the body sub item in the variable host?

Phipps, Thomas

unread,
Nov 18, 2021, 3:57:26 PM11/18/21
to salt-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages