---
- block:
  - name: Create mount points
    file:
      path: "{{ item }}"
      state: directory
    with_items:
      - "{{ lower_loop_mount_location }}"
      - "{{ loop_mount_location }}"
      - "{{ chroot_mount_point }}"

  - name: Mount loop devices
    command: "mount {{ item.src }} {{ item.dest }} -o loop"
    with_items:
      - { src: "{{ lower_loop_file_location }}", dest: "{{ lower_loop_mount_location }}" }
      - { src: "{{ loop_file_location }}", dest: "{{ loop_mount_location }}" }

  - name: Create overlay mount points
    file:
      path: "{{ item }}"
      state: directory
    with_items:
      - "{{ loop_mount_location }}/work"
      - "{{ loop_mount_location }}/overlay"

  - name: Mount overlayfs
    command: "mount -t overlay overlay -o lowerdir={{lower_loop_mount_location}}/,upperdir={{ loop_mount_location }}/overlay/,workdir={{ loop_mount_location }}/work/ {{ chroot_mount_point }}"

  - name: Mount sys, dev, proc into chroot
    command: "mount --bind {{item}} {{ chroot_mount_point }}{{ item }}"
    with_items:
      - "/dev"
      - "/proc"
      - "/sys"
      - "/run"
      - "/tmp"

  become: yes
