how to wirte a multiline text including space key to "late-commands" section in cloud-init "user-data" file

i am trying to write a user-data file so i could deploy many ubuntu server at the same time. but now i am stuck at the late-commands section. i am trying to add a netplan configuration to 01-network-manager-all.yaml file. it is a yaml file so i have to keep the space key and input multiline text. i tried using
echo "" >> 01-network-manager-all.yaml
doing this line by line. is there any way to insert multiline text at the same time? thanks!

Asked By: Jw9394

||

Is there any way to insert multiline text at the same time?

Yes, plenty … In Bash(The default login shell in Ubuntu), a few examples using:

Here Document

$ cat << 'EOF' > file
Line one
  Line two
    Line three
Line four
EOF
$ cat file 
Line one
  Line two
    Line three
Line four

printf

$ printf '%sn' 
'Line one' 
'  Line two' 
'    Line three' 
'Line four' 
> file
$ cat file 
Line one
  Line two
    Line three
Line four

or

$ printf '%sn' 
'Line one
  Line two
    Line three
Line four' 
> file
$ cat file
Line one
  Line two
    Line three
Line four

echo

$ echo -e 
'Line onen'
'  Line twon'
'    Line threen'
'Line four' 
> file
$ cat file
Line one
  Line two
    Line three
Line four

or

$ echo 
'Line one
  Line two
    Line three
Line four' 
> file
$ cat file
Line one
  Line two
    Line three
Line four
Answered By: Raffa

An autoinstall like the following snippet will delete the installer generated network config and write a custom network config using late-commands.

#cloud-config
autoinstall:
  late-commands:
    - |
      rm /target/etc/netplan/00-installer-config.yaml
      cat <<EOF > /target/etc/netplan/01-network-manager-all.yaml
      network:
          version: 2
          ethernets:
              zz-all-en:
                  match:
                      name: "en*"
                  dhcp4: true
              zz-all-eth:
                  match:
                      name: "eth*"
                  dhcp4: true
      EOF

see also

Answered By: Andrew Lowther