尝试将参数从主模板传递到子模板 [英] Trying to pass parameters from Master to child template

查看:76
本文介绍了尝试将参数从主模板传递到子模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将列表参数从主模板传递到子模板,但是遇到两个错误。这些是我在主模板上的当前参数。

I'm trying to pass list parameters from master to child template, however I'm running into two errors.. These are my current parameters on the master template.

"Parameters": {
    "ELBSubnets": {
        "Default": "subnet-5d8fea67,subnet-3e35cf15",
        "Type": "CommaDelimitedList"
    },
    "LCKeyPair": {
        "Default": "key-master",
        "Type": "String"
    },
    "LCSecurityGroups": {
        "Default": "sg-10a15c74,sg-880e5fec",
        "Type": "CommaDelimitedList"
    }
},

在传递给子模板时,在同一模板中使用此方法对其进行引用。

They are being referenced in this method on the same template when passing on to the child template.

    "ChildTempate1": {
        "Properties": {
            "Parameters": {
                "ELBSubnets": {
                    "Ref": "ELBSubnets"
                },
                "KeyPair": {
                    "Ref": "LCKeyPair"
                },
                "LCSecurityGroups": {
                    "Ref": "LCSecurityGroups"
                }
            },

在子模板上,它们被声明为完全相同。

On the child template, they are declared exactly the same.

"Parameters": {
    "ELBSubnets": {
        "Type": "CommaDelimitedList"
    },
    "LCKeyPair": {
        "Type": "String"
    },
    "LCSecurityGroups": {
        "Type": "CommaDelimitedList"
    }
},

在子模板中使用这种方法引用了它们。

And they're being referenced in this method in the child template.

            "KeyName": {
                "Ref": "LCKeyPair"
            },
            "SecurityGroups": {
                "Fn::Join": [
                    ",",
                    [
                        {
                            "Ref": "LCSecurityGroups"
                        }
                    ]
                ]
            }
        },

这是模板的另一部分。

            "Subnets": {
                "Fn::Join": [
                    ",",
                    [
                        {
                            "Ref": "ELBSubnets"
                        }
                    ]
                ]
            }
        },

当我尝试在主模板上使用fn :: join时,

When I attempt to use the fn::join on the master template, it says


模板验证错误:模板错误:每个Fn :: Join对象都需要两个参数,(1)字符串定界符和(2 )要连接的字符串列表或返回要连接的字符串列表(例如Fn :: GetAZs)的函数。

"Template validation error: Template error: every Fn::Join object requires two parameters, (1) a string delimiter and (2) a list of strings to be joined or a function that returns a list of strings (such as Fn::GetAZs) to be joined."

在主模板上不使用fn :: join时,错误是

When I don't use fn::join on the master template the error is


属性值参数必须是具有字符串(或简单类型)属性的对象

Value of property Parameters must be an object with String (or simple type) properties

无论子模板中的相同参数是否有fn :: join。

Regardless of whether I have fn::join on the same parameters in the child template.

可以在此处找到这两个模板: https://github.com/slimg00dy/Troposphere-CloudformationTests

Both templates can be found here: https://github.com/slimg00dy/Troposphere-CloudformationTests

推荐答案

问题是,在CommaDelimitedList上使用Ref时,您传递了一个列表,因此它传递的是 [a,b,c]而不是 a,b,c

The issue is that when using Ref on a CommaDelimitedList, you pass a list, so it passes "[a,b,c]" rather than "a,b,c"

因此在主模板上,我使用Join(,)传递列表,并使用Join())传递字符串。这样,它们被引用为 a,b,c和字符串

So on the master template I've used Join(",") to pass lists, and Join(" ") to pass strings. This way they're Referenced as "a,b,c" and " String"

在子模板上,我已将参数设置为列表和字符串的CommaDelimitedLists从字符串。这是因为它们是从主模板传递的。

On the child template I've set the parameters as CommaDelimitedLists for the lists and String from the String. This is because of the way they're being passed from the master template.

然后我在子模板上使用了Ref,而在子模板上没有使用Join() 。这样就创建了CommaDelimitedLists到列表中,并使用Join()作为字符串传递了连接的字符串。

I then used Ref on the Child template without the use of Join() on the child template. This created the CommaDelimitedLists into Lists and the Strings that were joined with Join(" ") passed as strings.

这是我在主模板上的参数声明。

Here is my parameter declaration on the master Template.

"Parameters": {
    "ELBSubnets": {
        "Default": "subnet-5d8fea67,subnet-3e35cf15",
        "Type": "CommaDelimitedList"
    },
    "LCKeyPair": {
        "Default": "key-master",
        "Type": "CommaDelimitedList"
    },
    "LCSecurityGroups": {
        "Default": "sg-10a15c74,sg-880e5fec",
        "Type": "CommaDelimitedList"
    }
},

这是我使用Join(,)的方式, Join()和参考。由于Ref将CommaDelimitedLists转换为列表,因此我使用Join(,)将其保留为CommaDelimitedLists并将其传递给子模板。

Here is how I used Join(","), Join(" ") and Ref. Since Ref converts the CommaDelimitedLists into Lists, I used Join(",") to keep them as CommaDelimitedLists and pass them on to the child template.

对于KeyPair字符串,我确保在父模板上将其声明为CommaDelimitedList并与Join()一起加入,这实际上使它成为了

As for the KeyPair String, I made sure that it was declared as a CommaDelimitedList on the parent template and joined with Join(" "), this effectively made it into a string when referencing to the child template.

            "Parameters": {
                "ELBSubnets": {
                    "Fn::Join": [
                        ",",
                        {
                            "Ref": "ELBSubnets"
                        }
                    ]
                },
                "LCKeyPair": {
                    "Fn::Join": [
                        " ",
                        {
                            "Ref": "LCKeyPair"
                        }
                    ]
                },
                "LCSecurityGroups": {
                    "Fn::Join": [
                        ",",
                        {
                            "Ref": "LCSecurityGroups"
                        }
                    ]
                }
            },

在子模板上的声明如下。

On the child template, they're declared like so.

 "Parameters": {
    "ELBSubnets": {
        "Type": "CommaDelimitedList"
    },
    "LCKeyPair": {
        "Type": "String"
    },
    "LCSecurityGroups": {
        "Type": "CommaDelimitedList"
    }
},

并且通常在不使用子模板上使用Join的情况下全部引用它们。

And they are all referenced normally without the use of Join on the child template.

Subnets": {
                "Ref": "ELBSubnets"
            }

有可能有许多不同的方法可以做到这一点。我本可以在子模板而不是父模板上进行联接。但是,我更喜欢使一个模板保持复杂,而其余模板则尽可能保持干净。希望这可以帮助其他人。

There could have been many different ways to do this. I could have had the joins on the child template rather than the parent template. However I prefer to keep one template complicated and the rest as clean as possible. Hope this helps others down the line.

我也应该能够将列表作为子模板上的列表传递,但是随后我收到了错误消息 Unknown parameter类型:列表

I also should have been able to pass the list as a list on the child template, however then I received the error "Unknown parameter type: List"

这篇关于尝试将参数从主模板传递到子模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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