-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_host_vms.yml
More file actions
150 lines (138 loc) · 4.77 KB
/
manage_host_vms.yml
File metadata and controls
150 lines (138 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
# Unified VM Management Playbook
# This playbook consolidates VM creation and configuration
#
# Usage modes:
# 1. Create single VM from command line:
# ansible-playbook -i inventory manage_host_vms.yml -e "vm_name=myvm"
#
# 2. Create multiple VMs from inventory (YAML format):
# ansible-playbook -i inventory.yml manage_host_vms.yml
#
# 3. Create VMs from host_vars:
# ansible-playbook -i inventory manage_host_vms.yml -l hostname
#
# 4. Skip configuration:
# ansible-playbook -i inventory manage_host_vms.yml --skip-tags configure
#
# 5. Only configure existing VMs:
# ansible-playbook -i inventory manage_host_vms.yml --tags configure
#
- name: Create and configure VMs
hosts: libvirt_hosts
become: false
gather_facts: true
tasks:
# Normalize VM input: convert single VM to list format
- name: Build unified VM list
ansible.builtin.set_fact:
vms_to_create: >-
{{
[{'name': vm_name,
'memory': vm_memory_mb | default(2048),
'vcpus': vm_vcpus | default(2),
'disk_size': vm_disk_size_gb | default(20),
'autostart': vm_autostart | default(false)}]
if vm_name is defined
else (vms | default([]))
}}
tags:
- always
- name: Display execution mode
ansible.builtin.debug:
msg: |
Host: {{ inventory_hostname }}
Mode: {{ 'Single VM (command-line)' if vm_name is defined else 'Multiple VMs (inventory)' }}
VMs to process: {{ vms_to_create | map(attribute='name') | list }}
Total count: {{ vms_to_create | length }}
tags:
- always
# Handle case where no VMs defined
- name: Check if VMs are defined
when: vms_to_create | length == 0
block:
- name: No VMs to create
ansible.builtin.debug:
msg: |
No VMs defined for host {{ inventory_hostname }}.
To create VMs, use one of these methods:
1. Command-line: -e "vm_name=myvm"
2. Inventory: Define 'vms' list in inventory.yml
3. Host vars: Create host_vars/{{ inventory_hostname }}.yml with 'vms' list
tags:
- always
- name: Skip host with no VM definitions
ansible.builtin.meta: end_host
tags:
- always
# Unified VM creation - handles both single and multiple VMs
- name: Create VMs
ansible.builtin.include_role:
name: vm-create
vars:
vm_name: "{{ item.name }}"
vm_memory_mb: "{{ item.memory | default(2048) }}"
vm_vcpus: "{{ item.vcpus | default(2) }}"
vm_disk_size_gb: "{{ item.disk_size | default(20) }}"
vm_autostart: "{{ item.autostart | default(false) }}"
vm_networks: "{{ item.networks | default([]) }}"
loop: "{{ vms_to_create }}"
loop_control:
label: "{{ item.name }}"
register: vm_creation_results
tags:
- create
- name: Add created VMs to dynamic group for configuration
ansible.builtin.add_host:
name: "{{ item.item.name }}"
groups: vms_to_configure
ansible_host: "{{ item.vm_ip_result.stdout | default('') }}"
loop: "{{ vm_creation_results.results | default([]) }}"
loop_control:
label: "{{ item.item.name }}"
when:
- item.vm_ip_result is defined
- item.vm_ip_result.stdout is defined
- item.vm_ip_result.stdout | length > 0
tags:
- create
- name: Display creation summary
ansible.builtin.debug:
msg: |
VM creation completed for {{ inventory_hostname }}
Total VMs created: {{ vms_to_create | length }}
VMs: {{ vms_to_create | map(attribute='name') | list | join(', ') }}
tags:
- create
# Optional: Configure created VMs
# This section can be skipped with --skip-tags configure
- name: Configure created VMs
hosts: vms_to_configure
gather_facts: true
tasks:
- name: Display configuration target
ansible.builtin.debug:
msg: "Configuring VM: {{ inventory_hostname }} at {{ ansible_host }}"
tags:
- configure
- name: Wait for VM to be accessible via SSH
ansible.builtin.wait_for:
host: "{{ ansible_host }}"
port: 22
timeout: 300
state: started
delegate_to: localhost
when: ansible_host is defined and ansible_host | length > 0
tags:
- configure
- name: Configure VM
ansible.builtin.include_role:
name: vm-configure
when: ansible_host is defined and ansible_host | length > 0
tags:
- configure
- name: Display configuration complete
ansible.builtin.debug:
msg: "VM {{ inventory_hostname }} configured successfully"
tags:
- configure