news 2026/3/28 7:26:20

ansible部署nfs

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ansible部署nfs

文章目录

    • 实验环境
    • 安装ansible
    • 部署nfs网络文件系统
      • 1、创建系统用户和组
      • 2、在nfs上创建共享目录
      • 3、编辑nfs配置文件
      • 4、开启nfs服务
      • 5、进行挂载

实验环境

主机IP【配置静态IP地址】主机名字(身份)
10.0.0.61m01(管理节点)
10.0.0.31nfs(网络文件系统)

安装ansible

安装ansible,只需要在主控节点进行安装ansible服务,配置ssh公私钥

#使用yum安装ansible和依赖开发软件包,前提需要配置好阿里云的yum和epel源[root@m01 yum.repos.d]# lsCentOS-Base.repo epel.repo nginx.repo[root@m01 yum.repos.d]# yum repolistLoaded plugins: fastestmirror, langpacks Determining fastest mirrors repoidrepo name status!base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com10,072!epel/x86_64 Extra PackagesforEnterprise Linux7- x86_6413,791!extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com526!nginx-stable/7/x86_64 nginx stable repo364!updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com6,173repolist:30,926#安装ansible和依赖yum -yinstallansible libselinux-python#配置主机文件,这里我同步配置了/etc/hosts文件,做了主机名和主机ip映射,这样在配置ansible主机文件的时候可以使用主机名字,没有的话就直接配置主机IP地址#注意ansible都是使用ssh进行登入的,需要保障管理节点和被管理的节点可以ssh登入[root@m01 ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.0.0.5 lb0110.0.0.6 lb0210.0.0.7 web0110.0.0.8 web0210.0.0.9 web0310.0.0.31 nfs10.0.0.41 backup10.0.0.51 db0110.0.0.61 m0110.0.0.71 zabbix#测试主机映射是否成功[root@m01 ~]# ping nfsPING nfs(10.0.0.31)56(84)bytes of data.64bytes from nfs(10.0.0.31):icmp_seq=1ttl=64time=0.179ms64bytes from nfs(10.0.0.31):icmp_seq=2ttl=64time=0.231ms64bytes from nfs(10.0.0.31):icmp_seq=3ttl=64time=0.504ms ^C --- nfspingstatistics ---3packets transmitted,3received,0% packet loss,time2000ms rtt min/avg/max/mdev=0.179/0.304/0.504/0.143 ms#配置ansible的主机文件[root@m01 ~]# tail -2 /etc/ansible/hosts[test]nfs 在test组里面,有主机nfs#配置ssh的公私钥#生成公私秘钥,-t 加密类型 -C描述信息[root@m01 ~]# ssh-keygen -t rsa -C A-Server.comGenerating public/private rsa key pair. Enterfileinwhichto save the key(/root/.ssh/id_rsa): Created directory'/root/.ssh'.Enter passphrase(emptyforno passphrase): Enter same passphrase again: Your identification has been savedin/root/.ssh/id_rsa. Your public key has been savedin/root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:xO2DoVA16c+29mx39r1hbG90M4dEPXJszdW8tBWB/Fo A-Server.com The key's randomart image is: +---[RSA 2048]----+ | ..o. . o*B| | . ..o +.*B| | . .+ . .* =| | . o.+ .E | | . Soo .o. | | +. .oo+| | . . *=| | o.. + B| | . oo. =*| +----[SHA256]-----+ #分发公钥到被管理节点,输入被管理节点的root密码 [root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub "root@10.0.0.31" /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.0.31's password: Number of key(s)added:1Now try logging into the machine, with:"ssh 'root@10.0.0.31'"and check tomakesure that only the key(s)you wanted were added.#检查管理节点的公私钥信息[root@m01 ~]# cd /etc/ssh/[root@m01 ssh]# lsmoduli ssh_host_ecdsa_key ssh_host_ed25519_key.pub ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key sshd_config ssh_host_ed25519_key ssh_host_rsa_key.pub

部署nfs网络文件系统

1、创建系统用户和组

[root@m01~]# ansible nfs -m group -a 'name=www gid=666 state=present'nfs|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"gid":666,"name":"www","state":"present","system":false}[root@m01~]# ansible nfs -m user -a 'name=www uid=666 group=666 create_home=no shell=/sbin/nologin state=present'nfs|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"comment":"","create_home":false,"group":666,"home":"/home/www","name":"www","shell":"/sbin/nologin","state":"present","system":false,"uid":666}

2、在nfs上创建共享目录

