在一行上为阵列中的所有元素打印键值

时间:2018-08-26 17:21:07

标签: amazon-web-services jq

我正在尝试从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

1 个答案:

答案 0 :(得分:0)

将其用于您的jq过滤器:

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