云配置中 write_files 指令的正确语法?

Proper syntax of write_files directive in cloud config?

我正在尝试让云配置脚本与我的 DigitalOcean Droplet 一起正常工作,但我正在临时测试本地 lxc 容器。

我一直遇到的一个问题是,我永远无法让 write_files 指令对多个文件正常工作。 它的行为似乎很奇怪,我无法理解

比如这个配置不正确,在/tmp中只输出一个文件(.tarsnaprc):

#cloud-config
users:
  - name: julian
    shell: /bin/bash
    ssh_authorized_keys:
      - ssh-rsa myrsakeygoeshere julian@hostname
write_files:
  - path: /tmp/.tarsnaprc
    permissions: "0644"
    content: |
      cachedir /home/julian/tarsnap-cache
      keyfile /home/julian/tarsnap.key
      nodump
      print-stats
      checkpoint-bytes 1G
    owner: julian:julian
  - path: /tmp/lxc
    content: |
      lxc.id_map = u 0 100000 65536
      lxc.id_map = g 0 100000 65536
      lxc.network.type = veth
      lxc.network.link = lxcbr0
    permissions: "0644"

但是,如果我交换 write_files 数组中的两个项目,它会神奇地工作,并创建两个文件,.tarsnaprclxc。我做错了什么,语法错误吗?

可能为时已晚,因为它是 1 年前发布的。问题是在 /tmp/.tarsnaprc 中设置所有者,因为创建文件时用户不存在。 检查 清楚解释云配置指令顺序的答案。

Do not write files under /tmp during boot because of a race with systemd-tmpfiles-clean that can cause temp files to get cleaned during the early boot process. Use /run/somedir instead to avoid race LP:1707222.

参考:https://cloudinit.readthedocs.io/en/latest/topics/modules.html#write-files

来到这里是因为使用了 canonicals multipass。现在 and 的答案仍然暗示了正确的方向。更正后的示例如下所示:

#cloud-config
users:
  - name: julian
    shell: /bin/bash
    ssh_authorized_keys:
      - ssh-rsa myrsakeygoeshere julian@hostname
write_files:
  # not writing to /tmp
  - path: /data/.tarsnaprc
    permissions: "0644"
    content: |
      cachedir /home/julian/tarsnap-cache
      keyfile /home/julian/tarsnap.key
      nodump
      print-stats
      checkpoint-bytes 1G
  # at execution time, this owner does not yet exist (see runcmd)
  #   owner: julian:julian
  - path: /data/lxc
    content: |
      lxc.id_map = u 0 100000 65536
      lxc.id_map = g 0 100000 65536
      lxc.network.type = veth
      lxc.network.link = lxcbr0
    permissions: "0644"
runcmd:
  - "chown julian:julian /data/lxc /data/.tarsnaprc"