Allow VMs to access internet
[yardstick.git] / ansible / roles / infra_create_vms / tasks / configure_vm.yml
1 # Copyright (c) 2017-2018 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ---
15 - name: Remove directory
16   file:
17     path: "{{ '/tmp/'+node_item.hostname }}"
18     state: absent
19
20 - name: Create directory
21   file:
22     path: "{{ '/tmp/'+node_item.hostname }}"
23     state: directory
24     mode: 0755
25
26 - name: Define user-data file name
27   set_fact:
28     user_data: "{{ '/tmp/'+node_item.hostname+'/user-data' }}"
29
30 - name: Define image-dir
31   set_fact:
32     image_dir: "{{ '/var/lib/libvirt/images/' }}"
33
34 - name: Create a new empty file for user-data
35   file:
36     path: "{{ user_data }}"
37     state: touch
38
39 - name: Add user-data
40   blockinfile:
41     path: "{{ user_data }}"
42     marker: "MARKER"
43     content: |
44       #cloud-config
45       preserve_hostname: False
46       hostname: {{ node_item.hostname }}
47       output:
48         all: ">> /var/log/cloud-init.log"
49       ssh_pwauth: True
50       bootcmd:
51         - echo 127.0.0.1 {{ node_item.hostname }} >> /etc/hosts
52       users:
53           - name: {{ node_item.user }}
54             lock-passwd: False
55             plain_text_passwd: {{ node_item.password }}
56             chpasswd: { expire: False }
57             sudo: ALL=(ALL) NOPASSWD:ALL
58             ssh_pwauth: True
59
60 - name: Remove the marker
61   lineinfile:
62     dest: "{{ user_data }}"
63     state: absent
64     regexp: "MARKER"
65
66 - name: Define network-config file name
67   set_fact:
68     network_config: "{{ '/tmp/'+node_item.hostname+'/network-config' }}"
69
70 - name: Create a new empty file for network-config
71   file:
72     path: "{{ network_config }}"
73     state: touch
74
75 - name: Add network-data
76   blockinfile:
77     path: "{{ network_config }}"
78     marker: "MARKER"
79     content: |
80       version: 2
81       ethernets:
82
83 - name: Define meta-data file name
84   set_fact:
85     meta_data: "{{ '/tmp/'+node_item.hostname+'/meta-data' }}"
86
87 - name: Create a new empty file for meta-data
88   file:
89     path: "{{ meta_data }}"
90     state: touch
91
92 - name: Add meta-data
93   blockinfile:
94     path: "{{ meta_data }}"
95     marker: "MARKER"
96     content: |
97       instance-id: {{ node_item.hostname }}
98       local-hostname: {{ node_item.hostname }}
99
100 - name: Remove the marker
101   lineinfile:
102     dest: "{{ meta_data }}"
103     state: absent
104     regexp: "MARKER"
105
106 - name: Define xml file name
107   set_fact:
108     xml_file: "{{ '/tmp/'+node_item.hostname+'/'+node_item.hostname+'.xml' }}"
109
110 - name: Create a new empty file for xml file
111   file:
112     path: "{{ xml_file }}"
113     state: touch
114
115 - name: Add root "domain" node
116   blockinfile:
117     path: "{{ xml_file }}"
118     marker: ""
119     content: |
120       <domain>
121       </domain>
122
123 - name: Add "type" attribute to "domain" node
124   xml:
125     path: "{{ xml_file }}"
126     xpath: /domain
127     attribute: type
128     value: "kvm"
129     pretty_print: yes
130
131 - name: Add new children nodes to "domain" node
132   xml:
133     path: "{{ xml_file }}"
134     xpath: /domain
135     add_children:
136       - name: "{{ node_item.hostname }}"
137       - memory: "{{ node_item.ram }}"
138       - vcpu: "{{ node_item.vcpus }}"
139       - os
140       - cpu
141       - devices
142     pretty_print: yes
143
144 - name: Add "unit" attribute to "memory" node
145   xml:
146     path: "{{ xml_file }}"
147     xpath: /domain/memory
148     attribute: unit
149     value: "MB"
150     pretty_print: yes
151
152 - name: Add "placement" attribute to "vcpu" node
153   xml:
154     path: "{{ xml_file }}"
155     xpath: /domain/vcpu
156     attribute: placement
157     value: "static"
158     pretty_print: yes
159
160 - name: Add new children nodes to "os" node
161   xml:
162     path: "{{ xml_file }}"
163     xpath: /domain/os
164     add_children:
165       - type: "hvm"
166       - boot
167     pretty_print: yes
168
169 - name: Add "arch" attribute to "type" node
170   xml:
171     path: "{{ xml_file }}"
172     xpath: /domain/os/type
173     attribute: arch
174     value: "x86_64"
175     pretty_print: yes
176
177 - name: Add "dev" attribute to "boot" node
178   xml:
179     path: "{{ xml_file }}"
180     xpath: /domain/os/boot
181     attribute: dev
182     value: "hd"
183     pretty_print: yes
184
185 - name: Add new children nodes to "cpu" node
186   xml:
187     path: "{{ xml_file }}"
188     xpath: /domain/cpu
189     add_children:
190       - cache
191     pretty_print: yes
192
193 - name: Add "mode" attribute to "cpu" node
194   xml:
195     path: "{{ xml_file }}"
196     xpath: /domain/cpu
197     attribute: mode
198     value: "host-passthrough"
199     pretty_print: yes
200
201 - name: Add "mode" attribute to "cache" node
202   xml:
203     path: "{{ xml_file }}"
204     xpath: /domain/cpu/cache
205     attribute: mode
206     value: "passthrough"
207     pretty_print: yes
208
209 - name: Add new children nodes to "devices" node
210   xml:
211     path: "{{ xml_file }}"
212     xpath: /domain/devices
213     add_children:
214       - disk:
215           type: file
216           device: disk
217       - controller:
218           type: virtio-serial
219           index: '0'
220       - serial:
221           type: pty
222       - console:
223           type: pty
224           tty: '/dev/pts/14'
225     pretty_print: yes
226
227 - name: Add new children nodes to "disk" node
228   xml:
229     path: "{{ xml_file }}"
230     xpath: /domain/devices/disk
231     add_children:
232       - driver:
233           name: qemu
234           type: qcow2
235       - source:
236           file: "{{ '/var/lib/libvirt/images/'+node_item.hostname+'.qcow2' }}"
237       - target:
238           dev: vda
239           bus: virtio
240       - alias:
241           name: virtio-disk0
242     pretty_print: yes
243
244 - name: Add new children nodes to "devices" node
245   xml:
246     path: "{{ xml_file }}"
247     xpath: /domain/devices
248     add_children:
249       - disk:
250           type: file
251           device: cdrom
252     pretty_print: yes
253
254 - name: Add new children nodes to "disk" node
255   xml:
256     path: "{{ xml_file }}"
257     xpath: /domain/devices/disk
258     add_children:
259       - source:
260           file: "{{ '/var/lib/libvirt/images/'+node_item.hostname+'-ci-data.img' }}"
261       - target:
262           dev: hdb
263           bus: ide
264       - readonly
265     pretty_print: yes
266
267 - name: Configure controller
268   xml:
269     path: "{{ xml_file }}"
270     xpath: /domain/devices/controller
271     add_children:
272       - alias:
273           name: virtio-serial0
274     pretty_print: yes
275
276 - name: Configure serial
277   xml:
278     path: "{{ xml_file }}"
279     xpath: /domain/devices/serial
280     add_children:
281       - source:
282           path: '/dev/pts/14'
283       - target:
284           port: '0'
285       - alias:
286           name: 'serial0'
287     pretty_print: yes
288
289 - name: Configure console
290   xml:
291     path: "{{ xml_file }}"
292     xpath: /domain/devices/console
293     add_children:
294       - source:
295           path: '/dev/pts/14'
296       - target:
297           port: '0'
298           type: 'serial'
299       - alias:
300           name: 'serial0'
301     pretty_print: yes
302
303 - set_fact:
304     slot_address: 5
305
306 - name: Populate network-config and add interface to xml file
307   include_tasks: create_interfaces.yml
308   extra_vars: "{{ network_config xml_file slot_address mac_address_counter }}"
309   loop_control:
310     loop_var: interface_item
311   with_items: "{{ node_item.interfaces }}"
312
313 - name: Create directory
314   file:
315     path: "{{ '/tmp/'+node_item.hostname }}"
316     state: directory
317     mode: 0755
318
319 - name: Generate iso image
320   shell: >
321     genisoimage -output {{ image_dir+node_item.hostname+'-ci-data.img' }} -volid cidata -joliet
322     -r {{ '/tmp/'+node_item.hostname+'/network-config' }} {{ '/tmp/'+node_item.hostname+'/user-data' }} {{ '/tmp/'+node_item.hostname+'/meta-data' }}
323     &>> {{ '/tmp/'+node_item.hostname+'/hostname.log' }}
324
325 - name: Copy and convert the ubuntu image
326   shell: >
327     qemu-img convert -O qcow2 {{ node_item.image }} {{ image_dir+node_item.hostname+'.qcow2' }}
328
329 - name: Resize image
330   shell: >
331     qemu-img resize {{ image_dir+node_item.hostname+'.qcow2' }} {{ node_item.disk }}MB
332
333 - name: Define the VMs
334   virt:
335     command: define
336     name: "{{ node_item.hostname }}"
337     xml: "{{ lookup('file', '/tmp/'+node_item.hostname+'/'+node_item.hostname+'.xml') }}"
338
339 - name: Start the VMs
340   virt:
341     command: create
342     name: "{{ node_item.hostname }}"