简体   繁体   English

如何为 aws cli cloudformation deploy 添加空间?

[英]How do you add spaces for aws cli cloudformation deploy?

I am trying to get spaces into the tags parameter for the aws cli and it works if I hardcode it but not if I use bash variables.我正在尝试将空格放入 aws cli 的 tags 参数中,如果我对其进行硬编码,它会起作用,但如果我使用 bash 变量则不起作用。 What is going on and how do I fix it?这是怎么回事,我该如何解决?

This works with out spaces:这适用于空格:

aws cloudformation deploy  \
      --template-file /path_to_template/template.json \ 
      --stack-name my-new-stack \
      --tags Key1=Value1 Key2=Value2

This works with out spaces but with variables:这适用于空格但使用变量:

tags="Key1=Value1 Key2=Value2" 
aws cloudformation deploy \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags $tags

This works with spaces:这适用于空格:

aws cloudformation deploy  \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags 'Key1=Value1' 'Key Two=Value2'

This does not work, spaces and variables:这不起作用,空格和变量:

tags="'Key1=Value1' 'Key Two=Value2'"

aws cloudformation deploy  \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags $tags

Attempting to fix bash expansion, also does not work, spaces and variables:尝试修复 bash 扩展,也不起作用,空格和变量:

tags="'Key1=Value1' 'Key Two=Value2'"

aws cloudformation deploy  \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags "$tags"

Attempting to fix bash expansion, also does not work, spaces and variables:尝试修复 bash 扩展,也不起作用,空格和变量:

tags="'Key1=Value1' 'Key Two=Value2'"

aws cloudformation deploy  \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags "$(printf '%q' $tags)"

Error:错误:

Invalid parameter: Tags Reason: The given tag(s) contain invalid characters (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID无效参数:标签原因:给定标签包含无效字符(服务:AmazonSNS;状态代码:400;错误代码:InvalidParameter;请求 ID

Try this :尝试这个 :

tags="'Key1=Value1' 'Key Two=Value2'"

aws cloudformation deploy  \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags "$tags"
#        ^     ^
#     double quotes

Learn how to quote properly in shell, it's very important :学习如何在 shell 中正确引用,这非常重要:

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var" , "$(command "$var")" , "${array[@]}" , "a & b" . “双引号”包含空格/元字符和每个扩展的每个文字: "$var""$(command "$var")""${array[@]}""a & b" Use 'single quotes' for code or literal $'s: 'Costs $5 US' , ssh host 'echo "$HOSTNAME"' .对代码或文字$'s: 'Costs $5 US'使用'single quotes' $'s: 'Costs $5 US'ssh host 'echo "$HOSTNAME"' See
http://mywiki.wooledge.org/Quotes http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words http://wiki.bash-hackers.org/syntax/words

Would you please try:请你试试:

tags=('Key1=Value1' 'Key Two=Value2')

aws cloudformation deploy  \
  --template-file /path_to_template/template.json \ 
  --stack-name my-new-stack \
  --tags "${tags[@]}"

Stealing some ideas from https://github.com/aws/aws-cli/issues/3274 I was able to get this working by doing the followinghttps://github.com/aws/aws-cli/issues/3274窃取一些想法我能够通过执行以下操作来使其工作

    deploy=(aws cloudformation deploy 
            ...
            --tags $(cat tags.json | jq '.[] | (.Key + "=" + .Value)'))

    eval $(echo ${deploy[@]})

With a tags.json file structure of带有tags.json文件结构

[
    {
        "Key": "Name With Spaces",
        "Value": "Value With Spaces"
    },
    {
        "Key": "Foo",
        "Value": "Bar"
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM