1

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!

Jw9394
  • 13
  • 2
  • 1
    This sounds like it would suit a [here document](https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents). See for example [How to write multiple lines in one file from bash](https://askubuntu.com/questions/1269866/how-to-write-multiple-lines-in-one-file-from-bash). – steeldriver Jul 27 '23 at 10:26

2 Answers2

0

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 '%s\n' \
'Line one' \
'  Line two' \
'    Line three' \
'Line four' \
> file
$ cat file 
Line one
  Line two
    Line three
Line four

or

$ printf '%s\n' \
'Line one
  Line two
    Line three
Line four' \
> file
$ cat file
Line one
  Line two
    Line three
Line four

echo

$ echo -e \
'Line one\n'\
'  Line two\n'\
'    Line three\n'\
'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
Raffa
  • 24,905
  • 3
  • 35
  • 79
0

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

Andrew Lowther
  • 5,811
  • 1
  • 15
  • 23