Ansible Emergency Hotline Series: Automated Management of Linux Services – (15) Automated Deployment and Configuration of NFS Servers
#Linux #Automation #Ansible #NFS
🤩 Introduction: Still configuring NFS manually? Jump on the Ansible automation express!
Dear “warriors” in the operations and maintenance field, are you still troubled by manually configuring NFS (Network File System)? 😫
Editing the <span><span>/etc/exports</span></span> file, a single space or a typo can lead to a complete failure of the share. Maintaining configuration consistency across dozens or hundreds of servers is a headache-inducing “extreme challenge.” Improper permission settings can also pose serious security risks!
To completely eliminate this pain point, this issue of Ansible Emergency Hotline🔥 is here! We will show you an enterprise-level Ansible automation solution that allows you to install, configure, harden, and validate NFS servers with just one click. Making file sharing unprecedentedly simple, reliable, and secure! 🚀
🤔 Design Philosophy: Why is our Playbook a Best Practice?
A professional automation solution is not just a simple pile of commands. Our design philosophy incorporates the core practices advocated by Red Hat, allowing your automation solution to leap from “it just works” to “professional and reliable”!
1Modular Configuration, No Interference ✨ We do not directly and crudely modify the main configuration file <span><span>/etc/exports</span></span>, but instead use the more modern and secure <span><span>/etc/exports.d/</span></span> directory to manage share declarations. Each share can be an independent file, meaning you can add, delete, or modify a share independently without worrying about affecting other existing shares. This is crucial in collaborative and complex environments!2Variable-Driven, Flexible Adaptation 💻 We centralize all variable parameters (such as shared directories, client addresses, permission options) in the <span><span>vars</span></span> section at the top of the Playbook. This means that when you need to share a new directory or authorize a new client, you only need to modify these variables without touching any core automation task logic. This greatly enhances the Playbook’s flexibility and maintainability.3Idempotency Assurance, Safe and Worry-Free ✅ All our Playbooks strictly adhere to Ansible’s core principle of idempotency. You can confidently execute this Playbook repeatedly; Ansible will automatically detect the current state and only apply necessary changes. For example, it will not recreate directories that already exist. This ensures the safety and predictability of operations, eliminating unexpected issues caused by “shaky hands”.4Closed-Loop Verification, Visible Results 🎯 The last step of the Playbook is to execute <span><span>exportfs -rav</span></span> and display the results. This forms a configuration-verification closed loop. You not only execute automation but can immediately see the actual state after the configuration takes effect, ensuring everything is under control!
📈 Multi-Dimensional Scoring of Automation Scenarios
| Dimension | Score | One-Sentence Comment 💬 |
|---|---|---|
| Ease of Use | ⭐ | Logical clarity, centralized variables, very friendly for beginners. |
| Reusability | ⭐ ⭐ ⭐ ⭐ | Highly variable, can be easily reused for any NFS scenario by modifying <span><span>vars</span></span>. |
| Stability | ⭐ ⭐ ⭐ ⭐ ⭐ | Follows Linux standard configuration methods, combined with Ansible idempotency, extremely stable. |
| Scalability | ⭐ ⭐ ⭐ ⭐ | Easy to scale, just add variables or copy tasks to manage multiple shares. |
| Best Practice Compliance | ⭐ ⭐ ⭐ | Textbook-level example, perfectly embodies the philosophy of modern Linux service management. |
🛠️ Foolproof Deployment Guide
Seeing it a thousand times in theory is not as good as doing it once!
Prerequisites
1One Ansible control node.2The target server is configured with SSH trust, and the user executing Ansible has <span><span>sudo</span></span> permissions.3The control node has Ansible installed.
Project Directory Structure
This is a very simple project; you only need two files!
nfs-automation/
├── inventory
└── deploy_nfs_server.yml
File Content Overview (Copy and Paste Directly)
1. Host Inventory: <span><span>inventory</span></span>
[nfs_servers]
serverd.example.com
2. Core Playbook: <span><span>deploy_nfs_server.yml</span></span>
---
- name: Automate deployment and configuration of NFS server
hosts: nfs_servers
become: true
vars:
nfs_shared_directory: /srv/myshare
nfs_export_client: "client1.example.com"
nfs_export_options: "rw,no_root_squash"
tasks:
- name: Check if the operating system is RHEL/CentOS 8/9
ansible.builtin.assert:
that:
- ansible_distribution in ['RedHat', 'CentOS']
- ansible_distribution_major_version in ['8', '9']
fail_msg: "This Playbook only supports RHEL 8/9 or CentOS 8/9 series operating systems."
quiet: true
- name: 1) Ensure nfs-utils package is installed
ansible.builtin.yum:
name: nfs-utils
state: present
- name: 2) Ensure the directory to be exported exists and has the correct permissions
ansible.builtin.file:
path: "{{ nfs_shared_directory }}"
state: directory
owner: root
group: root
mode: '0755'
- name: 3) Declare shares in /etc/exports.d/
ansible.builtin.copy:
dest: /etc/exports.d/ansible-share.exports
content:"{{ nfs_shared_directory }} {{ nfs_export_client }}({{ nfs_export_options }})\n"
owner: root
group: root
mode: '0644'
notify: reload nfs exports
- name: 4) Ensure nfs-server service is started and set to start on boot
ansible.builtin.service:
name: nfs-server
state: started
enabled: true
- name: 5) Ensure the firewall allows nfs service
ansible.builtin.firewalld:
service: nfs
state: enabled
immediate: true
permanent: true
- name: 6) Refresh and list current active NFS shares for verification
ansible.builtin.command: exportfs -rav
register: exportfs_output
changed_when: "'re-exporting' in exportfs_output.stderr"
- name: Display configured NFS shares
ansible.builtin.debug:
var: exportfs_output.stdout_lines
handlers:
- name: reload nfs exports
listen: "reload nfs exports"
ansible.builtin.service:
name: nfs-server
state: reloaded
How to Use?
1
Modify Variables ✏️: Open the <span><span>deploy_nfs_server.yml</span></span> file and modify the contents of the <span><span>vars</span></span> section according to your needs, such as the directory to be shared, client address, and permission options.
2
Update Host Inventory 📝: Edit the <span><span>inventory</span></span> file and fill in your NFS server hostname or IP address.
3
Execute Automation ▶️: Run the following command, then you can go make a cup of coffee ☕️!
ansible-playbook -i inventory deploy_nfs_server.yml
🎁 Surprise Time! Get the Complete Annotated Version!
Do you find the above Playbook not detailed enough? Want to delve into the logic behind each line of code and the best practices recommended by the official documentation?
Let you not only use it but also be able to extrapolate, becoming the most outstanding Ansible automation expert in your team!
👉 Click the link below to get the complete annotated and syntax-highlighted Playbook project for download! 👈
Core Playbook: <span><span>deploy_nfs_server.yml</span></span>
---
- name: Automate deployment and configuration of NFS server
hosts: nfs_servers # Please define this group in your inventory file
become: true
# ======================================================================
# Variable Definitions
# ------------------------------------------------------------------------
# Centralize all configurable items here for easy management and modification.
# ====================================================================
vars:
nfs_shared_directory: /srv/myshare
nfs_export_client: "client1.example.com"
nfs_export_options: "rw,no_root_squash"
tasks:
# ======================================================================
# 1. Pre-flight Checks
Click the link below to get the complete annotated and syntax-highlighted Playbook project