ansible部署nfs
@
前言
使用虚拟机创建2台系统为centos7.9 的云主机,其中一台作为 ansible 的母机并命名为 ansible,另外一台云主机命名为 node1,用这台母机,编写 ansible 脚本
主机 | IP |
---|---|
ansible | 192.168.200.50 |
node1 | 192.168.200.51 |
配置基础环境
[root@localhost ~]# hostnamectl set-hostname ansible
[root@localhost ~]# bash
[root@ansible ~]#
[root@localhost ~]# hostnamectl set-hostname node1
[root@localhost ~]# bash
[root@node1 ~]#
[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# setenforce 0
[root@ansible ~]#
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# setenforce 0
[root@node1 ~]#
[root@ansible ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.50 ansible
192.168.200.51 node1
[root@ansible ~]#
[root@node1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.50 ansible
192.168.200.51 node1
[root@node1 ~]#
[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id root@node1
配置ansible和centos的仓库
[root@ansible ~]# ls
anaconda-ks.cfg ansible.tar.gz
[root@ansible ~]# tar -zxvf ansible.tar.gz -C /opt/
[root@ansible ~]# mkdir /opt/centos
[root@ansible ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@ansible ~]# cp -rfv /mnt/* /opt/centos/
[root@ansible ~]# umount /mnt/
[root@ansible ~]# mv /etc/yum.repos.d/* /mnt/
[root@ansible ~]# vi /etc/yum.repos.d/ftp.repo
[root@ansible ~]# cat /etc/yum.repos.d/ftp.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[ansible]
name=ansible
baseurl=file:///opt/ansible
gpgcheck=0
enabled=1
[root@ansible ~]#
配置ansible文件
[root@ansible ~]# yum install -y vsftpd ansible
[root@ansible ~]# echo "anon_root=/opt" >> /etc/vsftpd/vsftpd.conf
[root@ansible ~]# systemctl restart vsftpd
[root@ansible ~]# cat >> /etc/ansible/hosts <<eof
[node1]
192.168.200.51
eof
[root@ansible ~]# mkdir ansible_nfs
[root@ansible ~]# cd ansible_nfs/
[root@ansible ansible_nfs]# cat local.repo
[centos]
name=centos
baseurl=ftp://ansible/centos
gpgcheck=0
enabled=1
[ansible]
name=ansible
baseurl=ftp://ansible/ansible
gpgcheck=0
enabled=1
[root@ansible ansible_nfs]#
[root@ansible ansible_nfs]# cat install_nfs.yaml
---
- name: Install and configure NFS server
hosts: node1
become: true
vars:
nfs_export_dir: "data"
tasks:
- name: Copy local.repo file to node1
copy:
src: "/root/ansible_nfs/local.repo"
dest: "/etc/yum.repos.d/local.repo"
become: true
- name: Install NFS
yum:
name: nfs-utils
state: latest
become: true
- name: Configure NFS exports
template:
src: "/root/ansible_nfs/exports.j2"
dest: "/etc/exports"
become: true
- name: Create NFS share directory
file:
path: "{{ nfs_export_dir }}"
state: directory
mode: "0777"
become: true
- name: exportfs
shell: exportfs -r
- name: Start NFS service
service:
name: nfs
state: started
enabled: true
become: true
[root@ansible ansible_nfs]#
[root@ansible ansible_nfs]# cat exports.j2
data 192.168.200.0/24(rw,all_squash)
[root@ansible ansible_nfs]#
执行文件
[root@ansible ansible_nfs]# ansible-playbook install_nfs.yaml
验证
[root@node1 ~]# cat /etc/exports
data 192.168.200.0/24(rw,all_squash)
[root@node1 ~]# ls
anaconda-ks.cfg data
[root@node1 ~]# showmount -e 192.168.200.51
Export list for 192.168.200.51:
/root/data 192.168.200.0/24
[root@node1 ~]#