From a4688b6f990bf2e671773e10a591a91cf2e46ab7 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 9 Mar 2022 14:17:07 +0100 Subject: [PATCH] Feat: init nfs client role --- roles/nfs_client/README.md | 38 +++++++++++++++++++ roles/nfs_client/defaults/main.yml | 7 ++++ roles/nfs_client/handlers/main.yml | 2 + roles/nfs_client/molecule/default/INSTALL.rst | 23 +++++++++++ .../nfs_client/molecule/default/converge.yml | 24 ++++++++++++ .../nfs_client/molecule/default/molecule.yml | 19 ++++++++++ .../molecule/default/tests/conftest.py | 22 +++++++++++ .../molecule/default/tests/test_default.py | 7 ++++ roles/nfs_client/tasks/main.yml | 31 +++++++++++++++ roles/nfs_client/tests/inventory | 2 + roles/nfs_client/tests/test.yml | 5 +++ roles/nfs_client/vars/main.yml | 2 + 12 files changed, 182 insertions(+) create mode 100644 roles/nfs_client/README.md create mode 100644 roles/nfs_client/defaults/main.yml create mode 100644 roles/nfs_client/handlers/main.yml create mode 100644 roles/nfs_client/molecule/default/INSTALL.rst create mode 100644 roles/nfs_client/molecule/default/converge.yml create mode 100644 roles/nfs_client/molecule/default/molecule.yml create mode 100644 roles/nfs_client/molecule/default/tests/conftest.py create mode 100644 roles/nfs_client/molecule/default/tests/test_default.py create mode 100644 roles/nfs_client/tasks/main.yml create mode 100644 roles/nfs_client/tests/inventory create mode 100644 roles/nfs_client/tests/test.yml create mode 100644 roles/nfs_client/vars/main.yml diff --git a/roles/nfs_client/README.md b/roles/nfs_client/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/roles/nfs_client/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/nfs_client/defaults/main.yml b/roles/nfs_client/defaults/main.yml new file mode 100644 index 0000000..6792a59 --- /dev/null +++ b/roles/nfs_client/defaults/main.yml @@ -0,0 +1,7 @@ +--- +# defaults file for nfs_client +shares: + - mount_point: /media/nfs/default + server: test.lan + export: /test + options: defaults diff --git a/roles/nfs_client/handlers/main.yml b/roles/nfs_client/handlers/main.yml new file mode 100644 index 0000000..58bddd2 --- /dev/null +++ b/roles/nfs_client/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for nfs_client diff --git a/roles/nfs_client/molecule/default/INSTALL.rst b/roles/nfs_client/molecule/default/INSTALL.rst new file mode 100644 index 0000000..0c4bf5c --- /dev/null +++ b/roles/nfs_client/molecule/default/INSTALL.rst @@ -0,0 +1,23 @@ +********************************* +Vagrant driver installation guide +********************************* + +Requirements +============ + +* Vagrant +* Virtualbox, Parallels, VMware Fusion, VMware Workstation or VMware Desktop + +Install +======= + +Please refer to the `Virtual environment`_ documentation for installation best +practices. If not using a virtual environment, please consider passing the +widely recommended `'--user' flag`_ when invoking ``pip``. + +.. _Virtual environment: https://virtualenv.pypa.io/en/latest/ +.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site + +.. code-block:: bash + + $ pip install 'molecule_vagrant' diff --git a/roles/nfs_client/molecule/default/converge.yml b/roles/nfs_client/molecule/default/converge.yml new file mode 100644 index 0000000..b0ae013 --- /dev/null +++ b/roles/nfs_client/molecule/default/converge.yml @@ -0,0 +1,24 @@ +--- +- name: Converge + hosts: all + become: yes + vars: + - shares: + - mount_point: /media/nfs/default + server: test.lan + export: /test + options: defaults + - mount_point: /media/nfs/other + server: test.lan + export: /other + options: defaults + + pre_tasks: + - name: Update apt cache + apt: + update_cache: yes + + tasks: + - name: "Include nfs_client" + include_role: + name: "nfs_client" diff --git a/roles/nfs_client/molecule/default/molecule.yml b/roles/nfs_client/molecule/default/molecule.yml new file mode 100644 index 0000000..d5eed35 --- /dev/null +++ b/roles/nfs_client/molecule/default/molecule.yml @@ -0,0 +1,19 @@ +--- +dependency: + name: galaxy +driver: + name: vagrant + provider: + name: virtualbox +platforms: + - name: Debian + box: "debian/bullseye64" + pre_build_image: true + +provisioner: + name: ansible +verifier: + name: testinfra + options: + sudo: true + v: 3 diff --git a/roles/nfs_client/molecule/default/tests/conftest.py b/roles/nfs_client/molecule/default/tests/conftest.py new file mode 100644 index 0000000..f7ddb3f --- /dev/null +++ b/roles/nfs_client/molecule/default/tests/conftest.py @@ -0,0 +1,22 @@ +"""PyTest Fixtures.""" +from __future__ import absolute_import + +import os + +import pytest + + +def pytest_runtest_setup(item): + """Run tests only when under molecule with testinfra installed.""" + try: + import testinfra + except ImportError: + pytest.skip("Test requires testinfra", allow_module_level=True) + if "MOLECULE_INVENTORY_FILE" in os.environ: + pytest.testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ["MOLECULE_INVENTORY_FILE"] + ).get_hosts("all") + else: + pytest.skip( + "Test should run only from inside molecule.", allow_module_level=True + ) diff --git a/roles/nfs_client/molecule/default/tests/test_default.py b/roles/nfs_client/molecule/default/tests/test_default.py new file mode 100644 index 0000000..67cb559 --- /dev/null +++ b/roles/nfs_client/molecule/default/tests/test_default.py @@ -0,0 +1,7 @@ +"""Role testing files using testinfra.""" +import pytest + +def test_installed_packages(host): + nfs_common = host.package("nfs-common") + assert nfs_common.is_installed + diff --git a/roles/nfs_client/tasks/main.yml b/roles/nfs_client/tasks/main.yml new file mode 100644 index 0000000..66649ef --- /dev/null +++ b/roles/nfs_client/tasks/main.yml @@ -0,0 +1,31 @@ +--- +# tasks file for nfs_client + +- name: Install nfs-common + apt: + name: "{{ item }}" + state: present + with_items: + - nfs-common + +- name: Ensure rpcbind is running (Debian) + systemd: + name: rpcbind + state: started + enabled: yes + +- name: Ensure nfs mount points exist + file: + path: "{{ item.mount_point }}" + state: directory + with_items: "{{ nfs_shares }}" + +- name: Add nfs shares to fstab + mount: + src: "{{ item.server }}:{{ item.export }}" + path: "{{ item.mount_point }}" + opts: "{{ item.options }}" + state: mounted + fstype: nfs + with_items: "{{ nfs_shares }}" + diff --git a/roles/nfs_client/tests/inventory b/roles/nfs_client/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/roles/nfs_client/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/roles/nfs_client/tests/test.yml b/roles/nfs_client/tests/test.yml new file mode 100644 index 0000000..f6dae93 --- /dev/null +++ b/roles/nfs_client/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - nfs_client diff --git a/roles/nfs_client/vars/main.yml b/roles/nfs_client/vars/main.yml new file mode 100644 index 0000000..d623650 --- /dev/null +++ b/roles/nfs_client/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for nfs_client