NFS:使用 Ansible 自动化配置 NFS 客户端服务端
- 考试顺便整理
- 博文内容整理涉及使用 Ansible 部署 NFS 客户端和服务端
- 理解不足小伙伴帮忙指正
对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是对大众理想的懦弱回归,是随波逐流,是对内心的恐惧 ——赫尔曼·黑塞《德米安》
涉及到的文件
[student@workstation filestorage-automation]$ ls
ansible.cfg inventory nfs_client.yml nfs_server.yml smb_client.yml smb_server.yml smb_vars.yml templates
[student@workstation filestorage-automation]$ tree .
.
├── ansible.cfg
├── inventory
├── nfs_client.yml
├── nfs_server.yml
└── templates└── share.exports.j2
配置文件和主机清单文件,不多解释
[student@workstation filestorage-automation]$ cat ansible.cfg
[defaults]
inventory=inventory
remote_user=devops
[student@workstation filestorage-automation]$ cat inventory
[servers]
serverd.lab.example.com[clients]
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
[student@workstation filestorage-automation]$
客户端配置
- 安装nfs-utils软件包:使用yum模块确保目标主机上安装了nfs-utils软件包。
- 挂载NFS共享并添加到/etc/fstab:使用mount模块将NFS共享挂载到指定的挂载点,fstype参数设置为nfs表示文件系统类型为NFS。
[student@workstation filestorage-automation]$ cat nfs_client.yml
---
- name: Access an NFS exporthosts: servera.lab.example.combecome: truevars:shared_dir: /nfssharemount_point: /datanfstasks:- name: the nfs-utils package is installedyum:name: nfs-utilsstate: present- name: the NFS export is mounted and in /etc/fstabmount:path: "{{ mount_point }}"src: serverd.lab.example.com:{{ shared_dir }}state: mountedfstype: nfs
服务端配置
- 安装
nfs-utils
软件包:使用yum
模块确保目标主机上安装了nfs-utils
软件包。 - 目录存在性检查:使用file模块检查目标目录(
{{ shared_dir }}
)是否存在。如果目录不存在,则使用指定的所有者、组和权限创建该目录。 - 导出目录:使用template模块根据模板文件(
templates/share.exports.j2
)生成NFS导出配置文件(/etc/exports.d/share.exports
)。该配置文件定义了要导出的目录以及访问权限等参数。导出配置文件的所有者、组和权限也被指定。 - 启动并启用
nfs-server
服务:使用service模块启动并启用nfs-server服务,确保NFS服务器正在运行,并在系统启动时自动启用该服务。 - 打开nfs防火墙服务:使用firewalld模块打开nfs防火墙服务,确保NFS流量可以通过防火墙。service参数指定要打开的服务为nfs,state参数设置为enabled表示启用该服务,immediate和permanent参数设置为yes表示立即生效并在系统重启后仍然生效。
[student@workstation filestorage-automation]$ cat nfs_server.yml
---
- name: Export a directory using NFShosts: serverd.lab.example.combecome: truevars:shared_dir: /nfssharetasks:- name: the nfs-utils package is installedyum:name: nfs-utilsstate: present- name: the directory existsfile:path: "{{ shared_dir }}"owner: studentgroup: rootmode: '0755'state: directory- name: the directory is exportedtemplate:src: templates/share.exports.j2dest: /etc/exports.d/share.exportsowner: rootgroup: rootmode: '0644'notify: reload exports- name: the nfs-server service is started and enabledservice:name: nfs-serverstate: startedenabled: yes- name: the nfs firewall service is openedfirewalld:service: nfsstate: enabledimmediate: yespermanent: yeshandlers:- name: reload exportsservice:name: nfs-serverstate: reloaded
[student@workstation filestorage-automation]$
生成NFS共享的导出配置文件
[student@workstation filestorage-automation]$ cat templates/share.exports.j2
{{ shared_dir }}{% for host in groups['clients'] %}{{ host }}(rw)
{%- endfor %}
博文部分内容参考
© 文中涉及参考链接内容版权归原作者所有,如有侵权请告知,这是一个开源项目,如果你认可它,不要吝啬星星哦 😃
红帽服务管理与自动化(RH358)
授课笔记
© 2018-2023 liruilonger@gmail.com, All rights reserved. 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)