如何在ansible playbook中将变量作为json对象键传递? [英] how pass a variable as json object key in ansible playbook?

查看:18
本文介绍了如何在ansible playbook中将变量作为json对象键传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行 curl 命令,该命令将变量作为 json 正文中的键,但由于出现 400 错误,该变量没有被值替换:

I am trying to execute the curl command that take a variable as key in json body but the variable is not being replaced by the value due to which getting the 400 error:

tasks:
- name: set source tag 
  uri:
    url: https://****/api/v2/source
    method: POST
    body: 
      tags: 
         "{{ ec2_tag_KubernetesCluster }}": true
         "{{ ec2_tag_Environment }}": true
         "{{ ec2_tag_aws_autoscaling_groupName }}": true
      sourceName: "{{ ec2_tag_hostname }}"
    body_format: "json"  
    headers:
      Content-Type: "application/json"
      Accept: "application/json" 

输出:

"changed": false,
"connection": "close",
"content": "{\"status\":{\"result\":\"ERROR\",\"message\":\"invalid tag: {{ ec2_tag_Environment }}\",\"code\":400}}",
"content_length": "91",
"content_type": "application/json",
"date": "Tue, 22 Jun 2021 11:24:33 GMT",
"elapsed": 0,
"invocation": {
    "module_args": {
        "attributes": null,
        "backup": null,
        "body": {
            "sourceName": "eks-****-2-2a-worker53",
            "tags": {
                "{{ ec2_tag_Environment }}": true,
                "{{ ec2_tag_KubernetesCluster }}": true,
                "{{ ec2_tag_aws_autoscaling_groupName }}": true
            }
        },
        "body_format": "json",
        "client_cert": null,
        "client_key": null,
        "content": null,
        "creates": null,
        "delimiter": null,
        "dest": null,
        "directory_mode": null,
        "follow": false,
        "follow_redirects": "safe",
        "force": false,
        "force_basic_auth": false,
        "group": null,
        "headers": {
            "Accept": "application/json",

如您所见,ec2_tag_hostname"被替换为变量值,但 'ec2_tag_Environment'、'ec2_tag_KubernetesCluster' 和 'ec2_tag_aws_autoscaling_groupName' 没有被替换,因为这些是 json 对象的键.

as you see the "ec2_tag_hostname" is replace with variable value but 'ec2_tag_Environment', 'ec2_tag_KubernetesCluster' and 'ec2_tag_aws_autoscaling_groupName' is not getting replace as these are key of json object.

推荐答案

YAML 在我知道的任何情况下都不支持动态键;您需要将插值向上移动一级以实现您的目标:

YAML does not support dynamic keys in any circumstance that I'm aware of; you will want to move the interpolation up one level in order to achieve what you're after:

    body: >-
      {{
      {
      "tags": {
         ec2_tag_KubernetesCluster: True,
         ec2_tag_Environment: True,
         ec2_tag_aws_autoscaling_groupName: True,
      },
      "sourceName": ec2_tag_hostname,
      }
      }}
    body_format: "json"  

这是可行的,因为与 yaml 不同,python(因此 jinja2)确实支持动态 dict

That works because unlike yaml, python (and thus jinja2) does support dynamic dict keys

这篇关于如何在ansible playbook中将变量作为json对象键传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