Improve nfs role

This commit improves the nfs role by
using cluster_ip variable instead of ansible_host for getting
nfs client/server address, and
using /etc/exports.d instead of /etc/exports for
exporting nfs filesystem.
Also, this commit fixes other
issues in molecule found along the way.

Change-Id: Ibd4ea5d4fd937116e2dc9e1b6f85c3e019f62e17
Issue-ID: OOM-1770
Signed-off-by: Michal Zegan <m.zegan@samsung.com>
diff --git a/ansible/roles/nfs/molecule/default/cleanup.yml b/ansible/roles/nfs/molecule/default/cleanup.yml
new file mode 100644
index 0000000..a085bd5
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/cleanup.yml
@@ -0,0 +1,6 @@
+---
+- name: Cleanup
+  hosts: all
+  ignore_unreachable: true
+  roles:
+    - cleanup-nfs
diff --git a/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-1.yml b/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-1.yml
new file mode 100644
index 0000000..27a521b
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-1.yml
@@ -0,0 +1,2 @@
+---
+cluster_ip: "{{ ansible_default_ipv4.address }}"
diff --git a/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-2.yml b/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-2.yml
new file mode 100644
index 0000000..27a521b
--- /dev/null
+++ b/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-2.yml
@@ -0,0 +1,2 @@
+---
+cluster_ip: "{{ ansible_default_ipv4.address }}"
diff --git a/ansible/roles/nfs/molecule/default/molecule.yml b/ansible/roles/nfs/molecule/default/molecule.yml
index 71e08d0..f6610ec 100644
--- a/ansible/roles/nfs/molecule/default/molecule.yml
+++ b/ansible/roles/nfs/molecule/default/molecule.yml
@@ -14,6 +14,7 @@
     groups:
       - kubernetes
       - nfs-server
+    purge_networks: true
     networks:
       - name: nfs-net
     volumes:
@@ -27,15 +28,21 @@
     command: ${MOLECULE_DOCKER_COMMAND:-""}
     groups:
       - kubernetes
+    purge_networks: true
     networks:
       - name: nfs-net
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
 provisioner:
   name: ansible
+  playbooks:
+    cleanup: cleanup.yml
+  env:
+    ANSIBLE_ROLES_PATH: "../../../../test/roles"
   inventory:
     links:
       group_vars: ../../../../group_vars
+      host_vars: host_vars
   lint:
     name: ansible-lint
 verifier:
diff --git a/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py b/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py
index b702a73..0e3710c 100644
--- a/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py
+++ b/ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py
@@ -1,5 +1,6 @@
 import os
 
+import testinfra
 import testinfra.utils.ansible_runner
 
 testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
@@ -7,9 +8,11 @@
 
 
 def test_nfs_mount(host):
+    node1_ip = testinfra.get_host("docker://kubernetes-node-1").interface(
+        "eth0").addresses[0]
     mp = host.mount_point("/dockerdata-nfs")
     assert mp.exists
-    assert mp.filesystem == "nfs"
-    assert mp.device == "kubernetes-node-1:/dockerdata-nfs"
+    assert mp.filesystem == "nfs" or mp.filesystem == "nfs4"
+    assert mp.device == node1_ip + ":/dockerdata-nfs"
     assert host.file("/etc/fstab").\
-        contains("kubernetes-node-1:/dockerdata-nfs /dockerdata-nfs nfs")
+        contains(node1_ip + ":/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
index 078c653..88ba0a6 100644
--- a/ansible/roles/nfs/molecule/default/tests/test_nfs-server.py
+++ b/ansible/roles/nfs/molecule/default/tests/test_nfs-server.py
@@ -1,6 +1,7 @@
 import os
 import pytest
 
+import testinfra
 import testinfra.utils.ansible_runner
 
 testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
@@ -19,7 +20,9 @@
 
 
 def test_exports(host):
-    f = host.file("/etc/exports")
+    node2_ip = testinfra.get_host("docker://kubernetes-node-2").interface(
+        "eth0").addresses[0]
+    f = host.file("/etc/exports.d/dockerdata-nfs.exports")
     assert f.exists
     assert f.content_string == \
-        """/dockerdata-nfs  kubernetes-node-2(rw,sync,no_root_squash,no_subtree_check)"""  # noqa: E501
+        """/dockerdata-nfs  """ + node2_ip + """(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 e7580b6..1d84887 100644
--- a/ansible/roles/nfs/tasks/main.yml
+++ b/ansible/roles/nfs/tasks/main.yml
@@ -23,7 +23,7 @@
     - name: Add hosts to exports
       template:
         src: exports.j2
-        dest: /etc/exports
+        dest: /etc/exports.d/dockerdata-nfs.exports
       notify:
         - reload nfs
   when:
@@ -35,7 +35,7 @@
 - name: Mount dockerdata-nfs
   mount:
     path: "{{ nfs_mount_path }}"
-    src: "{{ hostvars[groups['nfs-server'].0].ansible_host | default(hostvars[groups['nfs-server'].0].inventory_hostname) }}:{{ nfs_mount_path }}"
+    src: "{{ hostvars[groups['nfs-server'].0].cluster_ip }}:{{ 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 465c9d2..f49c499 100644
--- a/ansible/roles/nfs/templates/exports.j2
+++ b/ansible/roles/nfs/templates/exports.j2
@@ -1,3 +1,3 @@
 {% 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)
+    {{ nfs_mount_path }}  {{ hostvars[host].cluster_ip }}(rw,sync,no_root_squash,no_subtree_check)
 {% endfor %}