如何在userdata部分使用!在下面的userdata中,我想用映射数据更新MainSshKey。
Mappings:
AccountToStage:
"123456789012":
StageName: Beta
Beta:
us-east-1:
MainSshKey: ssh-rsa AAAAB3NzaC
AdminSshKey: ssh-rsa AAAAB3NzaC1
用户数据:
Fn::Base64: !Sub |
#cloud-config
users:
- name: main
ssh_authorized_keys:
- ${MainSshKey}
- name: admin
ssh_authorized_keys:
- ${AdminSshKey}
this is what I have try
#cloud-config
users:
- name: main
ssh_authorized_keys:
- ${MainSshKey}
- MainSshKey: !FindInMap
- !FindInMap
- AccountToStage
- !Ref "AWS::AccountId"
- StageName
- !Ref "AWS::Region"
- MainSshKey
- name: admin
ssh_authorized_keys:
- ${AdminSshKey}
Cloudformation无法解决此问题。
注意:如果我定义MainSshKey作为一个参数,它工作得很好,似乎不工作使用FindInMap
任何提示都非常感谢
您必须使用Sub的列表形式,因此您应该遵循这些行。请注意,您可能会修复所有的缩进,并进一步更改它以使其工作。然而,Sub
的列表形式是问题的关键。
Fn::Base64: !Sub
- |
#cloud-config
users:
- name: main
ssh_authorized_keys:
- ${SubMainSshKey}
- name: admin
ssh_authorized_keys:
- ${AdminSshKey}
- SubMainSshKey: !FindInMap
- !FindInMap
- AccountToStage
- !Ref "AWS::AccountId"
- StageName
- !Ref "AWS::Region"
- MainSshKey
另外,这是不正确的userdata,所以我不确定你想要实现什么。
我可以使用下面的代码来解决这个问题,加号告诉它在后面保留一个换行符。Arg1告诉它用Fn::FindInMap的结果替换arg0中的MainSshKey。
Fn::Base64: !Sub
- |+
#cloud-config
users:
- name: main
ssh_authorized_keys:
- ${MainSshKey}
- name: admin
ssh_authorized_keys:
- ${AdminSshKey}
- MainSshKey:
Fn::FindInMap:
- !FindInMap
- AccountToStage
- !Ref "AWS::AccountId"
- StageName
- !Ref "AWS::Region"
- MainSshKey
同样,如果您需要替换两个值(例如:MainSshKey和AdminSshKey),您可以使用下面相同的列表Sub构造示例。
Fn::Base64: !Sub
- |+
#cloud-config
users:
- name: main
ssh_authorized_keys:
- ${MainSshKey}
- name: admin
ssh_authorized_keys:
- ${AdminSshKey}
- MainSshKey:
Fn::FindInMap:
- !FindInMap
- AccountToStage
- !Ref "AWS::AccountId"
- StageName
- !Ref "AWS::Region"
- MainSshKey
AdminSshKey:
Fn::FindInMap:
- !FindInMap
- AccountToStage
- !Ref "AWS::AccountId"
- StageName
- !Ref "AWS::Region"
- AdminSshKey