多台云主机实例批量挂载同一文件系统 Run Ansible playbook if [ f inventory ] && [ f mountnfs.yml ]; then ansibleplaybook i inventory mountnfs.yml else echo "One or both files are missing." exit 1 fi 6. 创建ansible脚本执行的yml文件:mountnfs.yml。 1)创建yml文件,并打开: plaintext touch mountnfs.yml plaintext vi mountnfs.yml 2)复制下述内容到yml文件中,保存退出 (:wq): 说明 可根据实际需求修改mountnfs.yml末尾处tasks中挂载命令的参数,参数说明请参考挂载NFS文件系统到弹性云主机 (Linux)。 plaintext name: Test Connectivity hosts: all gatherfacts: false tasks: name: "Check whether node is reachable" action: ping ignoreerrors: false tags: sfsping name: Install NFS Client hosts: all gatherfacts: yes tasks: name: Install NFS Client on CentOS yum: name: nfsutils state: present when: ansibledistribution 'CentOS' ignoreerrors: yes name: Install NFS Client on Ubuntu apt: name: nfscommon state: present when: ansibledistribution 'Ubuntu' ignoreerrors: yes name: Creat local path hosts: all gatherfacts: false tasks: name: "Create local mount path" file: path: "{{localpath}}" state: directory name: Check if localpath is mounted hosts: targethost gatherfacts: yes tasks: name: Check if localpath is mounted command: "mountpoint {{ localpath }}" ignoreerrors: yes register: mountpointcheck failedwhen: false name: Fail if localpath is mounted on any host assert: that: "not mountpointcheck.rc 0" successmsg: "localpath is not mounted on any host." failmsg: "localpath is mounted on at least one host." name: Mount NFS Filesystems hosts: targethost gatherfacts: yes tasks: name: Mount NFS Share mount: src: "{{sharepath}}" path: "{{localpath}}" fstype: nfs opts: "vers3,prototcp,async,nolock,noatime,nodiratime,wsize1048576,rsize1048576,timeo600" state: mounted become: yes 7. 执行批量挂载脚本。执行时注意不同操作系统执行命令不同。 CentOS操作系统 plaintext sh autobatchmount.sh Ubuntu操作系统 plaintext bash autobatchmount.sh 执行成功结果可参考下图。 也可执行 df h命令,查看当前云主机是否已经当前挂载文件系统,参考下图。