Initial commit
commit
a9403fd090
@ -0,0 +1,27 @@
|
||||
# Role Name: create_vm_proxmox
|
||||
|
||||
A simple role to create/clone virtual machines in Proxmox.
|
||||
|
||||
## Role variables
|
||||
|
||||
TODO: Add table of role variables
|
||||
|
||||
## Dependencies
|
||||
|
||||
Python module on the host used in proxmox tasks
|
||||
- `requests`
|
||||
- `proxmoxer`
|
||||
|
||||
Bash is required for the connect tasks
|
||||
|
||||
## Example Playbook
|
||||
|
||||
Example playbook:
|
||||
|
||||
- hosts: virtual_servers
|
||||
roles:
|
||||
- create_vm_proxmox
|
||||
|
||||
## License
|
||||
|
||||
BSD-3-Clause License
|
||||
@ -0,0 +1,19 @@
|
||||
---
|
||||
# defaults file for Create-VM
|
||||
# Generic Variables #
|
||||
create_vm_proxmox_recreate_vm: false
|
||||
|
||||
# promxox specific default variables #
|
||||
create_vm_proxmox_start_vm: true
|
||||
create_vm_proxmox_configure_cloudinit: true
|
||||
create_vm_proxmox_enable_guest_agent: true
|
||||
create_vm_proxmox_start_vm_onboot: true
|
||||
|
||||
# cloud-init variables #
|
||||
create_vm_proxmox_cloudinit_storage_pool: local-lvm
|
||||
|
||||
# connection variables #
|
||||
create_vm_proxmox_connect_to_vm: true
|
||||
create_vm_proxmox_remove_ssh_host_key: true
|
||||
|
||||
...
|
||||
@ -0,0 +1,8 @@
|
||||
galaxy_info:
|
||||
role_name: create_vm_proxmox
|
||||
author: Ronald
|
||||
description: Creates virtual machines in Proxmox from templates
|
||||
|
||||
license: BSD-3-Clause
|
||||
|
||||
min_ansible_version: "2.17"
|
||||
@ -0,0 +1,36 @@
|
||||
---
|
||||
|
||||
- name: Ensure we don't have an incorrect SSH host key stored on the Ansible controller
|
||||
ansible.builtin.command:
|
||||
cmd: "ssh-keygen -R {{ ansible_host }}"
|
||||
register: create_vm_proxmox_remove_ssh_host_key_cmd
|
||||
changed_when:
|
||||
- '"# Host " + ansible_host + " found" in create_vm_proxmox_remove_ssh_host_key_cmd.stdout'
|
||||
when:
|
||||
- create_vm_proxmox_remove_ssh_host_key | bool
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Wait for SSH to become available
|
||||
ansible.builtin.wait_for:
|
||||
host: "{{ ansible_host }}"
|
||||
port: 22
|
||||
delay: 10
|
||||
timeout: 120
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Get SSH host key
|
||||
ansible.builtin.shell:
|
||||
cmd: "ssh-keyscan {{ ansible_host }} >> ~/.ssh/known_hosts"
|
||||
args:
|
||||
executable: /bin/bash
|
||||
changed_when: true
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Ensure we can connect
|
||||
ansible.builtin.wait_for_connection:
|
||||
connect_timeout: 10
|
||||
delay: 5
|
||||
sleep: 2
|
||||
timeout: 120
|
||||
|
||||
...
|
||||
@ -0,0 +1,34 @@
|
||||
---
|
||||
# tasks file for Create-VM
|
||||
|
||||
- name: Check that all required variables are declared
|
||||
ansible.builtin.fail:
|
||||
msg: "{{ item }} is undefined"
|
||||
when:
|
||||
- vars[item] is undefined
|
||||
loop: "{{ create_vm_proxmox_required_vars }}"
|
||||
|
||||
- name: Check that required variables for proxmox are defined
|
||||
ansible.builtin.fail:
|
||||
msg: "{{ item }} is undefined"
|
||||
loop: "{{ create_vm_proxmox_required_vars }}"
|
||||
when:
|
||||
- vars[item] is undefined
|
||||
|
||||
- name: Check that required variables for Proxmox cloud-init are defined
|
||||
ansible.builtin.fail:
|
||||
msg: "{{ item }} is undefined"
|
||||
loop: "{{ create_vm_proxmox_required_vars_cloudinit }}"
|
||||
when:
|
||||
- vars[item] is undefined
|
||||
|
||||
- name: Include Proxmox tasks
|
||||
ansible.builtin.include_tasks: proxmox.yml
|
||||
|
||||
- name: Include tasks to connect to the new VM
|
||||
ansible.builtin.include_tasks:
|
||||
file: connect.yml
|
||||
when:
|
||||
- create_vm_proxmox_connect_to_vm | bool
|
||||
|
||||
...
|
||||
@ -0,0 +1,119 @@
|
||||
---
|
||||
|
||||
- name: Check if VM already exists
|
||||
community.general.proxmox_vm_info:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
register: create_vm_proxmox_check_if_vm_exists
|
||||
when:
|
||||
- create_vm_proxmox_recreate_vm | bool
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Delete VM if it already exists
|
||||
community.general.proxmox_kvm:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
name: "{{ create_vm_proxmox_name }}"
|
||||
state: absent
|
||||
force: true
|
||||
when:
|
||||
- create_vm_proxmox_check_if_vm_exists.proxmox_vms is defined
|
||||
- create_vm_proxmox_check_if_vm_exists.proxmox_vms | length > 0
|
||||
- create_vm_proxmox_recreate_vm | bool
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Check if VM already exists
|
||||
community.general.proxmox_vm_info:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
register: create_vm_proxmox_check_if_vm_exists
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Clone VM in Proxmox
|
||||
community.general.proxmox_kvm:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
node: "{{ create_vm_proxmox_host }}"
|
||||
newid: "{{ create_vm_proxmox_vmid }}"
|
||||
name: "{{ create_vm_proxmox_name }}"
|
||||
clone: "{{ create_vm_proxmox_source_vm }}"
|
||||
vcpus: "{{ create_vm_proxmox_numb_cpus | default(omit) }}"
|
||||
cores: "{{ create_vm_proxmox_numb_cores | default(omit) }}"
|
||||
memory: "{{ create_vm_proxmox_memory_mb | default(omit) }}"
|
||||
virtio: "{{ create_vm_proxmox_virtio_disks | default(omit) }}"
|
||||
net: "{{ create_vm_proxmox_net | default(omit) }}"
|
||||
when:
|
||||
- create_vm_proxmox_check_if_vm_exists.proxmox_vms | length == 0
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Add cloud-init drive to VM
|
||||
community.general.proxmox_kvm:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
node: "{{ create_vm_proxmox_host }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
name: "{{ create_vm_proxmox_name }}"
|
||||
update: true
|
||||
update_unsafe: true
|
||||
ciuser: "{{ create_vm_proxmox_cloudinit_user }}"
|
||||
cipassword: "{{ create_vm_proxmox_cloudinit_user }}"
|
||||
sshkeys: "{{ create_vm_proxmox_cloudinit_sshkeys }}"
|
||||
ipconfig: "{{ create_vm_proxmox_cloudinit_ipconfig }}"
|
||||
nameservers: "{{ create_vm_proxmox_cloudinit_nameservers }}"
|
||||
searchdomains: "{{ create_vm_proxmox_cloudinit_searchdomains }}"
|
||||
ide:
|
||||
ide2: "{{ create_vm_proxmox_cloudinit_storage_pool }}:cloudinit,format=raw"
|
||||
when:
|
||||
- create_vm_proxmox_check_if_vm_exists.proxmox_vms | length == 0
|
||||
- create_vm_proxmox_configure_cloudinit | bool
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Start VM
|
||||
community.general.proxmox_kvm:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
node: "{{ create_vm_proxmox_host }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
name: "{{ create_vm_proxmox_name }}"
|
||||
state: started
|
||||
when:
|
||||
- create_vm_proxmox_start_vm
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Enable QEMU guest agent for VM
|
||||
community.general.proxmox_kvm:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
node: "{{ create_vm_proxmox_host }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
name: "{{ create_vm_proxmox_name }}"
|
||||
agent: true
|
||||
when:
|
||||
- create_vm_proxmox_enable_guest_agent | bool
|
||||
delegate_to: localhost
|
||||
|
||||
- name: Set VM to autostart
|
||||
community.general.proxmox_kvm:
|
||||
api_host: "{{ create_vm_proxmox_host }}"
|
||||
api_user: "{{ create_vm_proxmox_user }}"
|
||||
api_password: "{{ create_vm_proxmox_password }}"
|
||||
node: "{{ create_vm_proxmox_host }}"
|
||||
vmid: "{{ create_vm_proxmox_vmid }}"
|
||||
name: "{{ create_vm_proxmox_name }}"
|
||||
proxmox_default_behavior: no_defaults
|
||||
onboot: true
|
||||
when:
|
||||
- create_vm_proxmox_start_vm_onboot | bool
|
||||
delegate_to: localhost
|
||||
|
||||
...
|
||||
@ -0,0 +1,21 @@
|
||||
---
|
||||
# vars file for Create-VM
|
||||
|
||||
create_vm_proxmox_required_vars:
|
||||
- create_vm_proxmox_host
|
||||
- create_vm_proxmox_name
|
||||
- create_vm_proxmox_user
|
||||
- create_vm_proxmox_password
|
||||
- create_vm_proxmox_vmid
|
||||
- create_vm_proxmox_source_vm
|
||||
|
||||
create_vm_proxmox_required_vars_cloudinit:
|
||||
- create_vm_proxmox_cloudinit_storage_pool
|
||||
- create_vm_proxmox_cloudinit_user
|
||||
- create_vm_proxmox_cloudinit_password
|
||||
- create_vm_proxmox_cloudinit_sshkeys
|
||||
- create_vm_proxmox_cloudinit_ipconfig
|
||||
- create_vm_proxmox_cloudinit_nameservers
|
||||
- create_vm_proxmox_cloudinit_searchdomains
|
||||
|
||||
...
|
||||
Loading…
Reference in New Issue