---
- name: configure network (static)
  block:
    - name: install 10-static-ethernet.network
      template: src=10-static-ethernet.network.j2 dest={{ chroot_path }}/etc/systemd/network/10-static-ethernet.network owner=root group=root mode=0644
      notify:
        - restart networkd

    - name: create drop-in directory for 10-static-ethernet.network
      file: path=/etc/systemd/network/10-static-ethernet.network.d state=directory owner=root group=root mode=0755

    - name: configure static dns (static)
      copy: src=dns.conf dest={{ chroot_path }}/etc/systemd/network/10-static-ethernet.network.d/dns.conf owner=root group=root mode=0644
      notify:
        - restart networkd
      when: static_dns|default(true)
  when: not dhcp|default(false)

- name: configure network (dhcp)
  block:
    - name: install 10-dhcp-ethernet.network
      template: src=10-dhcp-ethernet.network.j2 dest={{ chroot_path }}/etc/systemd/network/10-dhcp-ethernet.network owner=root group=root mode=0644
      notify:
        - restart networkd

    - name: create drop-in directory for 10-dhcp-ethernet.network
      file: path=/etc/systemd/network/10-dhcp-ethernet.network.d state=directory owner=root group=root mode=0755

    - name: configure static dns (dhcp)
      copy: src=dns.conf dest={{ chroot_path }}/etc/systemd/network/10-dhcp-ethernet.network.d/dns.conf owner=root group=root mode=0644
      notify:
        - restart networkd
      when: static_dns|default(false)
  when: dhcp|default(false)

- name: create symlink to resolv.conf
  file: src=/run/systemd/resolve/stub-resolv.conf dest={{ chroot_path }}/etc/resolv.conf state=link force=yes follow=no owner=root group=root

- name: install hcloud-init
  copy: src=hcloud-init dest={{ chroot_path }}/usr/local/bin/hcloud-init owner=root group=root mode=0755
  when: "'hcloud' in group_names or inventory_hostname == 'packer-base-image'"

- name: install hcloud-init.service
  copy: src=hcloud-init.service dest={{ chroot_path }}/etc/systemd/system/hcloud-init.service owner=root group=root mode=0644
  when: "'hcloud' in group_names or inventory_hostname == 'packer-base-image'"

- name: enable hcloud-init inside chroot
  command: chroot {{ chroot_path }} systemctl enable hcloud-init
  register: chroot_systemd_services
  changed_when: "chroot_systemd_services.rc == 0"
  when:
    - chroot_path | length != 0
    - "'hcloud' in group_names or inventory_hostname == 'packer-base-image'"

- name: start and enable hcloud-init
  service: name=hcloud-init daemon_reload=yes state=started enabled=yes
  when:
    - chroot_path | length == 0
    - "'hcloud' in group_names or inventory_hostname == 'packer-base-image'"

- name: start and enable networkd
  service: name=systemd-networkd state=started enabled=yes
  when: chroot_path | length == 0

- name: start and enable resolved
  service: name=systemd-resolved state=started enabled=yes
  when: chroot_path | length == 0
