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