Playbook #1

/root/kubeinit/ci/builds/6mbKNrxD/0/kubeinit/kubeinit/kubeinit-aux/kubeinit/playbook.yml

Report Status CLI Date Duration Controller User Versions Hosts Plays Tasks Results Files Records
26 Oct 2023 19:36:23 +0000 00:45:02.10 nyctea root Ansible 2.15.2 ara 1.6.1 (client), 1.6.1 (server) Python 3.11.4 8 9 1048 1048 53 1

File: /root/.ansible/collections/ansible_collections/kubeinit/kubeinit/roles/kubeinit_libvirt/tasks/deploy_centos_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 a CentOS based guest
  block:

    - name: Create the network interface template for networking details
      ansible.builtin.template:
        src: "ifcfg-eth0.j2"
        dest: "{{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/ifcfg-eth0"
        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.redhat.tmpl"
        mode: "0644"

    - name: Create the network cloudinit templates for networking details
      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: 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"

    # This will inject the VM configuration in the case of a CentOS machine
    - name: "Inject virt-customize assets in {{ kubeinit_deployment_node_name }}"
      ansible.builtin.shell: |
        virt-customize -a {{ kubeinit_libvirt_target_image_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}.qcow2 \
          --install python3 \
          --run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config" \
          --run-command "ssh-keygen -A" \
          --run-command "sed -i s/^SELINUX=.*$/SELINUX=permissive/g /etc/selinux/config" \
          {% 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 'id -u {{ kubeinit_libvirt_cloud_user }} &>/dev/null || /usr/sbin/useradd -u 1000 {{ kubeinit_libvirt_cloud_user }}' \
            --ssh-inject {{ kubeinit_libvirt_cloud_user }}:file:{{ kubeinit_libvirt_source_keystore_dir }}/{{ kubeinit_libvirt_source_pubkey_file }} \
            --root-password password:{{ kubeinit_libvirt_cloud_user_password }} \
          {% endif %}
          --copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/ifcfg-eth0:/etc/sysconfig/network-scripts \
          --copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/hosts.redhat.tmpl:/etc/cloud/templates \
          --copy-in {{ kubeinit_libvirt_hypervisor_tmp_dir }}/{{ hostvars[kubeinit_deployment_node_name].guest_name }}/cloud.cfg:/etc/cloud \
          --selinux-relabel
      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 rhel8.0 \
            --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 it 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 CentOS guests
  block:

    - name: Make NetworkManager stop updating resolv.conf
      ansible.builtin.lineinfile:
        dest: "/etc/NetworkManager/NetworkManager.conf"
        insertafter: '^\[main\]'
        state: present
        line: 'dns=none'

    - name: Restart NetworkManager
      ansible.builtin.service:
        name: NetworkManager
        state: restarted

    - name: Get resolv lines
      ansible.builtin.slurp:
        src: /etc/resolv.conf
      register: _result_resolv_conf

    - name: Add the local DNS server as a local resolver when not empty
      ansible.builtin.lineinfile:
        path: /etc/resolv.conf
        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/resolv.conf
        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/resolv.conf
        line: "nameserver {{ kubeinit_dns_public }}"
        insertafter: nameserver.*

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

    - name: Enable kernel modules and IP forward
      ansible.builtin.shell: |
        modprobe br_netfilter
        echo br_netfilter > /etc/modules-load.d/br_netfilter.conf
        modprobe overlay
        echo overlay > /etc/modules-load.d/overlay.conf
        echo '1' > /proc/sys/net/ipv4/ip_forward
        mkdir -p /proc/sys/net/bridge/
        echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
        sysctl -p
      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