Thursday, July 13, 2023

Working with Chef Automate API on Windows

This blog explains how to use the Chef Automate API on a Windows machine. 

Pre-requisites:

  • Chef Automate Dashboard with node added to compliance.

Chef Automate API request with an API token passed in the header. This token can be created in chef automate dashboard as follows.

Login to Chef Automate dashboard. Go to token section in the settings page. 


Create new token with relevant permissions. 



Go to token list and copy token value.



Following sample script retrieves details of the node with a provided IP if it is in reachable status.

$Uri = <your Chef Automate dashboard Url> +/api/v0/nodes/search

$token = value of token created in previous steps

$currentvmip = IP of the node added to compliance section of Chef Automate

$Uri = "https://my-chef-vm.testorg.azure/api/v0/nodes/search"
$token = "<<token-value>>"
$header = @{"api-token"=$token};

$currentvmip = <ip of the node>


$body = '{
  "filters" : [
    {
    "key" : "status",
    "values" : ["reachable"]
    },
    {
    "key": "name",
    "values": ["'+$currentvmip+'"]
    }
  ]
}'


$release = Invoke-RestMethod -Uri  $Uri -Headers $header -Method Post -Body $body -ContentType application/json

A Successful run of the above API will provide details of the compliance status of the given node.

Find sample code in GitHub


No comments:

Post a Comment