0
votes

I'm struggling for last week or more with sending rest api command from powershell to add host in AWX(from curl is working). When I sent one parameter is worki but I need to send also variables

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $VfAwxTokenX")
$Headers.Add('Content-Type', 'application/json')
$body = @{
  name = '$vmname'
  variables = 'test'

}| ConvertTo-Json  
write-output    $body
$response = Invoke-RestMethod 'https://awx/api/v2/inventories/2/hosts/' -Method 'POST' -Headers $headers -UseBasicParsing -Body $body

and error what i get:

{ "name": "wewewe", "variables": "test" } Invoke-RestMethod : {"variables":["Cannot parse as JSON (error: Expecting value: line 1 column 1 (char 0)) or YAML (error: Input type str is not a dictionary)."]} At line:32 char:13

  • $response = Invoke-RestMethod 'https://awx/api ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
  • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Maybe any one of users had that issue and now how to overcome it?

2

2 Answers

0
votes

According to my research, the parameter variables should be defined as JSON or YAML format. For more details, please refer to here. enter image description here

For more details about how to call the API, please refer to the blog.

0
votes

the problem was prosaic

  variables = 'test' 

if i put anything else then test is working :/