0

I'm trying to use CloudInit to spin up VMs in a KVM hypervisor environment.
I have downloaded the Ubuntu 18.04 image and created the following config file:

#cloud-config
package_upgrade: true
users:
  - name: <your_user>
    groups: wheel
    lock_passwd: false
    passwd: <your_passord_hash>
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - <your_public_ssh_key> 

I can spin up the VM and login via console.
I looked around but could not find any info on how to configure my network
interfaces, IP addresses, etc.
I could not understand the CloudInit documents.

Any links or tips will be appreciated.

kenlukas
  • 2,886
  • 2
  • 14
  • 25

1 Answers1

1

ok so here is how i solved it
for ubuntu image you can use the netplan file located at /etc/netplan/SOMEFILE.yaml
for centos i noticed the image does not have net plan so i just wrote the interface files in my config
use the write_files to create the netplan file on ubuntu and interface files on the centos
after that use the runcmd to initialize your interfaces by running the appropriate commands
here are my config.yaml files for ubuntu and centos respectively

#cloud-config
package_upgrade: true
users:
  - name: USER
    groups: wheel
    lock_passwd: false
    passwd: PASSWORD_HASH
    shell: /bin/bash
    ssh_pwauth: true
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - ssh-rsa YOUR_KEY

write_files:
  - path:  /etc/netplan/50-cloud-init.yaml
    permissions: '0644'
    content: |
         network:
           version: 2
           renderer: networkd
           ethernets:
             ens3:
               addresses: [IP/SUBNET]
               routes:
                  - to: NETWORK
                    via: NEXT_HOP

             ens4:
               addresses: [IP/SUBNET]
               gateway4: GATEWAY
               nameservers:
                 addresses: [DNS]

runcmd:
 - [sudo, ifconfig, IFNAME, up]
 - [sudo, ifconfig, IFNAME, up]
 - [sudo, netplan, generate]
 - [sudo, netplan, apply]
 - [sudo, sed ,-i, 's/PasswordAuthentication no/PasswordAuthentication yes/g', /etc/ssh/sshd_config] 
 - [sudo, systemctl, restart, sshd]

#cloud-config
package_upgrade: true
users:
  - name: USER
    groups: wheel
    lock_passwd: false
    passwd: PASSWORD_HASH
    shell: /bin/bash
    ssh_pwauth: true
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - ssh-rsa YOUT_KEY

write_files:
  - path:  /etc/sysconfig/network-scripts/ifcfg-INTERFACE
    permissions: '0644'
    content: |
         TYPE="Ethernet"
         PROXY_METHOD="none"
         BROWSER_ONLY="no"
         BOOTPROTO="none"
         IPV4_FAILURE_FATAL="no"
         NAME="INTERFACE"
         DEVICE="INTERFACE"
         ONBOOT="yes"
         IPADDR="IP"
         PREFIX="PREFIX"
         GATEWAY="GATEWAY"
         DNS1="DNS"
         ZONE=ZONE

  - path:  /etc/sysconfig/network-scripts/ifcfg-INTERFACE
    permissions: '0644'
    content: |
         TYPE=Ethernet
         PROXY_METHOD=none
         BROWSER_ONLY=no
         BOOTPROTO=none
         NAME=INTERFACE
         DEVICE=INTERFACE
         ONBOOT=yes
         IPADDR=IP
         PREFIX=PREFIX
         IPV6_PRIVACY=no
         ZONE=ZONE







runcmd:
 - [sudo, ifup, INTERFACE]
 - [sudo, ifup, INTERFACE]
 - [sudo, sed ,-i, 's/PasswordAuthentication no/PasswordAuthentication yes/g', /etc/ssh/sshd_config]
 - [sudo, systemctl, restart, sshd]