Playbook #1

/root/kubeinit/ci/builds/bsU8uCNn/0/kubeinit/kubeinit/kubeinit-aux/kubeinit/playbook.yml

Report Status CLI Date Duration Controller User Versions Hosts Plays Tasks Results Files Records
05 May 2024 01:59:58 +0000 00:12:42.96 nyctea root Ansible 2.15.2 ara 1.6.1 (client), 1.6.1 (server) Python 3.11.4 7 10 778 845 48 1

File: /root/.ansible/collections/ansible_collections/kubeinit/kubeinit/roles/kubeinit_libvirt/tasks/deploy_ubuntu_guest.yml

---
# 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 an Ubuntu based guest
  block:
    - name: Create the interfaces template for networking details
      ansible.builtin.template:
        src: "00-installer-ubuntu-netconfig.yaml.j2"
        dest: "{{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/00-installer-ubuntu-netconfig.yaml"
        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 }}/00-installer-ubuntu-netconfig.yaml:/etc/netplan \
          --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 Ubuntu guests
  block:

    - name: Add kubernetes repo for latest kubectl (Ubuntu)
      ansible.builtin.shell: |
        set -eo pipefail
        apt-get install -y apt-transport-https ca-certificates curl
        curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
        echo "deb [trusted=yes signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list
        apt-get update --allow-insecure-repositories
      args:
        executable: /bin/bash
      register: _result
      changed_when: "_result.rc == 0"

    - name: Install resolvconf
      ansible.builtin.package:
        name: resolvconf
        state: present

    - 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
      args:
        executable: /bin/bash
      register: _result
      changed_when: "_result.rc == 0"

    - 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