-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-vm-tasks.yaml
90 lines (80 loc) · 2.87 KB
/
create-vm-tasks.yaml
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
---
- name: render cloud-init file
template:
src: "cloud-init-template.j2"
dest: "cloud-init.yaml"
delegate_to: localhost
vars:
public_key: "{{ sshkey.public_key }}"
run_once: true
- name: set cloud-init file path
set_fact:
CLOUD_INIT_PATH: "cloud-init.yaml"
- name: render cloud-init file
template:
src: "../loadbalancer/cloud-init-template.j2"
dest: "../loadbalancer/cloud-init.yaml"
delegate_to: localhost
vars:
public_key: "{{ sshkey.public_key }}"
when: clusterConfig.vmType == 'loadbalancer'
- name: set cloud-init file path
set_fact:
CLOUD_INIT_PATH: "../loadbalancer/cloud-init.yaml"
when: clusterConfig.vmType == 'loadbalancer'
- name: create vm
shell: |
export VMNAME="{{ clusterConfig.vmType }}-node-{{ item }}"
export SPEC="{{ clusterConfig.spec }}"
if [[ -z $(multipass ls --format json | jq -c '.list[] | select(.name == env.VMNAME)') ]]; then
multipass launch lunar $SPEC --cloud-init {{ CLOUD_INIT_PATH }} --name $VMNAME > /dev/null 2>&1
multipass info $VMNAME
else
echo "multipass vm $VMNAME already exists"
multipass info $VMNAME
fi
register: cmd_output
with_sequence: count="{{ clusterConfig.nodeCount }}"
loop_control:
pause: 30
- name: debug output
debug:
msg:
- "{{ item.stdout }}"
with_items: "{{ cmd_output.results }}"
loop_control:
label: "{{ item.item}}"
- name: start vm
shell: multipass start {{ clusterConfig.vmType }}-node-{{ item }}
with_sequence: count="{{ clusterConfig.nodeCount }}"
- name: wait for vm restart
shell: multipass info {{ clusterConfig.vmType }}-node-{{ item }} --format json | jq -r '.info | .[keys[0]].state'
register: vm_status
until: vm_status.stdout == 'Running'
retries: 10
delay: 10
with_sequence: count="{{ clusterConfig.nodeCount }}"
- name: map node name to node IP address
shell: multipass info {{ clusterConfig.vmType }}-node-{{ item }} --format json | jq -r '.info | "\(keys[0]):\(.[keys[0]].ipv4[0])"'
register: vm_info
with_sequence: count="{{ clusterConfig.nodeCount }}"
- name: create inventory file
copy:
content: |
{{ clusterConfig.vmType }}:
hosts:
dest: "../inventories/{{ clusterConfig.vmType }}.yaml"
delegate_to: localhost
- name: update inventory
blockinfile:
dest: "../inventories/{{ clusterConfig.vmType }}.yaml"
marker: "#{{ item.stdout.split(':')[0] }}#"
block: |2
{{ item.stdout.split(':')[0] }}:
ansible_connection: ssh
ansible_host: {{ item.stdout.split(':')[1] }}
ansible_user: ubuntu
ansible_ssh_private_key_file: {{ sshkey.filename }}
with_items: "{{ vm_info.results }}"
loop_control:
label: "{{ item.item}}"