├── .gitignore ├── handlers └── main.yml ├── templates ├── kernel.j2 ├── finish-installation.j2 ├── deploy-args-bionic.j2 └── deploy-args-ubuntu-template.j2 ├── meta └── main.yml ├── files ├── ubuntu-bionic.seed └── ubuntu-template.seed ├── defaults └── main.yml ├── tasks └── main.yml ├── LICENSE.txt └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .venv 3 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: sleep 3 | pause: 4 | seconds: 10 5 | -------------------------------------------------------------------------------- /templates/kernel.j2: -------------------------------------------------------------------------------- 1 | -kernel /tmp/{{ distributions[item.key] }}-kernel 2 | -initrd /tmp/{{ item.key }}-initrd.gz 3 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | galaxy_info: 2 | role_name: proxmox_vms 3 | author: Michael Kunze, Martin Lesser, Karl-Erik Kley 4 | description: Creates virtual machines with Proxmox and installs (unattended) an Ubuntu distribution on it 5 | company: Inoxio Quality Services GmbH 6 | 7 | license: Apache 2.0 8 | 9 | min_ansible_version: 2.6.4 10 | 11 | platforms: 12 | - name: Debian 13 | versions: 14 | - all 15 | - name: Ubuntu 16 | versions: 17 | - all 18 | 19 | galaxy_tags: 20 | - inoxio 21 | - proxmox 22 | - ubuntu 23 | - virtual 24 | - vm 25 | - installation 26 | - setup 27 | - unattended 28 | - automatic 29 | 30 | dependencies: 31 | - role: geerlingguy.pip 32 | pip_package: python3-pip 33 | pip_install_packages: 34 | - name: proxmoxer 35 | version: 1.0.3 36 | -------------------------------------------------------------------------------- /templates/finish-installation.j2: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e -x 4 | 5 | echo "" 6 | echo "START finish-installation" 7 | echo "===============================================================" 8 | 9 | ################################################################################ 10 | ## grub options 11 | ################################################################################ 12 | 13 | sed -i 's,quiet splash,,' /etc/default/grub 14 | update-grub 15 | 16 | ################################################################################ 17 | ## Packages 18 | ################################################################################ 19 | 20 | apt install --assume-yes openssh-client openssh-server {% if item.item.value.additional_packages is defined %}\ 21 | {{ item.item.value.additional_packages | join(' ') }} 22 | {% endif %} 23 | 24 | ################################################################################ 25 | ## Paste Scripts from files/scripts 26 | ################################################################################ 27 | 28 | {% if item.item.value.scripts is defined %} 29 | {% for script in item.item.value.scripts %} 30 | {{ lookup('file', '../../../'+script) }} 31 | {% endfor %} 32 | {% endif %} 33 | 34 | ################################################################################ 35 | ## Cleanup 36 | ################################################################################ 37 | 38 | apt clean 39 | fstrim -a 40 | 41 | 42 | echo "===============================================================" 43 | echo "END finish-installation" 44 | echo "" 45 | -------------------------------------------------------------------------------- /templates/deploy-args-bionic.j2: -------------------------------------------------------------------------------- 1 | {% include 'kernel.j2' %} 2 | -append "preseed/file=/preseed.cfg 3 | debian-installer/allow_unauthenticated_ssl=true 4 | locale={{ item.value.locale }}.UTF-8 5 | debian/priority=critical 6 | vga=normal 7 | debian-installer/keymap={{ item.value.locale[:2] }} 8 | console-keymaps-at/keymap={{ item.value.locale[:2] }} 9 | console-setup/ask_detect=false 10 | console-setup/layoutcode={{ item.value.locale }} 11 | keyboard-configuration/ask_detect=false 12 | keyboard-configuration/layoutcode={{ item.value.locale }} 13 | netcfg/choose_interface=auto 14 | localechooser/translation/warn-light=true 15 | localechooser/translation/warn-severe=true 16 | netcfg/get_hostname={{ item.key }} 17 | passwd/root-password={{ item.value.root_password }} 18 | passwd/root-password-again={{ item.value.root_password }} 19 | {% if item.value.network is defined %} 20 | {% if item.value.network.domainname is defined %} 21 | netcfg/get_domain={{ item.value.network.domainname }} 22 | {% endif %} 23 | {% if ( item.value.network.ip is defined and item.value.network.netmask is defined ) %} 24 | netcfg/confirm_static=true 25 | netcfg/disable_dhcp=true 26 | netcfg/get_ipaddress={{ item.value.network.ip }} 27 | netcfg/get_netmask={{ item.value.network.netmask }} 28 | {% if item.value.network.gateway is defined %} 29 | netcfg/get_gateway={{ item.value.network.gateway }} 30 | {% endif %} 31 | {% if item.value.network.nameserver is defined %} 32 | netcfg/get_nameservers={{ item.value.network.nameserver }} 33 | {% endif %} 34 | {% endif %} 35 | {% else %} 36 | netcfg/get_domain=localdomain 37 | {% endif %} 38 | FRONTEND_BACKGROUND=original" 39 | -------------------------------------------------------------------------------- /templates/deploy-args-ubuntu-template.j2: -------------------------------------------------------------------------------- 1 | {% include 'kernel.j2' %} 2 | -append "preseed/file=/preseed.cfg 3 | debian-installer/allow_unauthenticated_ssl=true 4 | locale={{ item.value.locale }}.UTF-8 5 | debian/priority=critical 6 | vga=normal 7 | debian-installer/keymap={{ item.value.locale[:2] }} 8 | console-keymaps-at/keymap={{ item.value.locale[:2] }} 9 | console-setup/ask_detect=false 10 | console-setup/layoutcode={{ item.value.locale }} 11 | keyboard-configuration/ask_detect=false 12 | keyboard-configuration/layoutcode={{ item.value.locale }} 13 | netcfg/choose_interface=auto 14 | localechooser/translation/warn-light=true 15 | localechooser/translation/warn-severe=true 16 | netcfg/get_hostname={{ item.key }} 17 | passwd/root-password={{ item.value.root_password }} 18 | passwd/root-password-again={{ item.value.root_password }} 19 | {% if item.value.network is defined %} 20 | {% if item.value.network.domainname is defined %} 21 | netcfg/get_domain={{ item.value.network.domainname }} 22 | {% endif %} 23 | {% if ( item.value.network.ip is defined and item.value.network.netmask is defined ) %} 24 | netcfg/confirm_static=true 25 | netcfg/disable_dhcp=true 26 | netcfg/get_ipaddress={{ item.value.network.ip }} 27 | netcfg/get_netmask={{ item.value.network.netmask }} 28 | {% if item.value.network.gateway is defined %} 29 | netcfg/get_gateway={{ item.value.network.gateway }} 30 | {% endif %} 31 | {% if item.value.network.nameserver is defined %} 32 | netcfg/get_nameservers={{ item.value.network.nameserver }} 33 | {% endif %} 34 | {% endif %} 35 | {% else %} 36 | netcfg/get_domain=localdomain 37 | {% endif %} 38 | FRONTEND_BACKGROUND=original" 39 | -------------------------------------------------------------------------------- /files/ubuntu-bionic.seed: -------------------------------------------------------------------------------- 1 | # 2 | # Preseed file for an automatic generic bionic installation. 3 | # 4 | # This will complete a standard full-disk installation. Everything on one 5 | # partition. 6 | # 7 | 8 | # the installation system will display only critical messages and try to do the right thing without fuss 9 | d-i debconf/priority string critical 10 | 11 | # time-zone 12 | d-i time/zone string Europe/Berlin 13 | 14 | # package management 15 | d-i mirror/http/hostname string archive.ubuntu.com 16 | 17 | # partitioning 18 | # crypto (instead of regular) for encryption 19 | d-i partman-auto/method string regular 20 | # all files in one partition 21 | d-i partman-auto/choose_recipe select atomic 22 | # automatically partition without confirmation 23 | d-i partman-partitioning/confirm_write_new_label boolean true 24 | d-i partman/choose_partition select finish 25 | d-i partman/confirm boolean true 26 | d-i partman/confirm_nooverwrite boolean true 27 | 28 | # login 29 | d-i passwd/root-login boolean true 30 | d-i user-setup/allow-password-weak boolean true 31 | d-i passwd/make-user boolean false 32 | 33 | # finish installation 34 | # avoid that last message about the install being complete 35 | d-i finish-install/reboot_in_progress note 36 | # comment to next line to reboot instead (but this starts the installer again!) 37 | d-i debian-installer/exit/poweroff boolean true 38 | 39 | # late command 40 | d-i preseed/late_command string \ 41 | cp /finish-installation /target/ ; \ 42 | in-target /bin/bash /finish-installation -------------------------------------------------------------------------------- /files/ubuntu-template.seed: -------------------------------------------------------------------------------- 1 | # 2 | # Preseed file for an automatic generic ubuntu installation. 3 | # 4 | # This will complete a standard full-disk installation. Everything on one 5 | # partition. 6 | # 7 | 8 | # the installation system will display only critical messages and try to do the right thing without fuss 9 | d-i debconf/priority string critical 10 | 11 | # time-zone 12 | d-i time/zone string Europe/Berlin 13 | 14 | # package management 15 | d-i mirror/http/hostname string archive.ubuntu.com 16 | 17 | # partitioning 18 | # crypto (instead of regular) for encryption 19 | d-i partman-auto/method string regular 20 | # all files in one partition 21 | d-i partman-auto/choose_recipe select atomic 22 | # automatically partition without confirmation 23 | d-i partman-partitioning/confirm_write_new_label boolean true 24 | d-i partman/choose_partition select finish 25 | d-i partman/confirm boolean true 26 | d-i partman/confirm_nooverwrite boolean true 27 | 28 | # login 29 | d-i passwd/root-login boolean true 30 | d-i user-setup/allow-password-weak boolean true 31 | d-i passwd/make-user boolean false 32 | 33 | # finish installation 34 | # avoid that last message about the install being complete 35 | d-i finish-install/reboot_in_progress note 36 | # comment to next line to reboot instead (but this starts the installer again!) 37 | d-i debian-installer/exit/poweroff boolean true 38 | 39 | # late command 40 | d-i preseed/late_command string \ 41 | cp /finish-installation /target/ ; \ 42 | in-target /bin/bash /finish-installation -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | proxmox_vms_defaults: 4 | # Specify emulated CPU type. 5 | # 'host' uses the same cpu type as the host system 6 | cpu: "host" 7 | 8 | # A hash/dictionary of network interfaces for the VM. net='{"key":"value", "key":"value"}'. 9 | # Keys allowed are - net[n] where 0 ≤ n ≤ N. 10 | # Virtio was chosen to be the main platform for IO virtualization in KVM 11 | # The bridge parameter can be used to automatically add the interface to a bridge device. 12 | # The Proxmox VE standard bridge is called 'vmbr0'. 13 | # Option rate is used to limit traffic bandwidth from and to this interface. 14 | # It is specified as floating point number, unit is 'Megabytes per second'. 15 | net: '{"net0":"virtio,bridge=vmbr0"}' 16 | 17 | # Specify number of cores per socket. 18 | cores: '2' 19 | 20 | # Memory size in MB for instance. 21 | memory_size: '2048' 22 | 23 | # Specify the amount of RAM for the VM in MB. Using zero disables the balloon driver. 24 | # The virtio balloon device allows KVM guests to reduce their memory size (thus relinquishing memory to the host) and 25 | # to increase it back (thus taking memory from the host). 26 | balloon: '1024' 27 | 28 | # Specifies the SCSI controller model. 29 | # Choices: lsi, lsi53c810, virtio-scsi-pci, virtio-scsi-single, megasas, pvscsi 30 | # virtio-scsi-pci: A virtio storage interface for efficient I/O that overcomes virtio-blk limitations and 31 | # supports advanced SCSI hardware. 32 | scsihw: 'virtio-scsi-pci' 33 | 34 | # A hash/dictionary of volume used as VIRTIO hard disk. virtio='{"key":"value", "key":"value"}'. 35 | # Keys allowed are - virto[n] where 0 ≤ n ≤ 15. 36 | # Values allowed are - "storage:size,format=value". 37 | # storage is the storage identifier where to create the disk. 38 | # size is the size of the disk in GB. 39 | # format is the drive's backing file's data format. qcow2|raw|subvol 40 | virtio: '{"virtio0":"local-lvm:10,cache=writeback,discard=on"}' 41 | 42 | # Specifies guest operating system. This is used to enable special optimization/features for specific operating systems. 43 | # The l26 is Linux 2.6/3.X Kernel. 44 | # Choices: other, wxp, w2k, w2k3, w2k8, wvista, win7, win8, l24, l26, solaris 45 | ostype: 'l26' 46 | 47 | # Locale is a set of parameters that defines the user's language. 48 | # This is not necessary for proxmox_kvm but for the preseed-file. This value is commited to the deploy-args file. 49 | locale: 'en_US' 50 | 51 | # Specifies whether a VM will be started during system bootup. 52 | onboot: 'yes' 53 | 54 | # Specifies the name of the file which contains the deploy arguments for the vm. The arguments in it are attached 55 | # to the args variable of a vm (see 'create vms' in tasks/main.yml) and are used when installing an os on a vm. 56 | deploy_args_template: 'deploy-args-ubuntu-template.j2' 57 | 58 | # Specifies the name of the template preseed file, which will be taken if the definition of the vm in the playbook 59 | # has no preseed path. 60 | preseed_template: 'ubuntu-template.seed' 61 | 62 | # Specifies the ubuntu distribution which will be installed on the vm. 63 | ubuntu_distribution: 'bionic' 64 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Check if deploy-args file exists 4 | stat: 5 | path: "{{ role_path }}/templates/deploy-args-{{ item.value.ubuntu_distribution | 6 | default(proxmox_vms_defaults.ubuntu_distribution) }}.j2" 7 | delegate_to: localhost 8 | become: false 9 | with_dict: "{{ vms }}" 10 | register: deploy_file_exists 11 | 12 | - name: Make a bool list if deploy file exists for every vm 13 | set_fact: 14 | deploy_file_exists_list: "{{ deploy_file_exists_list | default({}) | 15 | combine( {item.item.key: item.stat.exists} ) }}" 16 | with_items: "{{ deploy_file_exists.results }}" 17 | 18 | - name: Check if preseed file exists 19 | stat: 20 | path: "{{ role_path }}/files/ubuntu-{{ item.value.ubuntu_distribution | 21 | default(proxmox_vms_defaults.ubuntu_distribution) }}.seed" 22 | delegate_to: localhost 23 | become: false 24 | with_dict: "{{ vms }}" 25 | register: preseed_file_exists 26 | 27 | - name: Make a bool list if preseed file exists for every vm 28 | set_fact: 29 | preseed_file_exists_list: "{{ preseed_file_exists_list | default({}) | 30 | combine( {item.item.key: item.stat.exists} ) }}" 31 | with_items: "{{ preseed_file_exists.results }}" 32 | 33 | - name: Make a distribution list for shorter statements 34 | set_fact: 35 | distributions: "{{ distributions | default({}) | combine( {item.key: item.value.ubuntu_distribution | 36 | default(proxmox_vms_defaults.ubuntu_distribution)} ) }}" 37 | with_items: "{{ query('dict', vms) }}" 38 | 39 | - name: Create virtual machines 40 | proxmox_kvm: 41 | api_user: "{{ proxmox.api_user }}" 42 | api_password: "{{ proxmox.api_password }}" 43 | api_host: "{{ proxmox.api_host }}" 44 | node: "{{ item.value.node }}" 45 | name: "{{ item.key }}" 46 | net: "{{ item.value.net | default(proxmox_vms_defaults.net) }}" 47 | scsihw: "{{ item.value.scsihw | default(proxmox_vms_defaults.scsihw) }}" 48 | virtio: "{{ item.value.virtio | default(proxmox_vms_defaults.virtio) }}" 49 | cores: "{{ item.value.cores | default(proxmox_vms_defaults.cores) }}" 50 | memory: "{{ item.value.memory_size | default(proxmox_vms_defaults.memory_size) }}" 51 | balloon: "{{ item.value.balloon | default(proxmox_vms_defaults.balloon) }}" 52 | vga: "qxl" 53 | ostype: "{{ item.value.ostype | default(proxmox_vms_defaults.ostype) }}" 54 | # Look for deploy-args-file of the vm and remove all newlines. If not found take the 55 | # template-file and remove all newlines (all commands in one line). See ansible filter documentation. 56 | args: "{{ lookup('template', 'deploy-args-'~distributions[item.key]~'.j2' 57 | if deploy_file_exists_list[item.key] else proxmox_vms_defaults.deploy_args_template) | 58 | replace('\n', '') }}" 59 | cpu: "{{ item.value.cpu | default(proxmox_vms_defaults.cpu) }}" 60 | onboot: "{{ item.value.onboot | default(proxmox_vms_defaults.onboot) }}" 61 | state: present 62 | with_dict: "{{ vms }}" 63 | loop_control: 64 | pause: 10 65 | notify: sleep 66 | register: created_vms_pve 67 | 68 | - meta: flush_handlers 69 | 70 | - name: Create temporary directory for netboot image 71 | file: 72 | path: /tmp/{{ distributions[item.item.key] }} 73 | state: directory 74 | mode: 0755 75 | with_items: "{{ created_vms_pve.results }}" 76 | when: item is changed 77 | 78 | - name: Download and unpack netboot image 79 | unarchive: 80 | src: "http://archive.ubuntu.com/ubuntu/dists/{{ distributions[item.item.key] }}/\ 81 | main/installer-amd64/current/images/netboot/netboot.tar.gz" 82 | dest: /tmp/{{ distributions[item.item.key] }} 83 | remote_src: yes 84 | with_items: "{{ created_vms_pve.results }}" 85 | when: item is changed 86 | 87 | - name: Move linux kernel file 88 | copy: 89 | src: /tmp/{{ distributions[item.item.key] }}/ubuntu-installer/amd64/linux 90 | dest: /tmp/{{ distributions[item.item.key] }}-kernel 91 | remote_src: yes 92 | with_items: "{{ created_vms_pve.results }}" 93 | when: item is changed 94 | 95 | - name: Create temporary directory for initrd 96 | file: 97 | path: /tmp/{{ distributions[item.item.key] }}/{{ item.item.key }}-initrd 98 | state: directory 99 | mode: 0755 100 | with_items: "{{ created_vms_pve.results }}" 101 | when: item is changed 102 | 103 | - name: Unpack initrd 104 | shell: zcat /tmp/{{ distributions[item.item.key] }}/ubuntu-installer/amd64/initrd.gz | cpio -id 105 | args: 106 | chdir: /tmp/{{ distributions[item.item.key] }}/{{ item.item.key }}-initrd 107 | with_items: "{{ created_vms_pve.results }}" 108 | when: item is changed 109 | 110 | - name: Copy preseed file 111 | copy: 112 | src: "{{ 'files/ubuntu-'~distributions[item.item.key]~'.seed' if preseed_file_exists_list[item.item.key] 113 | else proxmox_vms_defaults.preseed_template }}" 114 | dest: /tmp/{{ distributions[item.item.key] }}/{{ item.item.key }}-initrd/preseed.cfg 115 | with_items: "{{ created_vms_pve.results }}" 116 | when: item is changed 117 | 118 | - name: Build post installation script 119 | template: 120 | src: templates/finish-installation.j2 121 | dest: /tmp/{{ distributions[item.item.key] }}/{{ item.item.key }}-initrd/finish-installation 122 | with_items: "{{ created_vms_pve.results }}" 123 | when: item is changed 124 | 125 | - name: Repack initrd 126 | shell: find . | cpio --create --format='newc' | gzip > /tmp/{{ item.item.key }}-initrd.gz 127 | args: 128 | chdir: /tmp/{{ distributions[item.item.key] }}/{{ item.item.key }}-initrd 129 | with_items: "{{ created_vms_pve.results }}" 130 | when: item is changed 131 | 132 | - name: Delete temporary directory 133 | file: 134 | state: absent 135 | path: /tmp/{{ distributions[item.item.key] }} 136 | with_items: "{{ created_vms_pve.results }}" 137 | when: item is changed 138 | 139 | - name: Start ubuntu setup 140 | proxmox_kvm: 141 | api_user: "{{ proxmox.api_user }}" 142 | api_password: "{{ proxmox.api_password }}" 143 | api_host: "{{ proxmox.api_host }}" 144 | node: "{{ item.item.value.node }}" 145 | name: "{{ item.item.key }}" 146 | state: started 147 | with_items: "{{ created_vms_pve.results }}" 148 | notify: sleep 149 | when: item is changed 150 | 151 | - meta: flush_handlers 152 | 153 | - name: Run ubuntu setup only once 154 | proxmox_kvm: 155 | api_user: "{{ proxmox.api_user }}" 156 | api_password: "{{ proxmox.api_password }}" 157 | api_host: "{{ proxmox.api_host }}" 158 | node: "{{ item.item.value.node }}" 159 | name: "{{ item.item.key }}" 160 | delete: args 161 | with_items: "{{ created_vms_pve.results }}" 162 | when: item is changed 163 | 164 | - name: Wait for virtual machines to finish installation 165 | proxmox_kvm: 166 | api_user: "{{ proxmox.api_user }}" 167 | api_password: "{{ proxmox.api_password }}" 168 | api_host: "{{ proxmox.api_host }}" 169 | node: "{{ item.item.value.node }}" 170 | name: "{{ item.item.key }}" 171 | state: current 172 | with_items: "{{ created_vms_pve.results }}" 173 | register: current_vm_state 174 | until: current_vm_state.status == "stopped" 175 | retries: 60 176 | delay: 10 177 | when: item is changed 178 | 179 | - name: Restart vms 180 | proxmox_kvm: 181 | api_user: "{{ proxmox.api_user }}" 182 | api_password: "{{ proxmox.api_password }}" 183 | api_host: "{{ proxmox.api_host }}" 184 | node: "{{ item.item.value.node }}" 185 | name: "{{ item.item.key }}" 186 | state: started 187 | with_items: "{{ created_vms_pve.results }}" 188 | when: item is changed 189 | 190 | - name: Wait for virtual machines to finish rebooting 191 | wait_for: 192 | port: 22 193 | host: "{{ item.item.value.network.ip }}" 194 | search_regex: OpenSSH 195 | delay: 10 196 | timeout: 480 197 | msg: Timeout or polling failed. 198 | with_items: "{{ created_vms_pve.results }}" 199 | when: item is changed 200 | 201 | - name: Delete initrd 202 | file: 203 | state: absent 204 | path: /tmp/{{ item.item.key }}-initrd.gz 205 | with_items: "{{ created_vms_pve.results }}" 206 | when: item is changed 207 | 208 | - name: Delete kernel 209 | file: 210 | state: absent 211 | path: /tmp/{{ distributions[item.item.key] }}-kernel 212 | with_items: "{{ created_vms_pve.results }}" 213 | when: item is changed 214 | 215 | - name: Print success message 216 | debug: 217 | msg: "The installation of {{ distributions[item.item.key] }} on {{ item.item.key }} was successful!" 218 | with_items: "{{ created_vms_pve.results }}" 219 | when: item is changed 220 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 inoxio Quality Services GmbH 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ansible Role: Create virtual machines with Proxmox and install (unattended) Ubuntu distributions on them 2 | ========= 3 | 4 | The inoxio.proxmox_vms role creates all VMs that are listed within 5 | 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. 6 | 7 | This role was developed based on [morph027's pve-infra-poc role](https://gitlab.com/morph027/pve-infra-poc). 8 | 9 | The main.yml in the 'default' directory contains default values for the VMs. 10 | 11 | The main.yml of the 'tasks' directory contains all tasks to create virtual machines and to install the operating system. 12 | At first the VMs will be created. After that the tasks for preparing the OS installations will start. 13 | The newest netboot version of the given Ubuntu version will be fetched and unzipped to get the initrd and kernel. 14 | Then the preseed file and finish-installation-script will be moved into the initrd and packed afterwards. 15 | After that the installation begins and the installation-arguments will be deleted 16 | (in order to start the installed OS after the next reboot). 17 | The VMs will be rebooted after finishing all installations and a success message will be displayed when reboot 18 | was successful (by polling the IP of the VM and searching for 'OpenSSH'). 19 | 20 | The files for the preseed installation (netboot image, kernel and initrd) and the Proxmoxer module will be copied to 21 | the host machine. If you want to use multiple nodes on the host machine, all nodes will get these files. 22 | 23 | Requirements 24 | ------------ 25 | 26 | Running Proxmox server. 27 | 28 | Dependencies 29 | ------------ 30 | 31 | geerlingguy.pip (used to install Proxmoxer) 32 | 33 | Role Variables 34 | -------------- 35 | 36 | api_user, api_password, api_host: these are needed to log into the Proxmox server. 37 | 38 | * File: defaults/main.yml 39 | * `Defaults`: These variables are the standard configuration for a VM. 40 | * `cpu`: Specify emulated CPU type. 'host' uses the same CPU type as the host system. 41 | * Default: `host` 42 | * `net`: A hash/dictionary of network interfaces for the VM. net='{"key":"value", "key":"value"}'. 43 | Keys allowed are - net[n] where 0 ≤ n ≤ N. 44 | Virtio was chosen to be the main platform for IO virtualization in KVM. 45 | The bridge parameter can be used to automatically add the interface to a bridge device. 46 | The Proxmox VE standard bridge is called 'vmbr0'. 47 | Option rate is used to limit traffic bandwidth from and to this interface. 48 | It is specified as floating point number, unit is 'Megabytes per second'. 49 | * Default: `{"net0":"virtio,bridge=vmbr0"}` 50 | * `cores`: Specify number of cores per socket. 51 | * Default: `2` 52 | * `memory_size`: Memory size in MB for instance. 53 | * Default: `2048` 54 | * `balloon`: Specify the amount of RAM for the VM in MB. Using zero disables the balloon driver. 55 | The virtio balloon device allows KVM guests to reduce their memory size (thus relinquishing memory to the host) and 56 | to increase it back (thus taking memory from the host). 57 | * Default: `1024` 58 | * `scsihw`: Specifies the SCSI controller model. 59 | Choices: lsi, lsi53c810, virtio-scsi-pci, virtio-scsi-single, megasas, pvscsi 60 | virtio-scsi-pci: A virtio storage interface for efficient I/O that overcomes virtio-blk limitations and 61 | supports advanced SCSI hardware. 62 | * Default: `virtio-scsi-pci` 63 | * `virtio`: A hash/dictionary of volume used as VIRTIO hard disk. virtio='{"key":"value", "key":"value"}'. 64 | Keys allowed are - virto[n] where 0 ≤ n ≤ 15. 65 | Values allowed are - "storage:size,format=value". 66 | storage is the storage identifier where to create the disk. 67 | size is the size of the disk in GB. 68 | format is the drive's backing file's data format. qcow2|raw|subvol 69 | * Default: `{"virtio0":"local-lvm:10,cache=writeback,discard=on"}` 70 | * `ostype`: Specifies guest operating system. This is used to enable special optimization/features for specific operating systems. 71 | The l26 is Linux 2.6/3.X Kernel. 72 | Choices: other, wxp, w2k, w2k3, w2k8, wvista, win7, win8, l24, l26, solaris 73 | * Default: `l26` 74 | * `onboot`: Specifies whether a VM will be started during system bootup. 75 | * Default: `yes` 76 | * `locale`: Locale is a set of parameters that defines the user's language. 77 | This is not necessary for proxmox_kvm but for the preseed-file. This value is commited to the deploy-args file. 78 | * `deploy_args_template`: Specifies the name of the template file which contains the deploy arguments for the VM. The arguments in it are attached 79 | to the args variable of a VM (see 'Create VMs' in tasks/main.yml) and are used when installing an os on a VM. 80 | This file is used when the VM definition does not contain a path for a custom file. 81 | * `preseed_template`: Specifies the name of the template preseed file, which will be taken if the definition of the vm in the playbook 82 | has no preseed path. This file is used when the VM definition does not contain a path for a custom file. 83 | * `ubuntu_distribution`: Specifies the Ubuntu distribution which will be installed on the VM. 84 | 85 | * File: Your playbook (see example playbook) 86 | * `proxmox`: Contains login data for the Proxmox server which should be encrypted. You need a file which contains 87 | the password for the Ansible vault. 88 | Encrypting is done by the following command on the terminal: 89 | `ansible-vault encrypt_string --vault-id '' --name ''` 90 | Example: `ansible-vault encrypt_string --vault-id ~/.ansible.secret 'some_password' --name 'api_password'` 91 | * `api_user`: Specifies the user-name for the Proxmox login. If you use Linux Pluggable Authentication Modules (PAM), you have to add @PAM to the username. 92 | * `api_password`: Specifies the password of a user for Proxmox login. 93 | * `api_host`: Specifies the host name or ip of the Proxmox server. 94 | * `vms`: Lists all virtual machines to be installed. When some variables are not stated, the default values will be taken 95 | (see defaults in defaults/main.yml). 96 | * ``: Specifies the name of the VM. 97 | * `node`: Specifies the name of the node on the Proxmox server. Below the node the VM will be installed. 98 | * `ubuntu_distribution`: Specifies the ubuntu distribution which will be installed on the VM. See deployments in defaults/main.yml. 99 | * `locale`: Locale is a set of parameters that defines the user's language. 100 | * `root_password`: Specifies the root password of the VM. 101 | * `memory_size`: Specifies the size of memory in MB for the VM. 102 | * `virtio`: Specifies the the hard-disk and its size to be used by the VM. See default/main.yml. 103 | * `network`: 104 | * `ip`: Specifies the ip address of the VM. 105 | * `netmask`: Specifies the netmask to be used by the VM. 106 | * `gateway`: Specifies the gateway ip to be used by the VM. 107 | * `nameserver`: Specifies the nameserver ips to be used by the VM. 108 | * `domainname`: Specifies the domain name of the VM. 109 | * `additional_packages`: (Optional/No Default) Contains a list of additional packages that will be installed. 110 | * `scripts`: (Optional/No Default) Scripts is a list of files whose content will be inserted into the finish-installation file. 111 | E.g. copying ssh keys from AWS. 112 | 113 | Example playbook 114 | ---------------- 115 | 116 | ``` 117 | - hosts: # see hosts file 118 | remote_user: root 119 | 120 | roles: 121 | - role: inoxio.proxmox_vms 122 | proxmox: 123 | api_user: (@PAM in case you use Linux PAM) 124 | api_password: 125 | api_host: 126 | vms: 127 | : 128 | node: 129 | ubuntu_distribution: 130 | locale: en_US 131 | root_password: 132 | memory_size: 133 | virtio: '{"virtio0":"local-lvm:,cache=writeback,discard=on"}' 134 | network: 135 | ip: 136 | netmask: 137 | gateway: 138 | nameserver: 139 | domainname: 140 | additional_packages: 141 | - curl 142 | - gnupg 143 | scripts: 144 | - files/scripts/my_script.sh 145 | 146 | : 147 | node: 148 | root_password: 149 | ubuntu_distribution: 'xenial' 150 | memory_size: 151 | virtio: '{"virtio0":"local-lvm:,cache=writeback,discard=on"}' 152 | network: 153 | ip: 154 | netmask: 155 | gateway: 156 | nameserver: 157 | domainname: 158 | 159 | ``` 160 | 161 | 162 | Add new Ubuntu distribution 163 | ------------ 164 | 165 | To add a new Ubuntu distribution you have the following options: 166 | * You can add the deploy-args file in templates directory. E.g. deploy-args-cosmic.j2. But this is optional. 167 | The role tries to find a deploy-args file named like the given distribution name and if it can't find it the default 168 | preseed file will be taken. 169 | * You can add preseed-file in files directory. E.g. ubuntu-cosmic.seed. But this is optional. 170 | The role tries to find a preseed file named like the given distribution name and if it can't find it the default 171 | preseed file will be taken. 172 | 173 | Deploy Arguments 174 | ------------ 175 | 176 | The deploy-args-file delivers necessary settings for an unattended Ubuntu installation. 177 | These settings could be delivered by the preseed-file itself, but since the deploy-args file is a Jinja2 file (see 178 | Jinja2 documentation: http://jinja.pocoo.org/docs/2.10/) you can code which settings should be used for given 179 | parameters. You can also include other files. And use Ansible variables to make the settings dependending on the VM 180 | definition. E.g. locale={{ item.value.locale }}.UTF-8. If the local is en_US, Jinja2 replaces it to 181 | locale=en_US.UTF-8 182 | 183 | Preseed File 184 | ------------ 185 | 186 | The preseed-file contains settings for a unattended installation of an Ubuntu distribution. E.g. information how the disk 187 | should be partitioned, which user should be created and so on. It contains a section for late-commands. These are 188 | commands that will be executed when the installation is done and will be executed in the installed os itself. The 189 | late-commands can be delivered in a shell-script. 190 | 191 | Finish-installation 192 | ------------ 193 | 194 | The finish-installation-template will be executed when the OS installation is finished. It can be used for example 195 | to install packages or transfer SSH-keys. The file is a Jinja2 template file and will add additional packages, which 196 | are stated in the VM definition, and it adds scripts, which can also be stated in the VM definition. 197 | 198 | Testing 199 | ------------ 200 | 201 | Automatic testing this role is difficult because you need a VM with Proxmox on it which creates the VMs within the VM itself. 202 | Thus a hypervisor is needed which supports nested virtualization. 203 | --------------------------------------------------------------------------------