1、准备环境
1.1、主机准备
nfs 192.168.10.15
client 192.168.10.16
1.2、设置免密码登陆
ssh-keygen -t rsa -C root@qq.com -f ~/.ssh/id_rsa -P ""
yum install sshpass -y
sshpass -proot ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@192.168.10.15
sshpass -proot ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@192.168.10.16
1.3、配置主机清单
]# vi /etc/ansible/hosts
[nfs]
192.168.10.15
[client]
192.168.10.16
2、编写安装配置 nfs 服务的 playbook 文件
cat << 'CAT_END' > install_nfs.yaml
- hosts: nfs
tasks:
- name: Install NFS Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
copy:
src: ./exports.j2
dest: /etc/exports
- name: Init NFS Server
file:
path:/ops_web
state: directory
owner: nfsnobody
group: nfsnobody
- name: Systemd NFS Server
systemd:
name: nfs
state: started
enabled: yes
CAT_END
3、准备 playbook 依赖的 exports.j2 文件
echo "/data 192.168.10.0/24(rw,sync)" >exports.j2
4、检查 playbook 语法
ansible-playbook install_nfs.yaml --syntax-check
5、运行任务
]# ansible-playbook install_nfs.yaml
PLAY [nfs] ***************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.10.15]
TASK [Install NFS Server] ************************************************************************************************************************************
changed: [192.168.10.15]
TASK [Configure NFS Server] **********************************************************************************************************************************
changed: [192.168.10.15]
TASK [Init NFS Server] ***************************************************************************************************************************************
changed: [192.168.10.15]
TASK [Systemd NFS Server] ************************************************************************************************************************************
changed: [192.168.10.15]
PLAY RECAP ***************************************************************************************************************************************************
192.168.10.15 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6、客户端执行命令测试
]# ansible client -m shell -a 'showmount -e 192.168.10.15'
192.168.10.16 | CHANGED | rc=0 >>
Export list for 192.168.10.15:
/data 192.168.10.0/24