Added Proxmox VM bootstrap role

This commit is contained in:
syrell 2023-08-19 12:53:03 +02:00
parent 0ca263c251
commit d4dd61e4b3
Signed by: syrell
GPG Key ID: BC9570E849334AF9
9 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,76 @@
= VM bootstrap
A simple role de bootstrap a VM instance on a Proxmox node. It is based on community.general Proxmox modules which are quite complete. Therefore, it is quite hard to maintain a role which build on every possibility. Hence, this one focus on cloning a VM from an existing template, allows you to modify some hardware desired parameters (mainly sockets, CPU cores, RAM and drive size) and finally start the VM. Also, you will need to make sure cloud-init related variables are already present on the template you want to use.
== Requirements
This role was written for Debian (11) and doesn't require root privileges. However, it
will need the `+community.general+` collection and two Python libraries you'll have to install using your favorite Python package manager :
- proxmoxer (which enables communication with Proxmox API)
- requests (for API calls)
== Role Variables
Variables can be found in the link:./defaults/main.yml[default vars file]. Here is a detailed description of each variable:
=== Required variables
- `+proxmox_user+` (str): User used to login to Proxmox
- `+proxmox_token+` (str): User token name
- `+proxmox_password+` (str): User token secret
- `+proxmox_host+` (str): DNS/IP address of Proxmox host
- `+proxmox_node+` (str): Proxmox node name
- `+proxmox_template+` (str): Proxmox template name
- `+vm_name+` (str): Name provided for the new VM
WARNING: `+vm_name+` must be valid (e.g. no underscores)
=== Additional variables
- `+socket_count+` (int): Number of sockets (defaults to 1 if undefined)
- `+cpu_count+` (int): Number of CPU cores (defaults to 1 if undefined)
- `+memory_count+` (int): RAM amount in MB (defaults to 1024 if undefined)
- `+disk_name+` (str): Main disk name (defaults to scsi0)
- `+disk_size+` (str): Desired size for disk `+disk_name+`
== Dependencies
None.
== Example Playbook
[source,yaml]
----
- name: Create a VM
hosts: localhost
vars:
proxmox_user: "root@pam"
proxmox_token: "mytoken"
proxmox_password: "password"
proxmox_host: "pve.example.lan"
proxmox_template: "vm-template"
proxmox_node: "main-node"
vm_name: "ansible-vm"
roles:
- role: 'bootstrap_vm'
----
== License
BSD-3
== Author Information
Role created by https://git.syyrell.com/syrell[syrell].

View File

@ -0,0 +1,11 @@
---
# defaults file for bootstrap_vm
proxmox_user: "root@pam"
proxmox_token: ""
proxmox_password: ""
proxmox_host: ""
proxmox_node: ""
proxmox_template: ""
vm_name: "ansible-vm"
disk_name: "scsi0"
disk_size: ""

View File

@ -0,0 +1,2 @@
---
# handlers file for bootstrap_vm

View File

@ -0,0 +1,52 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -0,0 +1,48 @@
- name: Bootstrap cloned VM
community.general.proxmox_kvm:
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_token }}"
api_token_secret: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
node: "{{ proxmox_node }}"
clone: "{{ proxmox_template }}"
name: "{{ vm_name }}"
state: present
- name: Update Hardware
community.general.proxmox_kvm:
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_token }}"
api_token_secret: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
node: "{{ proxmox_node }}"
name: "{{ vm_name }}"
sockets: "{{ socket_count | default(1) }}"
cores: "{{ cpu_count | default(1) }}"
memory: "{{ memory_count | default(1024) }}"
update: True
when: (socket_count is defined and socket_count != 0) or
(cpu_count is defined and cpu_count != 0) or
(memory_count is defined and memory_count != 0)
- name: Resize disk
community.general.proxmox_disk:
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_token }}"
api_token_secret: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
name: "{{ vm_name }}"
disk: "{{ disk_name }}"
size: "{{ disk_size }}"
state: resized
when: disk_size is defined and disk_size != 0
- name: Launch the VM
community.general.proxmox_kvm:
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_token }}"
api_token_secret: "{{ proxmox_password }}"
api_host: "{{ proxmox_host }}"
name: "{{ vm_name }}"
node: "{{ proxmox_node }}"
state: started

View File

@ -0,0 +1,19 @@
---
# tasks file for bootstrap_vm
- name: Verify required vars are fullfiled
assert:
that:
- "{{ item }} is defined"
- "{{ item }} | length > 0"
quiet: true
fail_msg: "{{ item }} is undefined or null"
loop:
- proxmox_token
- proxmox_password
- proxmox_host
- proxmox_node
- proxmox_template
- import_tasks: init_vm.yml
delegate_to: localhost

View File

@ -0,0 +1,2 @@
localhost

View File

@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- bootstrap_vm

View File

@ -0,0 +1,2 @@
---
# vars file for bootstrap_vm