将行添加到文件的命令的 cloud-init runcmd syntx

Cloud-init runcmd syntx of a command that adding a line to file

我正在尝试执行这个简单的命令,但它总是失败:

 echo "secret_backend_command: /home/ec2-user/dd-get-secrets.py" >> /etc/datadog-agent/datadog.yaml"

我尝试了以下所有方法:

        - [ sh, -c, echo "secret_backend_command: /home/ec2-user/dd-get-secrets.py" >> /etc/datadog-agent/datadog.yaml ]

        - 'sh -c "echo \"secret_backend_command: /home/ec2-user/dd-get-secrets.py\" >> /etc/datadog-agent/datadog.yaml"'

        - sh -c "echo 'secret_backend_command: /home/ec2-user/dd-get-secrets.py' >> /etc/datadog-agent/datadog.yaml

知道如何修复这个引号吗?

note I tested with another file rather than datadpg.yaml because I'm getting Permission denied for this one.

我在 here 中找到的示例不起作用

我改变了这样做的方式,而不是将它放在 runcmds 的 yaml 列表中,我创建了一个脚本,而是将这个命令放在里面并在 runcmds 中调用脚本。通过这样做,我摆脱了 cloud-init 中对“:”和引号的棘手处理。

  UserData:
    'Fn::Base64': !Sub |
      #cloud-config
      write_files:
        - path: /home/ec2-user/update-datadog-config.sh
          permissions: 0744
          owner: root
          content: |
            #!/usr/bin/env bash
            set -e
            echo "secret_backend_command: /home/ec2-user/dd-get-secrets.py" >> /etc/datadog-agent/datadog.yaml
      runcmd:
        - bash /home/ec2-user/update-datadog-config.sh