---
- name: "Create jail directory"
  file:
    path: "{{ item }}"
    state: directory
    mode: '0755'
    owner: root
    group: root
  with_items:
    - "{{ mbstat.dirs.chroot }}"
    - "{{ mbstat.dirs.chroot }}/etc"
    - "{{ mbstat.dirs.chroot }}/tmp"
    - "{{ mbstat.dirs.chroot }}/dev"
    - "{{ mbstat.dirs.chroot }}/logs"
    - "{{ mbstat.dirs.chroot }}/usr/share/zoneinfo"
    - "{{ mbstat.dirs.chroot }}/usr/lib64/gconv"
    - "{{ mbstat.dirs.chroot }}{{ OS.fpm.dirs.logs }}"
    - "{{ mbstat.dirs.chroot }}{{ mbstat.dirs.www }}"
    - "{{ mbstat.dirs.chroot }}{{ billing.dirs.logs }}"
    - "{{ mbstat.dirs.chroot }}{{ OS.database.percona.dirs.data }}"
    - "{{ mbstat.dirs.chroot }}{{ OS.fpm.dirs.save_path }}"
    - "{{ mbstat.dirs.chroot }}{{ billing.dirs.config }}"

- name: "copy zoneinfo files"
  command: "cp -fR --parents /usr/share/zoneinfo {{ mbstat.dirs.chroot }}"

- name: "copy gconv files"
  command: "cp -fR --parents /usr/lib64/gconv {{ mbstat.dirs.chroot }}"
  when: ansible_os_family | lower == "redhat"

- name: "copy gconv files"
  command: "cp -fR --parents /usr/lib/x86_64-linux-gnu/gconv {{ mbstat.dirs.chroot }}"
  when: ansible_os_family | lower == "debian"

- name: "mknod devices in chroot"
  command: "mknod -m 0666 {{ item.localdev }} c 1 {{ item.localvalue }} creates={{ item.localdev }}"
  args:
    chdir: "{{ mbstat.dirs.chroot }}"
  with_items:
    - { localdev: '/dev/null',    localvalue: '3'}
    - { localdev: '/dev/random',  localvalue: '8'}
    - { localdev: '/dev/urandom', localvalue: '9'}
    - { localdev: '/dev/zero',    localvalue: '5'}
  tags: mknod_devices

- name: "create hard links"
  file:
    src: "{{ item.src }}"
    dest: "{{ mbstat.dirs.chroot }}{{ item.dest }}"
    state: hard
    force: yes
    mode: "0755"
    owner: root
    group: root
  loop:
    - { src: "/usr/share/zoneinfo/{{ timezone.stdout }}", dest: "/etc/localtime" }
    - { src: "/etc/resolv.conf", dest: "/etc/resolv.conf" }
    - { src: "/etc/hosts", dest: "/etc/hosts" }
