-
Notifications
You must be signed in to change notification settings - Fork 26
/
00_multipass-prep-inventory-and-hosts.yml
154 lines (131 loc) · 5.91 KB
/
00_multipass-prep-inventory-and-hosts.yml
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
151
152
153
154
---
- name: "AT Computing - HashiCorp Demo - Prep machines, inventory and hosts file."
hosts: localhost
connection: local
vars_files:
- vars/general/main.yml
- vars/multipass/main.yml
- vars/hashicorp/vault.yml
- "vars/multipass/{{ ansible_os_family }}.yml"
tasks:
- name: Check if multipass is present.
ansible.builtin.stat:
path: "{{ multipass_binary }}"
changed_when: false
register: multipass_presence
failed_when: not multipass_presence.stat.exists
- name: Set the server names as a fact.
ansible.builtin.set_fact:
server_names: "{{ multipass_instances.servers | join('\\|') }}"
- name: Set the client names as a fact.
ansible.builtin.set_fact:
client_names: "{{ multipass_instances.clients | join('\\|') }}"
- name: Authentication.
block:
- name: Authenticate against Multipass.
ansible.builtin.command: "multipass authenticate {{ multipass_passphrase }}"
no_log: true
changed_when: false
rescue:
- name: Guide message for Multipass authentication.
ansible.builtin.fail:
msg: "Please make sure you have set the local passphrase to the correct one with: multipass set local.passphrase"
- name: Check if there are already multipass instances on this machine.
ansible.builtin.shell: "multipass list | grep '{{ client_names }}\\|{{ server_names }}'"
changed_when: false
register: multipass_current
failed_when: multipass_current.rc not in [0, 1]
- name: If there are instances already present, ask permission to delete.
ansible.builtin.pause:
prompt: "Am I allowed to remove all existing instances that match the names of the instances we want to build? (Y/n)"
echo: true
register: prompt_result
failed_when: (prompt_result.user_input is not defined) or (prompt_result.user_input | lower != "y")
when: multipass_current.stdout | length > 0
- name: Delete existing instances.
ansible.builtin.command: "multipass delete {{ item }}"
ignore_errors: true
with_items:
- "{{ multipass_instances.clients }}"
- "{{ multipass_instances.servers }}"
when:
- prompt_result.user_input is defined
- prompt_result.user_input | lower == "y"
- name: Purge the multipass inventory after deletion.
ansible.builtin.command: "multipass purge"
changed_when: false
when:
- prompt_result.user_input is defined
- prompt_result.user_input | lower == "y"
- name: Ensure the cloud-init file is present.
ansible.builtin.template:
src: "templates/multipass/at-cloud-init.yml.j2"
dest: "{{ cloud_init_full_path }}"
mode: "0644"
- name: Spin up the multipass instances.
ansible.builtin.command: "multipass launch --name {{ item }} --cloud-init {{ cloud_init_full_path }} {{ image_name }}"
register: multipass_output
changed_when: multipass_output.rc == 0
with_items:
- "{{ multipass_instances.servers }}"
- "{{ multipass_instances.clients }}"
- name: Ensure we cleanup the used cloud-init file.
ansible.builtin.file:
path: "{{ cloud_init_full_path }}"
state: absent
- name: Retrieve info on the running servers we've just created.
ansible.builtin.shell: "multipass list | grep Running | grep '{{ server_names }}' | awk '{print $1,$3}'"
changed_when: false
register: multipass_output_servers
- name: Create the multipass servers fact.
ansible.builtin.set_fact:
multipass_servers: "{{ multipass_servers | default([]) + [{'name': item.split(' ')[0], 'ip': item.split(' ')[1]}] }}"
with_items: "{{ multipass_output_servers.stdout_lines }}"
- name: Retrieve info on the running clients we've just created.
ansible.builtin.shell: "multipass list | grep Running | grep '{{ client_names }}' | awk '{print $1,$3}'"
changed_when: false
register: multipass_output_clients
- name: Create the multipass clients fact.
ansible.builtin.set_fact:
multipass_clients: "{{ multipass_clients | default([]) + [{'name': item.split(' ')[0], 'ip': item.split(' ')[1]}] }}"
loop: "{{ multipass_output_clients.stdout_lines }}"
- name: Ensure new dynamic inventory is created.
ansible.builtin.template:
src: templates/general/inventory.j2
dest: inventory
mode: "0644"
- name: Check if this is the first time we're running this.
ansible.builtin.debug:
msg: "Re-run this playbook, but now add '--tags hostfile --ask-become-pass'."
when: groups.all | length == 0
- name: End the playbook if this is the first run.
ansible.builtin.meta: end_play
when: groups.all | length == 0
- name: Hosts file
become: true
tags: hostfile
block:
- name: Add a comment block in the hosts file.
ansible.builtin.blockinfile:
backup: true
path: /etc/hosts
block: |
# Added for the AT HashiCorp Demo - discard afterwards
- name: Add the instances to the hosts file.
ansible.builtin.lineinfile:
dest: /etc/hosts
regexp: ".*{{ item }} ({{ demo_fqdn }})?$"
line: "{{ hostvars[item].ansible_host }} {{ item }}"
state: present
when: hostvars[item].ansible_host is defined
with_items: "{{ groups.all }}"
- name: Add the demo FQDN to the first client in the host file.
ansible.builtin.replace:
path: /etc/hosts
regexp: "(.*{{ multipass_instances.clients | first }})$"
replace: '\g<1> {{ demo_fqdn }}'
- name: Add the vault FQDN to the first server in the host file.
ansible.builtin.replace:
path: /etc/hosts
regexp: "(.*{{ multipass_instances.servers | first }})$"
replace: '\g<1> {{ vault_ssl_server_common_name }}'