【问题标题】:Print keyvalue on single line for all elements in an array在单行上打印数组中所有元素的键值
【发布时间】:2018-08-26 17:21:07
【问题描述】:

我正在尝试从 EC2 实例中获取标签列表,并将这些标签应用于该实例的所有附加卷,如下所示:

# Get the tags of the instance (filter out tags containing :aws:, since those are tags that AWS applies and they are NOT ours):
aws ec2 describe-tags --filters "Name=resource-id,Values=${instance_id}" --region us-west-2 | jq '[ .Tags[] | select( .Key | contains(":aws:") | not ) ]'

# That command produces this output:
[
  {
    "ResourceType": "instance",
    "ResourceId": "i-0f1da295d8343635b",
    "Value": "tester",
    "Key": "Name"
  },
  {
    "ResourceType": "instance",
    "ResourceId": "i-0f1da295d8343635b",
    "Value": "test_env",
    "Key": "environment"
  },
  {
    "ResourceType": "instance",
    "ResourceId": "i-0f1da295d8343635b",
    "Value": "ui_tester",
    "Key": "role"
  }
]

不幸的是,create-tags api 命令采用以下形式:

aws ec2 create-tags --resources vol-076317f0fd49cb024 vol-0e91c84611369fc3f \
 --tags Key=role,Value=ui_tester Key=environment,Value=test_env Key=Name,Value=tester

如何使用 jq 将该键/值数组转换为单行,如下所示:

Key=role,Value=ui_tester Key=environment,Value=test_env Key=Name,Value=tester

【问题讨论】:

    标签: amazon-web-services jq


    【解决方案1】:

    将此用于您的 jq 过滤器:

    [.Tags[] | select(.Key | contains(":aws:") | not) | "Key=\(.Key),Value=\(.Value)"] | join(" ")
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2017-08-22
      • 2023-03-09
      相关资源
      最近更新 更多