Use Vagrantfile to build virtual site
[icn.git] / deploy / metal3-vm / vm-setup / roles / libvirt / tasks / vm_setup_tasks.yml
1 # Create a libvirt volume pool.  This is where we'll be creating
2 # images for the VMs
3 # Note: the virt_pool module is not working properly on rhel-7.2
4 # https://bugs.launchpad.net/tripleo-quickstart/+bug/1597905
5 - name: ensure libvirt volume path exists
6   become: true
7   file:
8     path: "{{ libvirt_volume_path }}"
9     state: directory
10     mode: 0755
11
12 - name: Check volume pool
13   command: >
14     virsh pool-uuid "{{ libvirt_volume_pool }}"
15   register: pool_check
16   ignore_errors: true
17   changed_when: false
18   environment:
19     LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
20
21 - name: create the volume pool xml file
22   template:
23     src: volume_pool.xml.j2
24     dest: "{{ working_dir }}/volume_pool.xml"
25   when: pool_check is failed
26
27 - name: Define volume pool
28   command: "virsh pool-define {{ working_dir }}/volume_pool.xml"
29   when: pool_check is failed
30   environment:
31     LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
32
33 - name: Start volume pool
34   virt_pool:
35     command: start
36     state: active
37     name: "{{ libvirt_volume_pool }}"
38     uri: "{{ libvirt_uri }}"
39
40 # In some cases the pool_check can pass and the pool xml config is absent
41 # In this case it is required to dump the xml and redefine the pool.
42 - name: ensure tripleo-quickstart volume pool is defined
43   shell: >
44     virsh pool-dumpxml {{ libvirt_volume_pool }} |
45     virsh pool-define /dev/stdin
46   changed_when: true
47   environment:
48     LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
49
50 - name: Mark volume pool for autostart
51   virt_pool:
52     name: "{{ libvirt_volume_pool }}"
53     autostart: "yes"
54     uri: "{{ libvirt_uri }}"
55
56 - when: vm_nodes
57   environment:
58     LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
59   block:
60
61     # Create libvirt volumes for the vm hosts.
62     - name: Check if vm volumes exist
63       command: >
64         virsh vol-info --pool '{{ libvirt_volume_pool }}' '{{ item.name }}.qcow2'
65       register: vm_vol_check
66       ignore_errors: true
67       with_items: "{{ vm_nodes }}"
68
69     - name: Create vm vm storage
70       command: >
71         virsh vol-create-as '{{ libvirt_volume_pool }}'
72         '{{ item.item.name }}'.qcow2 '{{ flavors[item.item.flavor].disk }}'G
73         --format qcow2
74       when:
75         - item is failed
76       with_items: "{{ vm_vol_check.results }}"
77
78     # Define (but do not start) the vm nodes.  These will be
79     # booted later by ironic during the provisioning process.
80     - name: Define vm vms
81       virt:
82         name: "{{ item.name }}"
83         command: define
84         xml: "{{ lookup('template', 'baremetalvm.xml.j2') }}"
85         uri: "{{ libvirt_uri }}"
86       with_items: "{{ vm_nodes }}"
87
88     # Create additional blockdevices for each objectstorage flavor node
89     # These are sparse files, not using space if unused
90     - name: Create additional blockdevice for objectstorage nodes
91       command: >
92         dd if=/dev/zero of={{ libvirt_volume_path }}/{{ item[0].name }}_{{ item[1] }}.img bs=1 count=0 seek={{ extradisks_size }}
93       when: flavors[item[0].flavor].extradisks|default(false)
94       with_nested:
95         - "{{ vm_nodes }}"
96         - "{{ extradisks_list }}"
97
98     - name: Check if additional blockdevices are attached
99       command: >
100         virsh domblkinfo {{ item[0].name }} {{ libvirt_volume_path }}/{{ item[0].name }}_{{ item[1] }}.img
101       when: flavors[item[0].flavor].extradisks|default(false)
102       changed_when: false
103       ignore_errors: true
104       register: vm_extradisks_check
105       with_nested:
106         - "{{ vm_nodes }}"
107         - "{{ extradisks_list }}"
108
109     - name: Attach additional blockdevices to vm objectstorage VMs
110       command: >
111         virsh attach-disk --config {{ item.item[0].name }} {{ libvirt_volume_path }}/{{ item.item[0].name }}_{{ item.item[1] }}.img {{ item.item[1] }}
112       when: item is failed
113       with_items: "{{ vm_extradisks_check.results }}"
114
115 # Generate the ironic node inventory files.  Note that this
116 # task *must* occur after the above vm tasks, because if
117 # `vm_nodes` is defined the template depends on the
118 # `node_mac_map` variable.
119 - name: Write ironic node json files
120   template:
121     src: ../templates/ironic_nodes.json.j2
122     dest: "{{ working_dir }}/ironic_nodes.json"