Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Proxmox VM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSI Ansible
Proxmox VM
Commits
c81337a6
Verified
Commit
c81337a6
authored
1 month ago
by
David Mehren
Browse files
Options
Downloads
Patches
Plain Diff
The big HOA refactor™ that enables updating existing VMs
parent
65f1a478
No related branches found
No related tags found
No related merge requests found
Pipeline
#293442
failed
1 month ago
Stage: linting
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
tasks/create_vm.yml
+82
-0
82 additions, 0 deletions
tasks/create_vm.yml
tasks/main.yml
+44
-118
44 additions, 118 deletions
tasks/main.yml
tasks/update_vm.yml
+20
-0
20 additions, 0 deletions
tasks/update_vm.yml
with
146 additions
and
118 deletions
tasks/create_vm.yml
0 → 100644
+
82
−
0
View file @
c81337a6
---
-
name
:
Create temporary file
ansible.builtin.tempfile
:
state
:
file
register
:
tempfile
check_mode
:
false
-
name
:
Save SSH-keys to temporary file
ansible.builtin.copy
:
content
:
"
{{
proxmox_vm_sshkeys
}}"
dest
:
"
{{
tempfile.path
}}"
mode
:
"
0644"
-
name
:
Generate basic 'qm set' command line
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
--sshkey
{{
tempfile.path
}}
--ciuser
{{
proxmox_vm_user
}}
--cores
{{
proxmox_vm_cpu.cores
}}
--sockets
{{
proxmox_vm_cpu.sockets
}}
--numa
{{
proxmox_vm_cpu.numa
}}
--memory
{{
proxmox_vm_memory
}}"
# noqa yaml[line-length]
-
name
:
Generate ipconfig options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--ipconfig{{
index
}}
{{
item
}}"
loop
:
"
{{
proxmox_vm_ipconfig_list
}}"
loop_control
:
index_var
:
index
-
name
:
Generate cloud-init net options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--net{{
index
}}
virtio,{{
item
}}"
loop
:
"
{{
proxmox_vm_net_list
}}"
loop_control
:
index_var
:
index
when
:
proxmox_vm_net is defined
-
name
:
Generate startup order option
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--startup
{{
proxmox_vm_startup
}}"
when
:
proxmox_vm_startup is defined
-
name
:
Generate cloud-init nameserver option
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--nameserver
'{{
proxmox_vm_nameservers
|
join('
')
}}'"
when
:
proxmox_vm_nameservers is defined
-
name
:
Generate cloud-init searchdomain option
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--searchdomain
'{{
proxmox_vm_searchdomain
}}'"
when
:
proxmox_vm_searchdomain is defined
-
name
:
Generate ballooning options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--balloon
{{
proxmox_vm_min_memory
}}"
when
:
proxmox_vm_min_memory is defined
-
name
:
Generate hotplug options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--hotplug
{{
proxmox_vm_hotplug
}}"
when
:
proxmox_vm_hotplug is defined
-
name
:
Generate onboot options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--onboot
{{
proxmox_vm_onboot
}}"
when
:
proxmox_vm_onboot is defined
-
name
:
Get next free VM ID
ansible.builtin.command
:
pvesh get /cluster/nextid
register
:
vmid
changed_when
:
false
-
name
:
Clone template
# noqa no-changed-when
ansible.builtin.command
:
"
qm
clone
{{
proxmox_vm_templateid
}}
{{
vmid.stdout
}}
--name
{{
proxmox_vm_name
}}
--full
true"
# noqa yaml[line-length]
-
name
:
Resize harddisk
# noqa no-changed-when
ansible.builtin.command
:
"
qm
resize
{{
vmid.stdout
}}
scsi0
{{
proxmox_vm_disksize
}}"
-
name
:
Set VM options
# noqa no-changed-when
ansible.builtin.command
:
"
qm
set
{{
vmid.stdout
}}
{{
proxmox_create_vm_qm_args
}}"
-
name
:
Start VM
# noqa no-changed-when
ansible.builtin.command
:
"
qm
start
{{
vmid.stdout
}}"
-
name
:
Enable HA for VM
# noqa no-changed-when
ansible.builtin.command
:
"
ha-manager
add
{{
vmid.stdout
}}"
when
:
proxmox_vm_enable_ha is defined and proxmox_vm_enable_ha
This diff is collapsed.
Click to expand it.
tasks/main.yml
+
44
−
118
View file @
c81337a6
---
-
name
:
Fail if required settings are not defined
ansible.builtin.fail
:
msg
:
>-
Required setting `{{ item }}` is not defined
when
:
"
vars.get(item,
'')
==
''"
with_items
:
-
proxmox_vm_templateid
-
proxmox_vm_name
-
proxmox_vm_sshkeys
-
proxmox_vm_ipconfig
-
proxmox_vm_user
-
proxmox_vm_disksize
-
proxmox_vm_cpu
-
proxmox_vm_memory
-
name
:
Fail if required CPU settings are not defined
ansible.builtin.fail
:
msg
:
>-
Required setting `proxmox_vm_cpu.{{ item }}` is not defined
when
:
"
vars.get('proxmox_vm_cpu').get(item,
'')
==
''"
with_items
:
-
cores
-
sockets
-
name
:
Gather existing VMs
ansible.builtin.assert
:
that
:
-
proxmox_vm_templateid is defined
-
proxmox_vm_name is defined
-
proxmox_vm_sshkeys is defined
-
proxmox_vm_ipconfig is defined
-
proxmox_vm_user is defined
-
proxmox_vm_disksize is defined
-
proxmox_vm_cpu is defined
-
proxmox_vm_cpu.cores is defined
-
proxmox_vm_cpu.sockets is defined
-
proxmox_vm_memory is defined
fail_msg
:
"
A
required
parameter
is
not
defined"
quiet
:
true
-
name
:
Gather cluster resources
ansible.builtin.command
:
pvesh get /cluster/resources --output-format json
register
:
proxmox_vm_resources
register
:
proxmox_vm_resources
_json
changed_when
:
false
when
:
proxmox_vm_existing is not defined or proxmox_vm_existing == []
check_mode
:
false
# also run in check_mode
when
:
proxmox_vm_resources is not defined or proxmox_vm_existing == []
-
name
:
Save existing VMs
-
name
:
Parse cluster resource JSON
ansible.builtin.set_fact
:
proxmox_vm_existing
:
"
{{
proxmox_vm_resources.stdout
|
ansible.builtin.from_json
|
community.general.json_query('[?type==`qemu`].name')
}}"
# noqa yaml[line-length]
when
:
proxmox_vm_existing is not defined or proxmox_vm_existing == []
-
name
:
Create new VM
when
:
proxmox_vm_name not in proxmox_vm_existing
block
:
-
name
:
Create temporary file
ansible.builtin.tempfile
:
state
:
file
register
:
tempfile
proxmox_vm_resources
:
"
{{
proxmox_vm_resources_json.stdout
|
ansible.builtin.from_json
}}"
when
:
proxmox_vm_resources is not defined or proxmox_vm_existing == []
-
name
:
Save SSH-keys to temporary file
ansible.builtin.copy
:
content
:
"
{{
proxmox_vm_sshkeys
}}"
dest
:
"
{{
tempfile.path
}}"
mode
:
"
0644"
-
name
:
Extract VM facts
ansible.builtin.set_fact
:
proxmox_vm_existing
:
"
{{
proxmox_vm_resources
|
community.general.json_query('[?type==`qemu`].name')
}}"
# noqa yaml[line-length]
proxmox_vm_node
:
"
{{
proxmox_vm_resources
|
community.general.json_query(node_query)
|
first
|
default('')
}}"
# noqa yaml[line-length]
proxmox_vm_id
:
"
{{
proxmox_vm_resources
|
community.general.json_query(id_query)
|
first
|
default('')
}}"
# noqa yaml[line-length]
vars
:
node_query
:
"
[?name=='{{
proxmox_vm_name
}}'].node"
id_query
:
"
[?name=='{{
proxmox_vm_name
}}'].vmid"
-
name
:
Convert proxmox_vm_net to list
ansible.builtin.set_fact
:
...
...
@@ -55,74 +43,12 @@
-
name
:
Convert proxmox_vm_ipconfig to list
ansible.builtin.set_fact
:
proxmox_vm_ipconfig_list
:
"
{{
proxmox_vm_ipconfig
is
string
|
ternary([proxmox_vm_ipconfig],
proxmox_vm_ipconfig)
}}"
# noqa yaml[line-length]
-
name
:
Generate basic 'qm set' command line
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
--sshkey
{{
tempfile.path
}}
--ciuser
{{
proxmox_vm_user
}}
--cores
{{
proxmox_vm_cpu.cores
}}
--sockets
{{
proxmox_vm_cpu.sockets
}}
--numa
{{
proxmox_vm_cpu.numa
}}
--memory
{{
proxmox_vm_memory
}}"
# noqa yaml[line-length]
-
name
:
Generate ipconfig options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--ipconfig{{
index
}}
{{
item
}}"
loop
:
"
{{
proxmox_vm_ipconfig_list
}}"
loop_control
:
index_var
:
index
-
name
:
Generate cloud-init net options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--net{{
index
}}
virtio,{{
item
}}"
loop
:
"
{{
proxmox_vm_net_list
}}"
loop_control
:
index_var
:
index
when
:
proxmox_vm_net is defined
-
name
:
Generate startup order option
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--startup
{{
proxmox_vm_startup
}}"
when
:
proxmox_vm_startup is defined
-
name
:
Generate cloud-init nameserver option
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--nameserver
'{{
proxmox_vm_nameservers
|
join('
')
}}'"
# noqa yaml[line-length]
when
:
proxmox_vm_nameservers is defined
-
name
:
Generate cloud-init searchdomain option
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--searchdomain
'{{
proxmox_vm_searchdomain
}}'"
when
:
proxmox_vm_searchdomain is defined
-
name
:
Generate ballooning options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--balloon
{{
proxmox_vm_min_memory
}}"
when
:
proxmox_vm_min_memory is defined
-
name
:
Generate hotplug options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--hotplug
{{
proxmox_vm_hotplug
}}"
when
:
proxmox_vm_hotplug is defined
-
name
:
Generate onboot options
ansible.builtin.set_fact
:
proxmox_create_vm_qm_args
:
"
{{
proxmox_create_vm_qm_args
}}
--onboot
{{
proxmox_vm_onboot
}}"
when
:
proxmox_vm_onboot is defined
-
name
:
Get next free VM ID
ansible.builtin.command
:
pvesh get /cluster/nextid
register
:
vmid
changed_when
:
false
-
name
:
Clone template
# noqa no-changed-when
ansible.builtin.command
:
"
qm
clone
{{
proxmox_vm_templateid
}}
{{
vmid.stdout
}}
--name
{{
proxmox_vm_name
}}
--full
true"
# noqa yaml[line-length]
proxmox_vm_ipconfig_list
:
"
{{
proxmox_vm_ipconfig
is
string
|
ternary([proxmox_vm_ipconfig],
proxmox_vm_ipconfig)
}}"
-
name
:
Resize harddisk
# noqa no-changed-when
ansible.builtin.command
:
"
qm
resize
{{
vmid.stdout
}}
scsi0
{{
proxmox_vm_disksize
}}"
-
name
:
Set VM options
# noqa no-changed-when
ansible.builtin.command
:
"
qm
set
{{
vmid.stdout
}}
{{
proxmox_create_vm_qm_args
}}"
-
name
:
Start VM
# noqa no-changed-when
ansible.builtin.command
:
"
qm
start
{{
vmid.stdout
}}"
-
name
:
Create new VM
when
:
proxmox_vm_name not in proxmox_vm_existing
ansible.builtin.include_tasks
:
create_vm.yml
-
name
:
Enable HA for VM
# noqa no-changed-when
ansible.builtin.command
:
"
ha-manager
add
{{
vmid.stdout
}}"
when
:
proxmox_vm_enable_ha is defined and proxmox_vm_enable_ha
-
name
:
Update existing VM
when
:
proxmox_vm_name in proxmox_vm_existing
ansible.builtin.include_tasks
:
update_vm.yml
This diff is collapsed.
Click to expand it.
tasks/update_vm.yml
0 → 100644
+
20
−
0
View file @
c81337a6
---
-
name
:
Set ipconfig options
ansible.builtin.lineinfile
:
path
:
"
/etc/pve/nodes/{{
proxmox_vm_node
}}/qemu-server/{{
proxmox_vm_id
}}.conf"
regexp
:
'
^ipconfig{{
index
}}'
line
:
"
ipconfig{{
index
}}:
{{
item
}}"
loop
:
"
{{
proxmox_vm_ipconfig_list
}}"
loop_control
:
index_var
:
index
-
name
:
Set net options
ansible.builtin.lineinfile
:
path
:
"
/etc/pve/nodes/{{
proxmox_vm_node
}}/qemu-server/{{
proxmox_vm_id
}}.conf"
regexp
:
'
^net{{
index
}}:
virtio=([A-Z0-9:]{17}),.*$'
line
:
'
net{{
index
}}:
virtio=\1,{{
item
}}'
backrefs
:
true
loop
:
"
{{
proxmox_vm_net_list
}}"
loop_control
:
index_var
:
index
when
:
proxmox_vm_net is defined
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment