---

# ---
# Install nis
# ---

- name: (nis-install-client.yml) Set (nis) default domain (/etc/defaultdomain)
  template:
    dest: /etc/defaultdomain  
    src: etc/defaultdomain.j2
    owner: root
    group: root
    mode: 0644
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Create preconfigured /etc/yp.conf on nis clients
  template:
    dest: /etc/yp.conf
    src: etc/yp.conf.j2
    owner: root
    group: root
    mode: 0644
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Install nis common packages
  package:
    name: "{{ item }}"
    state: present
  with_items: "{{ nis_common_packages }}"
  tags:
    - nis-install
    - nis-install-client


# ---
# /etc/default/nis
# ---

- name: (nis-install-client.yml) Check if file '/etc/default/nis.ORIG' exists
  stat:
    path: /etc/default/nis.ORIG
  register: default_nis_exists
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Backup existing file /etc/default/nis
  command: cp -a /etc/default/nis /etc/default/nis.ORIG
  when:
    - default_nis_exists.stat.exists == False
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Adjust file /etc/default/nis - set 'NISSERVER' (client)
  replace:
    path: /etc/default/nis
    regexp: '^NISSERVER=.*'
    replace: 'NISSERVER=false'
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Adjust file /etc/default/nis - set 'NISCLIENT' (client)
  replace:
    path: /etc/default/nis
    regexp: '^NISCLIENT=.*'
    replace: 'NISCLIENT=true'
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Adjust file /etc/default/nis - set 'YPBINDARGS' (client)
  replace:
    path: /etc/default/nis
    regexp: '^YPBINDARGS=.*'
    replace: 'YPBINDARGS='
  tags:
    - nis-install
    - nis-install-client


# ---
# /etc/{passwd,group,shadow}
# ---

- name: (nis-install-client.yml) Add '+::::::' to file /etc/passwd
  lineinfile:
    path: /etc/passwd
    line: '+::::::'
    insertafter: EOF
    state: present
    owner: root
    group: root
    mode: '0644'
  when: "ansible_distribution_major_version|int  < 18"
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Add '+:::' to file /etc/group
  lineinfile:
    path: /etc/group
    line: '+:::'
    insertafter: EOF
    state: present
    owner: root
    group: root
    mode: '0644'
  when: "ansible_distribution_major_version|int  < 18"
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Add '+::::::::' to file /etc/shadow
  lineinfile:
    path: /etc/shadow
    line: '+::::::::'
    insertafter: EOF
    state: present
    owner: root
    group: shadow
    mode: '0640'
  when: "ansible_distribution_major_version|int  < 18"
  tags:
    - nis-install
    - nis-install-client


# ---
# /etc/hosts
# ---

- name: (nis-install-client.yml) Check if file '/etc/hosts.ORIG' exists
  stat:
    path: /etc/hosts.ORIG
  register: etc_hosts_orig_exists
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Backup existing file /etc/hosts
  command: cp -a /etc/hosts /etc/hosts.ORIG
  when:
    - etc_hosts_orig_exists.stat.exists == False
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Add nis-server to file /etc/hosts
  lineinfile:
    path: /etc/hosts
    line: '{{ nis_server_address }} {{ nis_server_name }} {{ nis_server_name.split(".")[1] }}'
    insertafter: EOF
    state: present
    owner: root
    group: root
    mode: '0644'
  tags:
    - nis-install
    - nis-install-client


# ---
# /etc/nsswitch.conf
# ---

#- name: (nis.yml) Check if file '/etc/nsswitch.conf.ORIG' exists
#  stat:
#    path: /etc/nsswitch.conf.ORIG
#  register: nsswitch_conf_orig_exists
#  tags:
#    - nis-install 
#    - nis-install-client
#
#- name: (nis.yml) Backup existing file /etc/nsswitch.conf
#  command: cp -a /etc/nsswitch.conf /etc/nsswitch.conf.ORIG
#  when:
#    - nsswitch_conf_orig_exists.stat.exists == False
#  tags:
#    - nis-install
#    - nis-install-client
#
#- name: (nis-install-client.yml) Adjust file /etc/nsswitch.conf (set hosts)
#  replace:
#    path: /etc/nsswitch.conf
#    regexp: '(hosts:\s+files)\s+([^nis].*)'
#    replace: '\1 nis \2'
#  tags:
#    - nis-install
#    - nis-install-client
#
#- name: (nis-install-client.yml) Adjust file /etc/nsswitch.conf (set passwd/group/shadow)
#  replace:
#    path: /etc/nsswitch.conf
#    regexp: '^({{ item }}:\s+.*(?!nis).*)'
#    replace: '\1 nis'
#  with_items:
#     - passwd
#     - group
#     - shadow
#  tags:
#    - nis-install
#    - nis-install-client
- name: Copy /etc/nsswitch.conf
  copy:
    src: etc/nsswitch.conf
    dest: /etc/nsswitch.conf
    owner: root
    group: root
    mode: 0644

