Skip to content

Commit

Permalink
[NEW] added NFS playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Oct 2, 2024
1 parent 02088b1 commit 63eba1e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ansible.python.interpreterPath": "/bin/python"
}
64 changes: 64 additions & 0 deletions Ansible/nfs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# NFS Server Setup with Ansible 🚀

![Ansible](https://img.shields.io/badge/Ansible-Playbook-blue?logo=ansible)
![NFS](https://img.shields.io/badge/NFS-Server-yellowgreen)

This playbook allows you to set up and configure an NFS server automatically. It supports different Linux distributions such as **RedHat** and **Debian** families. The process includes installing the NFS server, creating a directory, and exporting it to the network.

## Purpose 🎯

- Set up an **NFS (Network File System)** server and enable file sharing over the network.
- Be compatible with both RedHat and Debian-based systems.
- Create an NFS directory, export it, and manage the service.

## Requirements 📋

- Ansible must be installed.
- The `nfs_server` group in your inventory should contain the host(s) that will act as the NFS server.

## Variables 📂

- `nfs_export_path`: The path to the directory you want to share over NFS.
- `nfs_allowed_hosts`: Hosts allowed to access the shared directory.
- `nfs_service_name`: Variable to manage NFS service names for RedHat and Debian families.

## How to Use ⚙️

1. **Prepare an inventory file**, for example:
```ini
[nfs_server]
nfs.example.com ansible_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
```

2. **Run the playbook:**
```bash
ansible-playbook -i inventory nfs_setup.yml
```

3. **What the playbook does:**
- Installs required NFS packages.
- Creates the export directory and sets the correct permissions.
- Updates the `/etc/exports` file.
- Starts the NFS service and exports the directory.

## Testing 🧪

1. Check that the NFS service is running on the server:
```bash
systemctl status nfs-server
```

2. Verify that the directory is exported:
```bash
exportfs -v
```

3. Mount the NFS share from another machine to test:
```bash
mount nfs.example.com:/srv/nfs /mnt
```

## Supported OS Families 🖥️

- **RedHat** family: RHEL, CentOS, Fedora
- **Debian** family: Debian, Ubuntu
57 changes: 57 additions & 0 deletions Ansible/nfs/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
- hosts: nfs_server
become: yes
vars:
nfs_export_path: /srv/nfs
nfs_allowed_hosts: "*"
nfs_service_name:
RedHat: nfs-server
Debian: nfs-kernel-server

tasks:
- name: Install NFS packages (RedHat)
yum:
name: nfs-utils
state: present
when: ansible_os_family == "RedHat"

- name: Install NFS packages (Debian)
apt:
name:
- nfs-kernel-server
- nfs-common
state: present
update_cache: yes
when: ansible_os_family == "Debian"

- name: Create NFS export directory
file:
path: "{{ nfs_export_path }}"
state: directory
owner: root
group: root
mode: '0755'

- name: Configure NFS exports
copy:
content: "{{ nfs_export_path }} {{ nfs_allowed_hosts }}(rw,sync,no_root_squash,no_subtree_check)\n"
dest: /etc/exports
owner: root
group: root
mode: '0644'

- name: Start and enable NFS service
systemd:
name: "{{ nfs_service_name[ansible_os_family] }}"
enabled: yes
state: started

- name: Export NFS directories
command: exportfs -ra
notify: restart nfs

handlers:
- name: restart nfs
systemd:
name: "{{ nfs_service_name[ansible_os_family] }}"
state: restarted
4 changes: 4 additions & 0 deletions Ansible/nfs/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[nfs_server]
debian ansible_host=142.93.3.181 ansible_user=root
ubuntu ansible_host=104.248.116.57 ansible_user=root
rhel ansible_host=137.184.198.185 ansible_user=root

0 comments on commit 63eba1e

Please sign in to comment.