Skip to content

Using #include section from cloud-init specs #4603

@Andygol

Description

@Andygol

What are you trying to do?

I have several cloud-init files that describe virtual machine configurations. Some of their content is identical. I tried to separate the common part into a separate file and use it via the #include directive (https://cloudinit.readthedocs.io/en/latest/explanation/format.html#include-file). However, I didn't succeed.

To repeat my attempts, here is a brief example from the cloud-init documentation

multipass launch --cloud-init - <<EOF
#cloud-config

# If you don't specify the `users: []` section, multipass won't start deploying the VM.
users:
  - default

#include
https://raw.githubusercontent.com/canonical/cloud-init/403f70b930e3ce0f05b9b6f0e1a38d383d058b53/doc/examples/cloud-config-boot-cmds.txt
EOF

where #include content looks like this 👇

#cloud-config

bootcmd:
  - echo 192.168.1.130 us.archive.ubuntu.com >> /etc/hosts
  - [ cloud-init-per, once, mymkfs, mkfs, /dev/vdb ]

After creating the VM, check the contents of the /etc/hosts file.

ubuntu@infallible-ant:~$ sudo cat /etc/hosts
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.debian.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
#
127.0.1.1 infallible-ant infallible-ant
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

The expected line "192.168.1.130 us.archive.ubuntu.com" is missing.

Checking whether the device was created /dev/vdb

ubuntu@infallible-ant:~$ sudo ls /dev/vdb
ls: cannot access '/dev/vdb': No such file or directory

What's your proposed solution?

It would be great if we could use the #include directive to collect instructions for deploying a VM from several different files.

Additional context

The cloud-init documentation only provides an example for retrieving some settings from a remote source (https://...), but I hope that this should also work for local files. I also hope that file chains will work correctlyas well.

# file-users.yaml
---
#cloud-config

users:
 - name: username
   sudo: ALL=(ALL) NOPASSWD:ALL
   ssh_authorized_keys:
     - $( cat ~/.ssh/id_rsa.pub )

⬇️

# file-vm.yaml
---
#cloud-config

#include
file-users.yaml

package_update: true
package_upgrade: true
packages:
  - git
  - python3-pip

⬇️

# file-extra.yaml
---
#cloud-config

#include
file-vm.yaml

runcmd:
 - [ ls, -l, / ]
 - [ sh, -xc, "echo $(date) ': hello world!'" ]
 - [ sh, -c, echo "=========hello world=========" ]
 - ls -l /root

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions