Align license headers with REUSE guidelines
[kuberef.git] / playbooks / roles / jump-vm / tasks / main.yaml
1 ---
2
3 # SPDX-FileCopyrightText: 2021 Ericsson AB and others
4 #
5 # SPDX-License-Identifier: Apache-2.0
6
7 - name: get all running VMs
8   virt:
9     command: list_vms
10     state: running
11   register: running_vms
12
13 - name: shutdown existing jump VM
14   virt:
15     name: "{{ jumphost.name }}"
16     command: destroy
17   when:
18     jumphost.name in running_vms.list_vms
19
20 - name: get all shutdown VMs
21   virt:
22     command: list_vms
23     state: shutdown
24   register: shutdown_vms
25
26 - name: undefine existing jump VM
27   virt:
28     name: "{{ jumphost.name }}"
29     command: undefine
30   when:
31     jumphost.name in shutdown_vms.list_vms
32
33 - name: remove dhcp leases
34   shell: |
35     jq 'del( .[] | select(.domain == "{{ jumphost.name }}" or .hostname == "{{ jumphost.name }}"))' /var/lib/libvirt/dnsmasq/virbr0.{{ item }} > /tmp/{{ item }}.tmp
36     mv /tmp/{{ item }}.tmp /var/lib/libvirt/dnsmasq/virbr0.{{ item }}
37   with_items:
38     - status
39     - macs
40   become: true
41   when: deployment_type == 'k8s'
42
43 - name: clean workspace
44   file:
45     path: "{{ workspace }}"
46     state: absent
47
48 - name: create workspace if it does not exist
49   file:
50     path: "{{ workspace }}"
51     state: directory
52     mode: '0755'
53
54 - name: create directory for base images
55   file:
56     path: "{{ images_path }}"
57     state: directory
58     mode: '0755'
59
60 - name: download Ubuntu image for jump VM
61   get_url:
62     url: https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img
63     dest: "{{ images_path }}/bionic-server-cloudimg-amd64.img"
64     force: false
65     mode: '0666'
66
67 - name: create new VM image from base image
68   command: "qemu-img create -f qcow2 -F qcow2 -o backing_file={{ images_path }}/bionic-server-cloudimg-amd64.img {{ workspace }}/{{ jumphost.name }}.qcow2 10G"
69
70 - name: render config files for jump VM from templates
71   template:
72     src: "{{ kuberef_root }}/playbooks/roles/jump-vm/templates/{{ item }}.j2"
73     dest: "{{ workspace }}/{{ item }}"
74     mode: 0644
75   with_items:
76     - network-config
77     - user-data
78     - meta-data
79
80 - name: create config drive
81   command: "genisoimage -output {{ workspace }}/{{ jumphost.name }}-cidata.iso -volid cidata -joliet -rock \
82             {{ workspace }}/user-data {{ workspace }}/meta-data \
83             {{ workspace + '/network-config' if deployment_type == 'full' else '' }}"
84
85 # currently commented out because of portability issues between Centos and Ubuntu
86 # - name: setting root password for debugging
87 #   become: true
88 #   command: "virt-customize -a {{ workspace }}/{{ jumphost.name }}.qcow2 --root-password password:'root'"
89
90 - name: define jump VM
91   command: "virt-install --connect qemu:///system --name {{ jumphost.name }} \
92               --ram 8192 --vcpus=8 --os-type linux --os-variant ubuntu16.04 \
93               --disk path={{ workspace }}/kuberef-jump.qcow2,format=qcow2 \
94               --disk {{ workspace }}/kuberef-jump-cidata.iso,device=cdrom \
95               --network network=default,model=virtio,mac='{{ jumphost.interfaces[engine.net_config[engine.public_network].interface].mac_address }}' \
96               {{ '--network bridge=pxebr,model=rtl8139,mac=' + jumphost.interfaces[engine.net_config[engine.pxe_network].interface].mac_address if deployment_type == 'full' else '' }} \
97               --import --noautoconsole"
98
99 - name: start jump VM
100   virt:
101     name: "{{ jumphost.name }}"
102     state: running
103
104 - name: wait for ip to be visible
105   shell: virsh domifaddr "{{ jumphost.name }}" --full | grep "{{ jumphost.interfaces[engine.net_config[engine.public_network].interface].mac_address }}" | awk '{print $4}' | tail -n 1
106   register: ipblock
107   retries: 30
108   delay: 1
109   until: ipblock.stdout != ""
110   when: deployment_type == 'k8s'
111
112 - name: store ip
113   set_fact:
114     vm_ip: "{{ ipblock.stdout.split('/') }}"
115   when: deployment_type == 'k8s'
116
117 - name: wait for VM to be reachable
118   wait_for:
119     host: "{{ ( vm_ip is defined and vm_ip.0 or '' ) if 'k8s' in deployment_type else jumphost.interfaces[idf.net_config[engine.pxe_network].interface].address | default('') }}"
120     port: 22