# Copyright 2021 The Kubernetes Authors.

# 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: Install cloud-init packages
  apt:
    name: "{{ packages }}"
    state: present
    force_apt_get: yes
  vars:
    packages:
    - cloud-init
    - cloud-guest-utils
    - cloud-initramfs-copymods
    - cloud-initramfs-dyn-netconf

- name: Disable Hyper-V KVP protocol daemon on Ubuntu
  systemd:
    name: hv-kvp-daemon
    state: stopped
    enabled: false

- name: Copy networkd-dispatcher scripts to add DHCP provided NTP servers
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: a+x
  vars:
    server_dir: "/var/lib/dhcp"
    chrony_helper_dir: "/usr/lib/chrony"
  loop:
    - { src: files/etc/networkd-dispatcher/routable.d/20-chrony.j2, dest: /etc/networkd-dispatcher/routable.d/20-chrony }
    - { src: files/etc/networkd-dispatcher/off.d/20-chrony.j2, dest: /etc/networkd-dispatcher/off.d/20-chrony }
    - { src: files/etc/networkd-dispatcher/no-carrier.d/20-chrony.j2, dest: /etc/networkd-dispatcher/no-carrier.d/20-chrony }

- name: Create cloud-init datasource config file
  copy:
    dest: /etc/cloud/ds-identify.cfg
    force: true
    content: |
      datasource: VMware
  when: ansible_distribution_version is version('22.04', '>=')