---
- name: "Check if container {{ name }} exists"
  ansible.builtin.command:
    cmd: "lxc-ls {{ name }}"
  changed_when: False
  check_mode: no
  register: container_exists

- name: "Create container {{ name }}"
  community.general.lxc_container:
    name: "{{ name }}"
    container_log: true
    template: debian
    state: stopped
    template_options: "--arch amd64 --release {{ release }}"
  when: container_exists.stdout_lines | length == 0

- name: "Disable network configuration inside container {{ name }}"
  ansible.builtin.replace:
    name: "/var/lib/lxc/{{ name }}/rootfs/etc/default/networking"
    regexp: "^#CONFIGURE_INTERFACES=yes"
    replace: CONFIGURE_INTERFACES=no
  when: lxc_network_type == "none"

- name: "Disable interface shut down on halt inside container {{ name }} (Jessie container)"
  ansible.builtin.lineinfile:
    name: "/var/lib/lxc/{{ name }}/rootfs/etc/default/halt"
    line: "NETDOWN=no"
  when: lxc_network_type == "none" and release == "jessie"

- name: "Make the container {{ name }} poweroff on SIGPWR sent by lxc-stop (Jessie container)"
  ansible.builtin.file:
    src: /lib/systemd/system/poweroff.target
    dest: "/var/lib/lxc/{{ name }}/rootfs/etc/systemd/system/sigpwr.target"
    state: link
  when: release == 'jessie'

- name: "Configure the DNS resolvers in the container {{ name }}"
  ansible.builtin.copy:
    remote_src: yes
    src: /etc/resolv.conf
    dest: "/var/lib/lxc/{{ name }}/rootfs/etc/"

- name: "Add hostname in /etc/hosts for container {{ name }}"
  ansible.builtin.lineinfile:
    name: "/var/lib/lxc/{{ name }}/rootfs/etc/hosts"
    line: "127.0.0.1 {{ name }}"

- name: "Fix permission on /dev for container {{ name }}"
  ansible.builtin.lineinfile:
    name: "/var/lib/lxc/{{ name }}/rootfs/etc/rc.local"
    line: "chmod 755 /dev"
    insertbefore: "^exit 0$"
  when: release == 'jessie'

- name: "Ensure that {{ name }} container is running"
  community.general.lxc_container:
    name: "{{ name }}"
    state: started

- name: "Ensure /etc/profile.d exists in container {{ name }}"
  ansible.builtin.file:
    path: "/var/lib/lxc/{{ name }}/rootfs/etc/profile.d"
    mode: '0755'
    state: directory

- name: "Copy host /etc/profile.d/evolinux into container {{ name }}"
  ansible.builtin.copy:
    src: "/etc/profile.d/evolinux.sh"
    remote_src: true
    dest: "/var/lib/lxc/{{ name }}/rootfs/etc/profile.d/evolinux.sh"
    mode: '0644'

- name: "Purge OpenSSH in container {{ name }}"
  community.general.lxc_container:
    name: "{{ name }}"
    container_command: "DEBIAN_FRONTEND=noninteractive apt-get purge -y openssh-server"

- name: "Init /etc git repository in container {{ name }}"
  ansible.builtin.include_role:
    name: 'etc-git'
    tasks_from: 'repository.yml'
    apply:
      vars:
        gitignore_items:
          - "aliases.db"
          - "*.swp"
          - "postfix/sa-blacklist.access"
          - "postfix/*.db"
          - "postfix/spamd.cidr"
          - "evobackup/.keep-*"
          - "letsencrypt/.certbot.lock"
      become: yes
  loop: ["/var/lib/lxc/{{ name }}/rootfs/etc/"]
  loop_control:
    loop_var: 'repository_path'
