---
- name: Install and configure autofs on Ubuntu/Debian
  block:
    - name: Install the autofs packages on Ubuntu/Debian
      apt: pkg={{ autofs_packages_deb }} state=present cache_valid_time=1800

  when: ansible_distribution_file_variety == "Debian"
  tags: [ 'nfs', 'autofs' ]

- name: Install and configure autofs on EL
  block:
    - name: Install the autofs packages on EL
      yum: pkg={{ autofs_packages_el }} state=present

  when: ansible_distribution_file_variety == "RedHat"
  tags: [ 'nfs', 'autofs' ]

- name: Configure autofs and its maps
  block:
    - name: Create the mount points
      file: dest={{ item.mountpoint_prefix }} state=directory owner=root group=root mode=0755
      with_items: '{{ autofs_maps }}'

    - name: setup idmap.conf
      ini_file:
        path: /etc/idmapd.conf
        section: '{{ item.section }}'
        option: '{{ item.option }}'
        value: '{{ item.value }}'
        state: '{{ item.state }}'
        owner: 'root'
        group: 'root'
        mode: '0644'
        create: no
      loop: '{{ idmap_conf_options }}'

    - name: setup autofs.conf
      ini_file:
        path: /etc/autofs.conf
        section: '{{ item.section }}'
        option: '{{ item.option }}'
        value: '{{ item.value }}'
        state: '{{ item.state }}'
        owner: 'root'
        group: 'root'
        mode: '0644'
        create: no
      loop: '{{ autofs_conf_options }}'

    - name: Install the autofs master configuration
      template: src=auto.master.j2 dest=/etc/auto.master owner=root group=root mode=0644
      register: reg_autofs_master_conf

    - name: Install the autofs map files
      template: src=auto.data.j2 dest=/etc/auto.{{ item.map_name }} owner=root group=root mode=0644
      with_items: '{{ autofs_maps }}'
      register: autofs_conf

    - name: Ensure that autofs is enabled and running
      service: name=autofs state=started enabled=yes

    - name: Restart autofs if the configuration changed
      service: name=autofs state=restarted
      when: reg_autofs_master_conf is changed

  tags: [ 'nfs', 'autofs', 'autofs_conf' ]