【问题标题】:Cloudformation template order of execution?Cloudformation 模板的执行顺序?
【发布时间】:2016-09-05 23:10:06
【问题描述】:

我正在尝试创建一个安装 puppet 和 aws puppet 模块的 cloudformation 模板。我可以使用 puppet 创建我的实例,定义安全组等,它似乎工作正常,但我也想安装 aws puppet 模块作为我的模板的一部分。 这是我的木偶实例的代码

    "PuppetMasterInstance" : {
  "Type" : "AWS::EC2::Instance",
  "Metadata" : {
    "AWS::CloudFormation::Init" : {
      "config" : {
        "packages" : {
          "yum" : {
          "puppet3" : [],
            "puppet3-server" : [],
            "ruby-devel" : [],
            "gcc" : [],
            "make" : [],
            "rubygems" : []
          },
          "rubygems" : {
            "json" : []
          }
        },
        "files": {
          "/etc/yum.repos.d/epel.repo": {
            "source": "https://s3.amazonaws.com/cloudformation-examples/enable-epel-on-amazon-linux-ami",
            "mode": "000644",
            "owner": "root",
            "group": "root"
          },

          "/etc/puppet/autosign.conf": {
            "content": "*.internal\n",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          },
          "/etc/puppet/fileserver.conf": {
            "content": "[modules]\n allow *.internal\n",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          },
          "/etc/puppet/puppet.conf": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "[main]\n",
                  " logdir=/var/log/puppet\n",
                  " rundir=/var/run/puppet\n",
                  " ssldir=$vardir/ssl\n",
                  " pluginsync=true\n",
                  "[agent]\n",
                  " classfile=$vardir/classes.txt\n",
                  " localconfig=$vardir/localconfig\n"
                ]
              ]
            },
            "mode": "000644",
            "owner": "root",
            "group": "root"
          },
          "/etc/puppet/modules/cfn/manifests/init.pp": {
            "content": "class cfn {}",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          },
          "/etc/puppet/modules/cfn/lib/facter/cfn.rb": {
            "source": "https://s3.amazonaws.com/cloudformation-examples/cfn-facter-plugin.rb",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          }
        },
        "services": {
          "sysvinit": {
            "puppetmaster": {
              "enabled": "true",
              "ensureRunning": "true"
            }
          }
        }
      }
    }
  },
  "Properties": {
    "InstanceType": {
      "Ref": "InstanceType"
    },
    "SecurityGroups": [
      {
        "Ref": "PuppetGroup"
      }
    ],
    "ImageId": {

      "Ref": "AmiID"

    },
    "KeyName": {
      "Ref": "KeyName"
    },
    "UserData": {
      "Fn::Base64": {
        "Fn::Join": [
          "",
          [
            "#!/bin/bash\n",
            "yum update -y \n",


            "/opt/aws/bin/cfn-init --region ",
            {
              "Ref": "AWS::Region"
            },
            " -s ",
            {
              "Ref": "AWS::StackName"
            },
            " -r PuppetMasterInstance ",
            " --access-key ",
            {
              "Ref": "CFNKeys"
            },
            " --secret-key ",
            {
              "Fn::GetAtt": [
                "CFNKeys",
                "SecretAccessKey"
              ]
            },
            "\n",
            "/opt/aws/bin/cfn-signal -e $? '",
            {
              "Ref": "PuppetMasterWaitHandle"
            },
            "'\n"
          ]
        ]
      }
    }
  }
}

这很好,但是我想在安装 puppet 后执行以下命令:

"gem install aws-sdk-core",
"gem install retries",
"export AWS_ACCESS_KEY_ID=my_key",
"export AWS_SECRET_ACCESS_KEY=my_secret",
 "puppet module install puppetlabs-aws"

我尝试在“files:”之前使用“commands:”标签,但模板失败了。我试图将代码放在“UserData”中:但它又失败了。我找不到有关模板中不同部分的执行顺序的信息,我认为失败是由于错误的执行顺序造成的(命令运行时未安装 puppet 和 ruby​​)。

任何帮助将不胜感激。

【问题讨论】:

  • 我没有时间写一个例子,但我的第一个例子是写一些明确等待安装的 shell 脚本。执行顺序是针对cloud-init,看这个答案stackoverflow.com/questions/34095839/…
  • 感谢您的建议。我需要将所有内容都包含在我的模板中,并且我能想到的检查服务是否已安装的唯一方法是编写无限循环来检查服务是否正在运行,但是我想这会阻止模板的执行和整个事情最终将等待一个无限循环完成,而这又是等待安装服务打破循环......在安装实例包后必须有一种正确的方法来执行我的脚本...... .

