案例5: 远程批量安装nfs服务,并设置管理端修改配置文件变动时候执行playbook时触发重启服务
管理端:192.168.171.128
[root@localhost ~]# cat /etc/ansible/hosts
[test] #添加一个组名
192.168.171.129 #添加被管理主机的IP
192.168.171.130 #添加被管理主机的IP
[root@localhost ~]# ls
conf nfs_install.yaml
[root@localhost ~]# ls conf/
exports
[root@localhost ~]# cat /etc/exports
/data *(rw,no_root_squash)
[root@localhost ~]# cat nfs_install.yaml
#hosts: 指定要操作的主机清单
- hosts: test
tasks:
#远端安装nfs
- name: Install nfs server
yum: name=nfs-utils state=installed
#配置nfs,自定义配置文件传递到远端,并修改配置后触发重启服务动作
- name: configure nfs server
copy: src=./conf/exports dest=/etc/exports
notify: Restart Nfs Server
#远程递归创建共享目录
- name: create share data directory
file: path=/data state=directory recurse=yes owner=root group=root mode=755
#远程启动nfs
- name: start nfs server
service: name=nfs-server state=started enabled=yes
handlers:
- name: Restart Nfs Server
service: name=nfs-server state=restarted
[root@localhost ~]# ansible-playbook nfs_install.yaml #执行远程安装
[root@localhost ~]# yum -y install nfs-utils #安装客户端,查看挂载使用
[root@localhost ~]# showmount -e 192.168.171.129
Export list for 192.168.171.129:
/data *
[root@localhost ~]# showmount -e 192.168.171.130
Export list for 192.168.171.130:
/data *
所有被管理端:192.168.171.129和192.168.171.130
[root@localhost ~]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Drop-In: /run/systemd/generator/nfs-server.service.d
└─order-with-mounts.conf
Active: active (exited) since Fri 2019-10-04 17:39:08 CST; 14s ago