单机安装

  1. yum install nfs-utils -y
  2. sudo systemctl enable rpcbind
  3. sudo systemctl enable nfs

启动

  1. sudo systemctl restart rpcbind
  2. sudo systemctl restart nfs

数据共享

  1. mkdir /data/nfs
  2. chmod 755 /data/nfs
  3. vi /etc/exports
  4. /data/nfs/ *(rw,sync,no_root_squash,no_all_squash)
  5. systemctl restart nfs
  6. showmount -e localhost

安装nfs的playbook,实现开机自启

  1. - name: Install and configure NFS server
  2. hosts: modify ###如果共享目录是/data/nfs,就只需修改这里
  3. tasks:
  4. - name: Install NFS server package
  5. yum:
  6. name: nfs-utils
  7. state: present
  8. - name: ensure and start nfs
  9. systemd:
  10. name: nfs
  11. enabled: yes
  12. state: started
  13. - name: Create shared directory
  14. file:
  15. path: /data/nfs
  16. state: directory
  17. mode: '0755'
  18. - name: Configure NFS exports
  19. lineinfile:
  20. path: /etc/exports
  21. line: "/data/nfs *(rw,sync,no_root_squash)"
  22. state: present
  23. handlers:
  24. - name: restart nfs
  25. service:
  26. name: nfs
  27. state: restarted