summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ansible/roles/nfs/.yamllint11
-rw-r--r--ansible/roles/nfs/handlers/main.yml3
-rw-r--r--ansible/roles/nfs/molecule/default/molecule.yml44
-rw-r--r--ansible/roles/nfs/molecule/default/playbook.yml5
-rw-r--r--ansible/roles/nfs/molecule/default/tests/test_default.py16
-rw-r--r--ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py15
-rw-r--r--ansible/roles/nfs/molecule/default/tests/test_nfs-server.py25
-rw-r--r--ansible/roles/nfs/tasks/main.yml15
-rw-r--r--ansible/roles/nfs/templates/exports.j24
9 files changed, 130 insertions, 8 deletions
diff --git a/ansible/roles/nfs/.yamllint b/ansible/roles/nfs/.yamllint
new file mode 100644
index 00000000..ad0be760
--- /dev/null
+++ b/ansible/roles/nfs/.yamllint
@@ -0,0 +1,11 @@
+extends: default
+
+rules:
+ braces:
+ max-spaces-inside: 1
+ level: error
+ brackets:
+ max-spaces-inside: 1
+ level: error
+ line-length: disable
+ truthy: disable
diff --git a/ansible/roles/nfs/handlers/main.yml b/ansible/roles/nfs/handlers/main.yml
new file mode 100644
index 00000000..4b8deaeb
--- /dev/null
+++ b/ansible/roles/nfs/handlers/main.yml
@@ -0,0 +1,3 @@
+---
+- name: reload nfs
+ command: exportfs -ra
diff --git a/ansible/roles/nfs/molecule/default/molecule.yml b/ansible/roles/nfs/molecule/default/molecule.yml
new file mode 100644
index 00000000..552f4bcc
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/molecule.yml
@@ -0,0 +1,44 @@
+---
+dependency:
+ name: galaxy
+driver:
+ name: docker
+lint:
+ name: yamllint
+platforms:
+ - name: kubernetes-node-1
+ image: molecule-${PREBUILD_PLATFORM_DISTRO:-centos7}:${PREBUILD_DISTRO_VERSION:-latest}
+ pre_build_image: true
+ privileged: true
+ command: ${MOLECULE_DOCKER_COMMAND:-""}
+ groups:
+ - kubernetes
+ - nfs-server
+ networks:
+ - name: nfs-net
+ volumes:
+ - /sys/fs/cgroup:/sys/fs/cgroup:ro
+ - ${HOME}/data:/dockerdata-nfs:rw
+
+ - name: kubernetes-node-2
+ image: molecule-${PREBUILD_PLATFORM_DISTRO:-centos7}:${PREBUILD_DISTRO_VERSION:-latest}
+ pre_build_image: true
+ privileged: true
+ command: ${MOLECULE_DOCKER_COMMAND:-""}
+ groups:
+ - kubernetes
+ networks:
+ - name: nfs-net
+ volumes:
+ - /sys/fs/cgroup:/sys/fs/cgroup:ro
+provisioner:
+ name: ansible
+ inventory:
+ links:
+ group_vars: ../../../../group_vars
+ lint:
+ name: ansible-lint
+verifier:
+ name: testinfra
+ lint:
+ name: flake8
diff --git a/ansible/roles/nfs/molecule/default/playbook.yml b/ansible/roles/nfs/molecule/default/playbook.yml
new file mode 100644
index 00000000..37aad5b9
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/playbook.yml
@@ -0,0 +1,5 @@
+---
+- name: Converge
+ hosts: all
+ roles:
+ - role: nfs
diff --git a/ansible/roles/nfs/molecule/default/tests/test_default.py b/ansible/roles/nfs/molecule/default/tests/test_default.py
new file mode 100644
index 00000000..48139898
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/tests/test_default.py
@@ -0,0 +1,16 @@
+import os
+import pytest
+
+import testinfra.utils.ansible_runner
+
+testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
+ os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
+
+
+@pytest.mark.parametrize('pkg', [
+ 'nfs-utils'
+])
+def test_pkg(host, pkg):
+ package = host.package(pkg)
+
+ assert package.is_installed
diff --git a/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py b/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py
new file mode 100644
index 00000000..b702a73d
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py
@@ -0,0 +1,15 @@
+import os
+
+import testinfra.utils.ansible_runner
+
+testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
+ os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('kubernetes-node-2')
+
+
+def test_nfs_mount(host):
+ mp = host.mount_point("/dockerdata-nfs")
+ assert mp.exists
+ assert mp.filesystem == "nfs"
+ assert mp.device == "kubernetes-node-1:/dockerdata-nfs"
+ assert host.file("/etc/fstab").\
+ contains("kubernetes-node-1:/dockerdata-nfs /dockerdata-nfs nfs")
diff --git a/ansible/roles/nfs/molecule/default/tests/test_nfs-server.py b/ansible/roles/nfs/molecule/default/tests/test_nfs-server.py
new file mode 100644
index 00000000..078c653e
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/tests/test_nfs-server.py
@@ -0,0 +1,25 @@
+import os
+import pytest
+
+import testinfra.utils.ansible_runner
+
+testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
+ os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('nfs-server')
+
+
+@pytest.mark.parametrize('svc', [
+ 'rpcbind',
+ 'nfs-server'
+])
+def test_svc(host, svc):
+ service = host.service(svc)
+
+ assert service.is_running
+ assert service.is_enabled
+
+
+def test_exports(host):
+ f = host.file("/etc/exports")
+ assert f.exists
+ assert f.content_string == \
+ """/dockerdata-nfs kubernetes-node-2(rw,sync,no_root_squash,no_subtree_check)""" # noqa: E501
diff --git a/ansible/roles/nfs/tasks/main.yml b/ansible/roles/nfs/tasks/main.yml
index aca1771f..e7580b66 100644
--- a/ansible/roles/nfs/tasks/main.yml
+++ b/ansible/roles/nfs/tasks/main.yml
@@ -9,7 +9,7 @@
package:
name: "{{ item }}"
state: present
- with_items: "{{ nfs_packages[ansible_os_family] }}"
+ loop: "{{ nfs_packages[ansible_os_family] }}"
- name: Setup nfs server
block:
@@ -17,22 +17,25 @@
systemd:
name: "{{ item }}"
state: started
- with_items: "{{ nfs_services[ansible_os_family] }}"
+ enabled: true
+ loop: "{{ nfs_services[ansible_os_family] }}"
- name: Add hosts to exports
template:
src: exports.j2
dest: /etc/exports
-
- - name: Export nfs
- command: exportfs -ar
+ notify:
+ - reload nfs
when:
- "'nfs-server' in group_names"
+- name: Force notified handlers to run at this point
+ meta: flush_handlers
+
- name: Mount dockerdata-nfs
mount:
path: "{{ nfs_mount_path }}"
- src: "{{ hostvars[groups['nfs-server'].0].ansible_host }}:{{ nfs_mount_path }}"
+ src: "{{ hostvars[groups['nfs-server'].0].ansible_host | default(hostvars[groups['nfs-server'].0].inventory_hostname) }}:{{ nfs_mount_path }}"
fstype: nfs
state: mounted
when:
diff --git a/ansible/roles/nfs/templates/exports.j2 b/ansible/roles/nfs/templates/exports.j2
index 1f6956c2..465c9d21 100644
--- a/ansible/roles/nfs/templates/exports.j2
+++ b/ansible/roles/nfs/templates/exports.j2
@@ -1,3 +1,3 @@
-{% for host in groups.kubernetes[1:] -%}
- {{ nfs_mount_path }} {{ hostvars[host].ansible_host }}(rw,sync,no_root_squash,no_subtree_check)
+{% for host in groups.kubernetes | difference(groups['nfs-server']) -%}
+ {{ nfs_mount_path }} {{ hostvars[host].ansible_host | default(hostvars[host].inventory_hostname) }}(rw,sync,no_root_squash,no_subtree_check)
{% endfor %}