- name: Create homes
  block:
    - name: "Stat homes"
      stat:
        path: "/export/home/{{item}}"
      register: home
      loop: "{{mount.home.pontos[inventory_hostname | regex_replace('pontos([0-9]*).fi.muni.cz', '\\1')]}}"

    - name: "Create home dir"
      file:
        state: directory
        path: "/export/home/{{item}}"
        owner: "{{item}}"
        group: "paradise"
      loop: "{{home.results | map(attribute='item')}}"
      register: home_created

    - name: "Create public_html dir"
      file:
        state: directory
        path: "/export/home/{{item}}/public_html"
        owner: "{{item}}"
        group: "paradise"
      loop: "{{home.results | map(attribute='item')}}"

    - name: "Copy skeleton"
      copy:
        src: "/etc/skel/{{item.1}}"
        dest: "/export/home/{{item.0.item}}"
        remote_src: true
        owner: "{{item.0.item}}"
        group: "paradise"
      loop: "{{home_created.results | selectattr('changed') | product(['.bash_logout', '.bashrc', '.profile']) | list}}"
  when: inventory_hostname == "pontos01.fi.muni.cz" or inventory_hostname == "pontos02.fi.muni.cz" or inventory_hostname == "pontos03.fi.muni.cz" or inventory_hostname == "pontos04.fi.muni.cz"

- name: Setup home exports
  template:
    src: home.exports.j2
    dest: /etc/exports.d/home.exports
  notify:
    - export nfs
  when: '"pontos_home" in group_names'

- name: Install autofs (debian)
  apt:
    pkg:
      - autofs
  when: ansible_distribution == 'Debian'

- name: Install autofs (archlinux)
  pacman:
    name:
      - autofs
  when: ansible_distribution == 'Archlinux'

- name: Enable autofs in nsswitch.conf
  lineinfile:
    path: /etc/nsswitch.conf
    insertafter: EOF
    regexp: 'automount: files'
    line: 'automount: files'

- name: Enable autofs service
  systemd:
    enabled: true
    name: autofs
  notify:
    - restart autofs

- name: Master autofs file for home
  copy:
    content: |
      # managed by ansible
      /home {{autofs_prefix}}/auto.home -t 300,strictexpire
      /pontos {{autofs_prefix}}/auto.pontos -t 300,strictexpire
      /antea {{autofs_prefix}}/auto.antea -t 300,strictexpire
    dest: '{{autofs_prefix}}/auto.master.d/home.autofs'
  notify:
    - restart autofs

- name: Home autofs config
  template:
    src: 'auto.{{item}}.j2'
    dest: '{{autofs_prefix}}/auto.{{item}}'
    lstrip_blocks: true
  loop: ['home', 'pontos', 'antea']
  notify:
    - restart autofs

- name: Web autofs mounts
  block:
    - name: Userweb directory
      file:
        state: directory
        path: /srv/userweb

    - name: Master autofs for web
      copy:
        content: |
          # managed by ansible
          /srv/userweb/ {{autofs_prefix}}/auto.userweb -t 300,strictexpire
        dest: '{{autofs_prefix}}/auto.master.d/userweb.autofs'
      notify:
        - restart autofs

    - name:
      template:
        src: 'auto.userweb.j2'
        dest: '{{autofs_prefix}}/auto.userweb'
        lstrip_blocks: true
      notify:
        - restart autofs
  when: inventory_hostname in mount.export_web_to
