---
# Copyright kubeinit contributors
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: Deploy a Debian based guest
block:
- name: Create the interfaces template for networking details
ansible.builtin.template:
src: "debian-network-config-enp1s0.j2"
dest: "{{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/enp1s0"
mode: "0644"
- name: Create the hosts template
ansible.builtin.template:
src: "101-cloudinit-hosts.tmpl.j2"
dest: "{{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/hosts.debian.tmpl"
mode: "0644"
- name: Create the main config file for cloud-init
ansible.builtin.template:
src: "102-cloudinit-cloud.cfg.j2"
dest: "{{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/cloud.cfg"
mode: "0644"
- name: Create the datasource cloud-init file
ansible.builtin.template:
src: "90-cloudinit-dpkg.cfg.j2"
dest: "{{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/90_dpkg.cfg"
mode: "0644"
- name: Remove old disk images
ansible.builtin.file:
path: "{{ kubeinit_libvirt_target_image_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}.qcow2"
state: absent
- name: Grow the partitions
ansible.builtin.shell: |
qemu-img convert -f qcow2 '{{ kubeinit_libvirt_target_image_dir }}/{{ kubeinit_libvirt_cloud_images[kubeinit_cluster_distro].image }}' -O qcow2 '{{ kubeinit_libvirt_target_image_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}.qcow2'
qemu-img resize {{ kubeinit_libvirt_target_image_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}.qcow2 +{{ hostvars[kubeinit_deployment_node_name].disk }}
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: "Inject virt-customize assets"
ansible.builtin.shell: |
virt-customize -a {{ kubeinit_libvirt_target_image_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}.qcow2 \
--run-command "sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config" \
--run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config" \
--run-command "dpkg-reconfigure openssh-server" \
{% for authorized_key in kubeinit_cluster_hostvars.authorized_keys %}
--ssh-inject root:string:"{{ authorized_key }}" \
{% endfor %}
--hostname {{ kubeinit_deployment_node_name }}.{{ kubeinit_cluster_fqdn }} \
{% if kubeinit_libvirt_cloud_user_create %}
--run-command 'useradd -s /bin/bash -u 1000 -p "$(openssl passwd -1 {{ kubeinit_libvirt_cloud_user_password }})" {{ kubeinit_libvirt_cloud_user }}' \
--run-command 'echo "{{ kubeinit_libvirt_cloud_user }} ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/{{ kubeinit_libvirt_cloud_user }}' \
--run-command 'mkdir -p {{ kubeinit_libvirt_source_keystore_dir }}' \
--ssh-inject {{ kubeinit_libvirt_cloud_user }}:file:{{ kubeinit_libvirt_source_keystore_dir }}/{{ kubeinit_libvirt_source_pubkey_file }} \
{% endif %}
--copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/enp1s0:/etc/network/interfaces.d \
--copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/hosts.debian.tmpl:/etc/cloud/templates \
--copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/90_dpkg.cfg:/etc/cloud/cloud.cfg.d \
--copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/cloud.cfg:/etc/cloud
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: "Create VM definition for {{ kubeinit_deployment_node_name }}"
ansible.builtin.shell: |
virt-install \
--connect qemu:///system \
--name={{ hostvars[kubeinit_deployment_node_name].guest_name }} \
--memory memory={{ hostvars[kubeinit_deployment_node_name].ram|int // 1024 }} \
--vcpus={{ hostvars[kubeinit_deployment_node_name].vcpus }},maxvcpus={{ hostvars[kubeinit_deployment_node_name].maxvcpus }} \
--os-variant=ubuntu18.04 \
--autostart \
--network network={{ kubeinit_cluster_hostvars.network_name }},mac={{ hostvars[kubeinit_deployment_node_name].mac }},virtualport.parameters.interfaceid={{ hostvars[kubeinit_deployment_node_name].interfaceid }},target.dev=veth0-{{ hostvars[kubeinit_deployment_node_name].ansible_host | ansible.utils.ip4_hex }},model=virtio \
--graphics none \
--noautoconsole \
--import \
--disk {{ kubeinit_libvirt_target_image_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}.qcow2,format=qcow2,bus=virtio
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: "Wait until is running: {{ kubeinit_deployment_node_name }}"
community.libvirt.virt:
command: list_vms
state: running
register: _result
retries: 30
delay: 10
until: hostvars[kubeinit_deployment_node_name].guest_name in _result.list_vms
delegate_to: "{{ kubeinit_deployment_delegate }}"
- name: Check cluster nodes are up and running
ansible.builtin.include_tasks: 70_check_nodes_up.yml
- name: Configure common requirements in Debian guests
block:
- name: Make sure base file exists
ansible.builtin.copy:
content: ""
dest: /etc/resolvconf/resolv.conf.d/tail
force: no
mode: "0644"
- name: Get resolv lines
ansible.builtin.slurp:
src: /etc/resolvconf/resolv.conf.d/tail
register: _result_resolv_conf
- name: Add the local DNS server as a local resolver when not empty
ansible.builtin.lineinfile:
path: /etc/resolvconf/resolv.conf.d/tail
line: "nameserver {{ kubeinit_bind_service_address }}"
insertbefore: nameserver.*
when: (_result_resolv_conf.content | b64decode).splitlines() | length > 0
- name: Add the local DNS server as a local resolver when empty
ansible.builtin.lineinfile:
path: /etc/resolvconf/resolv.conf.d/tail
line: "nameserver {{ kubeinit_bind_service_address }}"
when: (_result_resolv_conf.content | b64decode).splitlines() | length == 0
- name: Add the local DNS server as a local resolver
ansible.builtin.lineinfile:
path: /etc/resolvconf/resolv.conf.d/tail
line: "nameserver {{ kubeinit_dns_public }}"
insertafter: nameserver.*
- name: Restart the resolvconf service
ansible.builtin.service:
name: "resolvconf"
state: restarted
enabled: yes
- name: Restart the systemd-resolved service
ansible.builtin.service:
name: "systemd-resolved"
state: restarted
enabled: yes
- name: Regenerate the resolv.conf
ansible.builtin.shell: |
set -o pipefail
resolvconf -u
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: Force apt-get update
ansible.builtin.shell: |
apt-get update
apt-get install -y gnupg
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: Update packages
ansible.builtin.package:
name: "*"
state: latest
- name: Disable SWAP
ansible.builtin.shell: |
swapoff -a
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: Resize root partition
ansible.builtin.shell: |
echo 'Reclaim space for root partition'
growpart /dev/vda 1
resize2fs /dev/vda1
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: Enable cloud init
ansible.builtin.shell: |
systemctl enable cloud-init
systemctl start cloud-init
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
- name: Perform any distro-specific post-deployment guest configuration
ansible.builtin.include_role:
name: "kubeinit.kubeinit.{{ kubeinit_cluster_distro_role }}"
tasks_from: post_configure_guest.yml
public: true
- name: Update packages
ansible.builtin.package:
name: "*"
state: latest
register: _result_update_packages
- name: Reboot immediately after the package update
ansible.builtin.shell: "sleep 5 && reboot"
args:
executable: /bin/bash
async: 1
poll: 0
when: _result_update_packages is changed and kubeinit_libvirt_reboot_guests_after_package_update
changed_when: false
delegate_to: "{{ kubeinit_deployment_node_name }}"
- name: Check cluster nodes are up and running
ansible.builtin.include_tasks: 70_check_nodes_up.yml
- name: Gather guest network facts
block:
- name: Gather network and host facts for guest
ansible.builtin.include_role:
name: kubeinit.kubeinit.kubeinit_libvirt
tasks_from: gather_host_facts.yml
public: yes
vars:
_param_gather_host: "{{ kubeinit_deployment_node_name }}"
tags: omit_from_grapher