-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_dns_vm.yml
286 lines (273 loc) · 13.2 KB
/
create_dns_vm.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
- name: Create dns virtual machine
vars:
vm_name: "dns.okd.local"
images_path: "/home/{{ ansible_user_id }}/.okd/images"
vm_path: "/home/{{ ansible_user_id }}/.okd/vms/{{ vm_name }}"
boot_init_iso_path: "{{ vm_path }}/boot-init.iso"
centos7_cloud_image_name: CentOS-7-x86_64-GenericCloud-1907.qcow2
centos7_cloud_image_url: "https://cloud.centos.org/centos/7/images/{{ centos7_cloud_image_name }}"
mac: "52:54:00:00:06:10"
ip: "10.0.6.10"
bridge: "virbr2"
bridge_ip: "10.0.6.1"
ssh_public_key: "/home/{{ ansible_user_id }}/.ssh/id_rsa.pub"
dns_name: "dns.okd.local"
block:
- name: List virtual machines
virt:
command: list_vms
uri: "qemu:///session"
register: vms
- name: Create {{ vm_name }} virtual machine
block:
- name: Create {{ vm_name }} virtual machine directories
file:
path: "{{ item }}"
state: directory
owner: "{{ ansible_user_id }}"
group: "{{ ansible_user_id }}"
with_items:
- "{{ images_path }}"
- "{{ vm_path }}"
- name: Check if centos 7 cloud image is present
stat:
path: "{{ images_path }}/{{ centos7_cloud_image_name }}"
register: centos7_cloud_image
- name: Download centos 7 cloud image
become: no
get_url:
url: "{{ centos7_cloud_image_url }}"
dest: "{{ images_path }}"
sha256sum: "520d01c2f2e1ed24cb2f15a4325aa30773930a2961b5484a68cf11b4a415c512"
when: centos7_cloud_image.stat.exists == False
- name: Copy centos7 cloud image from images to {{ vm_name }} virtual machine
copy:
src: "{{ images_path }}/{{ centos7_cloud_image_name }}"
dest: "{{ vm_path }}/{{ centos7_cloud_image_name }}"
owner: "{{ ansible_user_id }}"
group: "{{ ansible_user_id }}"
force: no
- name: Create meta-data file
copy:
dest: "{{ vm_path }}/meta-data"
content: |
instance-id: {{ vm_name }}
local-hostname: {{ vm_name }}
- name: Create user-data file
copy:
dest: "{{ vm_path }}/user-data"
content: |
#cloud-config
# Hostname management
preserve_hostname: False
hostname: {{ vm_name }}
fqdn: {{ vm_name }}.sandbox.okd.local
manage_etc_hosts: true
cloud_config_modules:
- resolv_conf
- package-update-upgrade-install
- runcmd
manage_resolv_conf: true
resolv_conf:
nameservers: ['127.0.0.1','8.8.8.8']
# Users
users:
- default
- name: {{ ansible_user_id }}
groups: ['wheel']
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh-authorized-keys:
- {{ lookup('file', ssh_public_key) }}
# Configure where output will go
output:
all: ">> /var/log/cloud-init.log"
# configure interaction with ssh server
ssh_genkeytypes: ['ed25519', 'rsa']
# Install my public ssh key to the first user-defined user configured
# in cloud.cfg in the template (which is centos for CentOS cloud images)
ssh_authorized_keys:
- {{ lookup('file', ssh_public_key) }}
timezone: US/Eastern
packages:
- dnsmasq
- bind-utils
write_files:
- content: |
local=/sandbox.okd.local/
address=/apps.sandbox.okd.local/10.0.5.57
srv-host=_etcd-server-ssl._tcp.sandbox.okd.local,control-plane-0.sandbox.okd.local,2380,0,10
no-hosts
addn-hosts=/etc/dnsmasq.okd.addnhosts
conf-dir=/etc/dnsmasq.d,.rpmnew,.rpmsave,.rpmorig
cache-size=10000
log-dhcp
log-queries=extra
log-facility=/var/log/dnsmasq.log
path: /run/myconf/dnsmasq.conf
- content: |
10.0.6.10 dns.okd.local
10.0.5.10 container-registry.sandbox.okd.local
10.0.5.57 lb.sandbox.okd.local api.sandbox.okd.local api-int.sandbox.okd.local
10.0.5.58 bootstrap.sandbox.okd.local
10.0.5.59 control-plane-0.sandbox.okd.local etcd-0.sandbox.okd.local
10.0.5.60 control-plane-1.sandbox.okd.local etcd-1.sandbox.okd.local
10.0.5.61 control-plane-2.sandbox.okd.local etcd-2.sandbox.okd.local
10.0.5.62 compute-0.sandbox.okd.local
10.0.5.63 compute-1.sandbox.okd.local
10.0.5.64 compute-2.sandbox.okd.local
path: /run/myconf/dnsmasq.okd.addnhosts
- content: |
# The objectif is to overide the definition of nameserver in /etc/resolv.conf if the vm restart
# You can define the localhost or virtual machine ip as nameserver but NOT the bridge ip however it will constantly loop
# when you will define it as primary nameserver on the host. You will not be able to resolve ip and to connect via ssh.
# The dns virtual machine will be used as primary dns. If it fails to resolve the secondary dns will handle it
supersede domain-name-servers 127.0.0.1,8.8.8.8;
timeout 300;
retry 60;
path: /etc/dhcp/dhclient.conf
# The message is located in /var/log/cloud-init.log
final_message: "The system is finally up, after $UPTIME seconds"
# Remove cloud-init when finished with it
runcmd:
- systemctl stop network && systemctl start network
- systemctl disable cloud-init.service
- /bin/cp /run/myconf/dnsmasq.conf /etc/dnsmasq.conf
- /bin/cp /run/myconf/dnsmasq.okd.addnhosts /etc/dnsmasq.okd.addnhosts
- systemctl enable dnsmasq
- systemctl start dnsmasq
- name: Create boot init iso
shell:
cmd: genisoimage -output {{ boot_init_iso_path }} -volid cidata -joliet -r {{ vm_path }}/user-data {{ vm_path }}/meta-data
- name: Define {{ vm_name }} virtual machine
virt:
command: define
name: "{{ vm_name }}"
xml: '{{ lookup("template", "templates/vm.xml.j2") }}'
uri: "qemu:///session"
vars:
name: "{{ vm_name }}"
memory: 1024
vcpus: 1
disk_file: "{{ vm_path }}/{{ centos7_cloud_image_name }}"
boot_init_iso: "{{ boot_init_iso_path }}"
vm_mac: "{{ mac }}"
vm_bridge: "{{ bridge }}"
- name: Start {{ vm_name }} vm
virt:
command: start
name: "{{ vm_name }}"
autostart: yes
uri: "qemu:///session"
- name: Wait for port 22
wait_for:
host: "{{ ip }}"
port: 22
state: drained
delay: 10
- name: |
Add {{ vm_name }} host to known_hosts - need to retry as ssh is not available yet after the port 22 is open
An empty valide response can be returned :(
block:
- name: Retrieve dns virtual machine public key
shell: ssh-keyscan -t ssh-rsa {{ ip }}
register: task_result
until: task_result.rc == 0 and task_result.stdout != ''
retries: 120
delay: 5
- debug:
msg: "{{ task_result }}"
- name: Write dns virtual machine public key into Host known hosts
lineinfile:
path: /home/{{ ansible_user_id }}/.ssh/known_hosts
insertafter: EOF
line: "{{ dns_name }},{{ task_result.stdout }}"
backup: yes
- name: Remove boot-init.iso from {{ vm_name }} vm
shell:
cmd: virsh change-media {{ vm_name }} --path {{ boot_init_iso_path }} --eject --config
- name: Wait until {{ vm_name }} virtual machine is ready
shell: ssh centos@{{ ip }} 'sudo grep -nri "The system is finally up" /var/log/cloud-init.log' > /dev/null
register: task_result
until: task_result.rc == 0
retries: 120
delay: 5
- name: Check {{ vm_name }} dnsmasq service is active
block:
- name: Get {{ vm_name }} dnsmasq service status
shell: ssh centos@{{ ip }} 'sudo systemctl is-active dnsmasq'
register: is_active
until: is_active.rc == 0
retries: 120
delay: 5
- debug:
msg: "{{ is_active }}"
- name: Ensure {{ vm_name }} dnsmasq service is active
assert:
that:
- "'active' == is_active.stdout"
- name: |
Add dns entry into Host to be able to access virtual machine in '.sandbox.okd'.
Howto do in Centos7 explains here : https://access.redhat.com/solutions/2817911
It seems that it is not working as expected after rebooting. So I lock /etc/resolv.conf to ensure
that no one will change it !
block:
- name: Add "dns=none" in /etc/NetworkManager/NetworkManager.conf if not present
lineinfile:
path: /etc/NetworkManager/NetworkManager.conf
regexp: '^dns\=none'
insertafter: '^\[main\]'
line: dns=none
backup: yes
- name: Add dns virtual machine ip {{ ip }} entry into /etc/resolv.conf
lineinfile:
path: /etc/resolv.conf
regexp: '^nameserver {{ ip }}'
insertbefore: BOF
line: nameserver {{ ip }}
backup: yes
- name: Lock /etc/resolv.conf to avoid to be rewritten at startup
shell: chattr +i /etc/resolv.conf
- name: Reload NetworkManager service
shell: systemctl reload NetworkManager
become: true
become_user: root
when: vm_name not in vms.list_vms
- name: Should {{ vm_name }} virtual machine is well configured
block:
- name: Should {{ item.vm_name }} virtual machine resolved full qualified domain name entries
include: dns_fqdn_vm_resolution.yml
with_items:
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.6.10', fqdn: 'dns.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.10', fqdn: 'container-registry.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.57', fqdn: 'lb.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.57', fqdn: 'api.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.57', fqdn: 'api-int.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.57', fqdn: 'console-openshift-console.apps.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.57', fqdn: 'oauth-openshift.apps.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.58', fqdn: 'bootstrap.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.59', fqdn: 'control-plane-0.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.59', fqdn: 'etcd-0.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.60', fqdn: 'control-plane-1.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.60', fqdn: 'etcd-1.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.61', fqdn: 'control-plane-2.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.61', fqdn: 'etcd-2.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.62', fqdn: 'compute-0.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.63', fqdn: 'compute-1.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.64', fqdn: 'compute-2.sandbox.okd.local'}
- {ip_from: "{{ ip }}", ip_to_resolve: '10.0.5.57', fqdn: 'test.apps.sandbox.okd.local'}
# TODO check SRV cf https://support.microsoft.com/fr-fr/help/816587/how-to-verify-that-srv-dns-records-have-been-created-for-a-domain-cont
- name: Should external dns resolution is working in {{ vm_name }} virtual machine
block:
- name: Execute external DNS resolution
shell: ssh {{ ansible_user_id }}@{{ ip }} 'dig google.fr +short'
register: ips_resolved
- debug:
msg: "{{ ips_resolved }}"
- name: Ensure external host is resolved
assert:
that:
- "ips_resolved is defined"
- "ips_resolved.stdout_lines is defined"
- "ips_resolved.stdout_lines|length > 0"