ansible-playbook剧本案例5

案例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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维实战课程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值