infra/roles/nfs/tasks/main.yml

168 lines
3.2 KiB
YAML

---
- name: test
debug:
var: nfs_export_paths
- name: install nfs
apt:
name:
- nfs-common
- nfs-kernel-server
state: latest
update_cache: true
tags:
- packages
- name: ensure /export exists
file:
state: directory
path: /export
owner: root
group: root
mode: 0755
when:
- nfs_exports | length > 0
- name: bind mount exported paths in /export
mount:
path: /export/{{ item.path.lstrip("/") }}
src: "{{ item.path }}"
opts: bind
state: mounted # adds to fstab
fstype: none
loop_control:
label: |-
{{ item.path }}
tags:
- nfs-export
- nfs-exports
when:
- nfs_exports | length > 0
with_items: "{{ nfs_exports }}"
- name: template /etc/exports
template:
src: "{{ item }}.j2"
dest: /etc/{{ item }}
owner: root
group: root
with_items:
- exports
tags:
- nfs-export
- nfs-exports
when:
- nfs_exports | length > 0
notify:
- exportfs -r
- name: template configs
template:
src: "{{ item }}.j2"
dest: /etc/{{ item }}
owner: root
group: root
with_items:
- nfs.conf
- default/nfs-kernel-server
- default/rpcbind
- default/nfs-common
notify:
- reload nfs-kernel-server
#- reload idmap
tags:
- nfs-server-config
- name: load kernel module
modprobe:
name: "{{ item }}"
state: present
with_items:
- nfsd
- nfs
tags:
- nfs-module
- name: daemon reload
systemd:
daemon_reload: true
tags:
- systemd
# rpcbind should not be needed
# https://wiki.debian.org/NFSServerSetup
- name: mask rpcbind (since only NFSv4 or higher is enabled, which does need rpcbind)
service:
name: "{{ item }}"
masked: "{{ nfs_mask_rpc|default(true) }}"
loop_control:
label: "{{ item }} masked={{nfs_mask_rpc|default(true)}}"
with_items:
- rpcbind.service
- rpcbind.socket
- name: enable nfs-kernel-server
service:
name: nfs-kernel-server
enabled: true
state: started
when:
- nfs_exports | length > 0
- name: template systemd mounts
template:
src: systemd-mount.j2
dest: "/etc/systemd/system/{{ systemd_name }}"
owner: root
group: root
mode: 0644
loop_control:
label: "{{ systemd_name }}"
vars:
systemd_name: "{{ item.where.split('/')[1:] | join('-') }}.mount"
when:
- nfs_mounts | length > 0
- systemd_name != ".mount"
tags:
- nfs-mounts
with_items: "{{ nfs_mounts }}"
- name: start nfs mounts
service:
daemon_reload: true
name: "{{ systemd_name }}"
state: started
enabled: true
loop_control:
label: "{{ systemd_name }}"
vars:
systemd_name: "{{ item.where.split('/')[1:] | join('-') }}.mount"
when:
- nfs_mounts | length > 0
- systemd_name != ".mount"
- item.enabled|default(true)
with_items: "{{ nfs_mounts }}"
tags:
- nfs-mounts
- name: stop nfs mounts
service:
daemon_reload: true
name: "{{ systemd_name }}"
state: stopped
enabled: false
loop_control:
label: "{{ systemd_name }}"
vars:
systemd_name: "{{ item.where.split('/')[1:] | join('-') }}.mount"
when:
- nfs_mounts | length > 0
- systemd_name != ".mount"
- not item.enabled|default(true)
with_items: "{{ nfs_mounts }}"
tags:
- nfs-mounts