标签: templates amazon-web-services puppet amazon-cloudformation


【解决方案1】:

我在 aws 论坛上发现了一篇关于执行顺序的信息非常丰富的帖子。 AWS::CloudFormation::Init 按以下顺序执行:

包 -> 组 -> 用户 -> 源 -> 文件 -> 命令 -> 服务

来源:https://forums.aws.amazon.com/message.jspa?messageID=414670

我解决问题的方法可能远非理想,但它确实有效:

    "PuppetMasterInstance" : {
  "Type" : "AWS::EC2::Instance",
  "Metadata" : {
    "AWS::CloudFormation::Init" : {
      "configSets" : {
        "ascending" : [ "config" , "config2" ],
        "descending" : [ "config2" , "config" ]
      },
      "config" : {
        "packages" : {
          "yum" : {
          "puppet3" : [],
            "puppet3-server" : [],
            "ruby-devel" : [],
            "gcc" : [],
            "make" : [],
            "rubygems" : []
          },
          "rubygems" : {
            "json" : []
          }
        },
        "files": {
          "/etc/yum.repos.d/epel.repo": {
            "source": "https://s3.amazonaws.com/cloudformation-examples/enable-epel-on-amazon-linux-ami",
            "mode": "000644",
            "owner": "root",
            "group": "root"
          },

          "/etc/puppet/autosign.conf": {
            "content": "*.internal\n",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          },
          "/etc/puppet/fileserver.conf": {
            "content": "[modules]\n allow *.internal\n",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          },
          "/etc/puppet/puppet.conf": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "[main]\n",
                  " logdir=/var/log/puppet\n",
                  " rundir=/var/run/puppet\n",
                  " ssldir=$vardir/ssl\n",
                  " pluginsync=true\n",
                  "[agent]\n",
                  " classfile=$vardir/classes.txt\n",
                  " localconfig=$vardir/localconfig\n"
                ]
              ]
            },
            "mode": "000644",
            "owner": "root",
            "group": "root"
          },
          "/etc/puppet/modules/cfn/manifests/init.pp": {
            "content": "class cfn {}",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          },
          "/etc/puppet/modules/cfn/lib/facter/cfn.rb": {
            "source": "https://s3.amazonaws.com/cloudformation-examples/cfn-facter-plugin.rb",
            "mode": "100644",
            "owner": "root",
            "group": "wheel"
          }
        },
        "services": {
          "sysvinit": {
            "puppetmaster": {
              "enabled": "true",
              "ensureRunning": "true"
            }
          }
        }
      },

      "config2" : {
        "commands" : {
          "1" : {
            "command" : "gem install aws-sdk-core"
          },
          "2" : {
            "command" : "gem install retries"
          },
          "3" : {
            "command" : "export _MYAWSKEY_"
          },
          "4" : {
            "command" : "export MY_AWS_SECRET_"
          },
          "5" : {
            "command" : "puppet module install puppetlabs-aws"
          }
        }

      }



    }
  },
  "Properties": {
    "InstanceType": {
      "Ref": "InstanceType"
    },
    "SecurityGroups": [
      {
        "Ref": "PuppetGroup"
      }
    ],
    "ImageId": {

      "Ref": "AmiID"

    },
    "KeyName": {
      "Ref": "KeyName"
    },
    "UserData": {
      "Fn::Base64": {
        "Fn::Join": [
          "",
          [
            "#!/bin/bash\n",
            "yum update -y \n",


            "/opt/aws/bin/cfn-init -c ascending --region ",
            {
              "Ref": "AWS::Region"
            },
            " -s ",
            {
              "Ref": "AWS::StackName"
            },
            " -r PuppetMasterInstance ",
            " --access-key ",
            {
              "Ref": "CFNKeys"
            },
            " --secret-key ",
            {
              "Fn::GetAtt": [
                "CFNKeys",
                "SecretAccessKey"
              ]
            },
            "\n",
            "/opt/aws/bin/cfn-signal -e $? '",
            {
              "Ref": "PuppetMasterWaitHandle"
            },
            "'\n"
          ]
        ]
      }
    }
  }
}

通过指定 configSets 的执行顺序,我可以运行安装和配置 puppet 所需的一切,然后运行命令来安装插件。

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 2017-07-02
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多