如何收集 grep 并在 aws configset 中使用它

How to collect a grep and use it in a aws configset

在我的 aws Cloud Formation cfn configset 中,我有一个命令将环境密钥设置为 apache 所属的用户组的名称,因为它可能是 apache 或 www-data,具体取决于发行版。

像这样:

Metadata:
  AWS::CloudFormation::Init:
    configSets:
      joomla:
        - "set_permissions"
        - "and_some_more..."

    configure_cfn:
      files:
        /etc/cfn/hooks.d/cfn-auto-reloader.conf:
          content: !Sub |
            [cfn-auto-reloader-hook]
            triggers=post.update
            path=Resources.EC2.Metadata.AWS::CloudFormation::Init
            action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2 --configsets joomla --region ${AWS::Region}
          mode: "000400"
          owner: root
          group: root

.....

    set_permissions:  
      commands:
        01_01_get_WebServerGroup:
          env:
          #webserver group might be apache or www-data depending on the distro
            WebServerGp: 
              command: "ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print }'"

但是,当我启动这个堆栈时,configsets 进程此时停止并且我在 cfn_init.log 中收到一个错误,如下所示:

File "/usr/lib/python2.7/dist-packages/cfnbootstrap/command_tool.py", line 80, in apply raise ToolError(u"%s does not specify the 'command' attribute, which is required" % name) ToolError: 01_01_get_WebServerGroup does not specify the 'command' attribute, which is required

这是在 configset 命令中捕获和使用 grep 结果的首选方法吗?有没有更好的办法?我该怎么做才能解决 cfn_init.log?

中抛出的错误

好的,我想我可以创建参数和映射元素以在启动时捕获发行版类型,然后相应地设置网络服务器组,但我真的想了解如何设置 env: 键以响应来自 cli .

你的代码的问题是这一行 WebServerGp.

command 必须与 env 处于同一级别,在 commands 名称下,在您的例子中是 01_01_get_WebServerGroup。所以,它必须是这样的:

commands:
    01_01_get_WebServerGroup:
      env: ..
      command: ..

如果你想使用grep的结果,你可以把它们放在变量上,以后再用。 您可以在 command 行下指定多个命令,使用 \n 来执行命令。

请检查下面的代码。

command: "result=(ps ef | grep ...)\n echo $result\n ..."

如果你有很长的命令,你可以使用Fn::Join作为command的值。