# ---
# /etc/systemd/system/systemd-logind.service.d/nis_allow_network.conf
# ---

# - !! Using NIS client in Ubuntu 18.04 crashes both Gnome and Unity !!
# - ===================================================================
#
# - Unter NIS in Ubuntu 18.04 stütrzt Gnome und Unity ab
# -
# - Abhilfe schafft:
# -
#
# - Create a new directory in /etc/systemd/system/ named exactly after the
# - service you want to extend including a '.d', here this would be:
# -    systemd-logind.service.d
# -
# -    mkdir /etc/systemd/system/systemd-logind.service.d
#
# - Create a new file choose_an_appropriate_name.conf (e.g. nis_allow_network.conf)
# - inside the newly created directory with the following content, which specifies
# - the IP or IP range you want to be allowed:
# -
# -    cat <<EOF > /etc/systemd/system/systemd-logind.service.d/nis_allow_network.conf
# -    [Service]
# -    IPAddressAllow=192.168.0.0/16
# -    EOF
# -
# -    systemctl daemon-reload
# -    systemctl restart systemd-logind.service

- name: (nis-install-client.yml) Ensure directory /etc/systemd/system/systemd-logind.service.d exists
  file:
    path: /etc/systemd/system/systemd-logind.service.d
    owner: root
    group: root
    mode: '0755'
    state: directory
  when: "ansible_distribution_major_version|int  >= 18"
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Ensure file /files/etc/systemd/system/systemd-logind.service.d/nis_allow_network.conf exists
  copy:
    src: "{{ role_path + '/files/etc/systemd/system/systemd-logind.service.d/nis_allow_network.conf' }}"
    dest: /etc/systemd/system/systemd-logind.service.d/nis_allow_network.conf
    owner: root
    group: root
    mode: '0755'
  when: "ansible_distribution_major_version|int  >= 18"
  # XXX: killt meine Xsession (Tim)
  # notify:
  #   - Restart systemd-logind.service
  tags:
    - nis-install
    - nis-install-client


# - Seit Ubuntu 16.04 startet nis vor dem portmapper (rpcbind). Das Starten
# - schlägt deshalb fehl und nis steht nicht zur Verfügung.
# -
# - Abhilfe:
# -
# - Run "systemctl edit rpcbind.socket" and add the following:
# -
# -    [Unit]
# -    DefaultDependencies=no
# -    Wants=rpcbind.target
# -    Before=rpcbind.target
# -
# - You can see your changes:
# -    cat /etc/systemd/system/rpcbind.socket.d/override.conf

- name: (nis-install-client.yml) Ensure directory /etc/systemd/system/rpcbind.socket.d exists
  file:
    path: /etc/systemd/system/rpcbind.socket.d
    owner: root
    group: root
    mode: '0755'
    state: directory
  when: "ansible_distribution_major_version|int  >= 16"
  tags:
    - nis-install
    - nis-install-client

- name: (nis-install-client.yml) Ensure file /files/etc/systemd/system/rpcbind.socket.d/override.conf exists
  copy:
    src: "{{ role_path + '/files/etc/systemd/system/rpcbind.socket.d/override.conf' }}"
    dest: /etc/systemd/system/rpcbind.socket.d/override.conf
    owner: root
    group: root
    mode: '0755'
  when: "ansible_distribution_major_version|int  >= 16"
  notify:
    - Restart rpcbind
  tags:
    - nis-install
    - nis-install-client


# TODO:
# /etc/systemd/system/systemd-logind.service.d/nis_allow_network.conf
# /etc/systemd/system/rpcbind.socket.d/override.conf