[root@m01~]# ansible nfs -m file -a 'path=/data state=directory owner=666 group=666 recurse=yes'nfs|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"gid":666,"group":"www","mode":"0755","owner":"www","path":"/data","size":6,"state":"directory","uid":666}

3、编辑nfs配置文件

[root@m01~]# ansible nfs -m copy -a 'dest=/etc/exports content="/data 10.0.0.0/24(rw,all_squash,anonuid=666,anongid=666)" mode=600'nfs|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"checksum":"9595deab6719ad80dcef8a71f50a317dbfba5235","dest":"/etc/exports","gid":0,"group":"root","md5sum":"7c78689c50af25261f54f251b34c7e26","mode":"0600","owner":"root","size":56,"src":"/root/.ansible/tmp/ansible-tmp-1699072334.11-1365-90513311577736/source","state":"file","uid":0}

4、开启nfs服务

[root@m01~]# ansible nfs -m systemd -a 'name=nfs state=started enabled=yes'nfs|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"enabled":true,

5、进行挂载

[root@m01~]# ansible web -m mount -a 'src=10.0.0.31:/data path=/web fstype=nfs opts=defaults state=mounted'web01|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"dump":"0","fstab":"/etc/fstab","fstype":"nfs","name":"/web","opts":"defaults","passno":"0","src":"10.0.0.31:/data"}web03|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"dump":"0","fstab":"/etc/fstab","fstype":"nfs","name":"/web","opts":"defaults","passno":"0","src":"10.0.0.31:/data"}web02|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"dump":"0","fstab":"/etc/fstab","fstype":"nfs","name":"/web","opts":"defaults","passno":"0","src":"10.0.0.31:/data"}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/25 12:30:46

30、Ubuntu不同版本及Wine使用全解析

Ubuntu不同版本及Wine使用全解析 1. Ubuntu Netbook Edition Ubuntu Netbook Edition(在Ubuntu 10.04之前的版本中称为Netbook Remix)是专门为上网本电脑重新设计的版本。上网本通常处理能力较弱、屏幕较小,如果你觉得当前操作系统运行缓慢且响应不佳,那么这个版本可能很适…

作者头像 李华
网站建设 2026/3/25 8:58:34

31、Ubuntu 使用指南与技巧全解析

Ubuntu 使用指南与技巧全解析 1. 访问模拟的 Windows C 盘 在 Ubuntu 中,若要使用终端切换到模拟的 c:\Program Files 文件夹,可输入以下命令: cd ~/.wine/dosdevices/c:/"Program Files"需注意,对于包含非字母数字字符的名称,要使用引号括起来。另外,也…

作者头像 李华
网站建设 2026/3/24 15:25:55

Helm:K8s应用部署的终极利器

一、Helm概述 helm通过打包的方式,支持发布的版本管理和控制,很大程度上简化了Kubernetes应用的部署和管理。 Helm本质就是让k8s的应用管理(Deployment、Service等)可配置,能动态生成。通过动态生成K8S资源清单文&am…

作者头像 李华
网站建设 2026/3/25 1:01:45

黑马点评前125节课遇到的问题及解决方案(在看网课过程中会有很多老师运行成功但我们失败并且老师还不没有讲到的情况,本文致力于解决这个问题,记录了本人在做这个项目的时候遇到的所有问题)

写在前面的话 在看网课过程中会有很多老师运行成功但我们失败并且老师还不没有讲到的情况,本文致力于解决这个问题,记录了本人在做这个项目的时候遇到的所有问题,希望可以帮助到大家。、 可以直接在根据目录或者导航栏找到自己的遇到问题的…

作者头像 李华
网站建设 2026/3/22 8:24:38

32、深入掌握 Bash 条件测试与流程控制

深入掌握 Bash 条件测试与流程控制 1. 使用 test 命令 在 Bash 脚本中,与 if 语句搭配最频繁使用的命令当属 test 。 test 命令可进行各类检查与比较,它有两种等价形式: - test expression - [ expression ] 其中, expression 是一个可被评估为真或假的表…

作者头像 李华
网站建设 2026/3/27 17:41:14

YashanDB数据库的核心优势与应用场景分析

YashanDB是一种新兴的数据库解决方案,具有多种核心优势和应用场景。以下是对其核心优势及应用场景的分析:核心优势1. 高性能- YashanDB采用高效的数据存储和检索机制,能够在处理大规模数据时保持优异的性能,适合对实时性要求较高的…

作者头像 李华