天天看点

ansible 剧本部署配置nfs

### ansible 剧本部署配置nfs

--目标

--配置nfs服务端-backup,共享目录/playbook-backup/ 共享192.168.200.0/24 all_squash访问

```sh

01 书写剧本流程

1)把对应的过程翻译程命令

 a)安装nfs

 yum install -y nfs-utils

 b)配置nfs服务端

 /playbook-backup/ 192.168.200.0/24(rw,sync,all_squash)

 mkdir -p /playbook-backup

 chown -R nfsnobody.nfsnobody /playbook-backup

 c)启动与开机自启动

 # systemctl enabled nfs rpcbind

 # systemctl start rpcbind nfs

 d)本地的挂载

 mount -t nfs 192.168.200.3:/playbook-backup /mnt

2)找出命令对应模块

 a) yum  

 -m yum -a 'name=nfs-utils state=present'

 b) copy

 -m copy -a 'dest=/etc/exports /context="/playbook-backup/ 192.168.200.0/24(rw,sync,all_squash)"'

 c) file

 -m file -a 'path=/playbook-backup state=directory owner=nfsnobody group=nfsnobody '

 d) systemd

 -m systemd -a 'name=rpcbind enabled=yes state=started'

 -m systemd -a 'name=nfs enabled=yes state=started'

 e) mount

 -m mount -a 'fstype=nfs src=192.168.200.3:/playbook-backup path=/mnt state=mounted'

3)根据模块书写剧本

4)测试剧本

5)正式使用

```

[root@web01 playbook]# ansible all -a 'df -h'

192.168.200.3 | CHANGED | rc=0 >>

Filesystem                      Size  Used Avail Use% Mounted on

/dev/mapper/centos-root          17G  1.2G   16G   8% /

devtmpfs                        899M     0  899M   0% /dev

tmpfs                           911M     0  911M   0% /dev/shm

tmpfs                           911M  9.5M  902M   2% /run

tmpfs                           911M     0  911M   0% /sys/fs/cgroup

/dev/sda1                      1014M  142M  873M  14% /boot

tmpfs                           183M     0  183M   0% /run/user/0

192.168.200.3:/playbook-backup   17G  1.2G   16G   8% /mnt

[root@web01 playbook]# ansible all -a 'cat /etc/exports'

/playbook-backup/ 192.168.200.0/24(rw,sync,all_squash)

```yaml

# cat 02nfs.yaml  

- hosts: all

 tasks:  

   - name: install nfs rpc

     yum: name=nfs-utils state=installed

   - name: config

     copy: dest=/etc/exports content="/playbook-backup/ 192.168.200.0/24(rw,sync,all_squash)"

   - name: mkdir chown

     file: path=/playbook-backup state=directory owner=nfsnobody group=nfsnobody

   - name: start && enable1

     systemd: name=rpcbind enabled=yes state=started

   - name:  

     systemd: name=nfs enabled=yes state=started

   - name: mount  

     mount: fstype=nfs src=192.168.200.3:/playbook-backup path=/mnt state=mounted