首页 > 解决方案 > powershell 向 AWX 调用 rest api

问题描述

上周或更长时间,我一直在努力从 powershell 发送 rest api 命令以在 AWX 中添加主机(来自 curl 正在工作)。当我发送一个参数是 worki 但我还需要发送变量

$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

和我得到的错误:

{ "name": "wewewe", "variables": "test" } Invoke-RestMethod : {"variables":["Cannot parse as JSON (error: Expecting value: line 1 column 1 (char 0)) 或 YAML (错误:输入类型str不是字典)。"]} 在 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

也许任何一个用户都有这个问题,现在如何克服它?

标签: powershellrestansible-awx

解决方案


根据我的研究,参数variables应该定义为 JSON 或 YAML 格式。更多详情,请参阅此处在此处输入图像描述

有关如何调用 API 的更多详细信息,请参阅博客


推荐阅读