inoxio / ansible-role-proxmox-vms

Ansible Role - Proxmox VMs
Apache License 2.0
48 stars 18 forks source link
ansible playbook proxmox role ubuntu unattended-installation

Ansible Role: Create virtual machines with Proxmox and install (unattended) Ubuntu distributions on them

The inoxio.proxmox_vms role creates all VMs that are listed within your role execution (see example playbook) and installs Ubuntu versions via preseeding on them. You can configure which Ubuntu version shall be installed on which VM.

This role was developed based on morph027's pve-infra-poc role.

The main.yml in the 'default' directory contains default values for the VMs.

The main.yml of the 'tasks' directory contains all tasks to create virtual machines and to install the operating system. At first the VMs will be created. After that the tasks for preparing the OS installations will start. The newest netboot version of the given Ubuntu version will be fetched and unzipped to get the initrd and kernel. Then the preseed file and finish-installation-script will be moved into the initrd and packed afterwards. After that the installation begins and the installation-arguments will be deleted (in order to start the installed OS after the next reboot). The VMs will be rebooted after finishing all installations and a success message will be displayed when reboot was successful (by polling the IP of the VM and searching for 'OpenSSH').

The files for the preseed installation (netboot image, kernel and initrd) and the Proxmoxer module will be copied to the host machine. If you want to use multiple nodes on the host machine, all nodes will get these files.

Requirements

Running Proxmox server.

Dependencies

geerlingguy.pip (used to install Proxmoxer)

Role Variables

api_user, api_password, api_host: these are needed to log into the Proxmox server.

Example playbook

- hosts: <host_name> # see hosts file
  remote_user: root

  roles:
    - role: inoxio.proxmox_vms
      proxmox:
        api_user: <encrypted user> (@PAM in case you use Linux PAM)
        api_password: <encrypted password>
        api_host: <encrypted host>
      vms:
        <vm1_name>:
          node: <node_name>
          ubuntu_distribution: <distribution_name>
          locale: en_US
          root_password:  <encrypted root password>
          memory_size: <ram_size_in_MB>
          virtio: '{"virtio0":"local-lvm:<disk_size_in_GB>,cache=writeback,discard=on"}'
          network:
            ip: <ip>
            netmask: <netmask>
            gateway: <gateway_ip>
            nameserver: <nameserver1> <nameserver2>
            domainname: <domainname>
          additional_packages:
             - curl
             - gnupg
          scripts:
            - files/scripts/my_script.sh

        <vm2_name>:
          node: <node_name>
          root_password: <encrypted root password>
          ubuntu_distribution: 'xenial'
          memory_size: <ram_size_in_MB>
          virtio: '{"virtio0":"local-lvm:<disk_size_in_GB>,cache=writeback,discard=on"}'
          network:
            ip: <ip>
            netmask: <netmask>
            gateway: <gateway_ip>
            nameserver: <nameserver1> <nameserver2>
            domainname: <domainname>

Add new Ubuntu distribution

To add a new Ubuntu distribution you have the following options:

Deploy Arguments

The deploy-args-file delivers necessary settings for an unattended Ubuntu installation. These settings could be delivered by the preseed-file itself, but since the deploy-args file is a Jinja2 file (see Jinja2 documentation: http://jinja.pocoo.org/docs/2.10/) you can code which settings should be used for given parameters. You can also include other files. And use Ansible variables to make the settings dependending on the VM definition. E.g. locale={{ item.value.locale }}.UTF-8. If the local is en_US, Jinja2 replaces it to locale=en_US.UTF-8

Preseed File

The preseed-file contains settings for a unattended installation of an Ubuntu distribution. E.g. information how the disk should be partitioned, which user should be created and so on. It contains a section for late-commands. These are commands that will be executed when the installation is done and will be executed in the installed os itself. The late-commands can be delivered in a shell-script.

Finish-installation

The finish-installation-template will be executed when the OS installation is finished. It can be used for example to install packages or transfer SSH-keys. The file is a Jinja2 template file and will add additional packages, which are stated in the VM definition, and it adds scripts, which can also be stated in the VM definition.

Testing

Automatic testing this role is difficult because you need a VM with Proxmox on it which creates the VMs within the VM itself. Thus a hypervisor is needed which supports nested virtualization.