---
- name: Build an Alpine chroot
  hosts: all
  vars:
    - chroot_dir: '{{ ansible_env.HOME }}/Alpine-chroot'
    - repository: https://dl-cdn.alpinelinux.org/alpine
  tasks:
    - block:
        - name: Ensure the chroot directory exists
          file:
            path: '{{ chroot_dir }}'
            state: directory
            mode: 0755

        - name: Create the chroot
          command:
            cmd: 'apk -X {{ repository }}/edge/main -U --allow-untrusted -p {{ chroot_dir }} --initdb add alpine-base'
            creates: '{{ chroot_dir }}/bin/busybox'

        - name: Update the repository urls
          template:
            src: ./templates/repositories.j2
            dest: '{{ chroot_dir }}/etc/apk/repositories'
            owner: root
            group: root
            mode: 0644
      become: yes

    - name: Install a stupid script for chrooting
      template:
        src: ./templates/alpine-chroot.j2
        dest: ~/.local/bin/alpine-chroot
        mode: 0755
