一、crond 定时任务模块
minute=* 分
hour=* 时
day=* 日
month=* 月
weekday=* 周
job 要执行的动作
回顾一下定时任务的书写格式:
1.检查是否安装ntpdate
[09:32 root@m01 ~]# ansible all -a 'rpm -qa ntpdate'
2.每5分钟同步一次时间:
[09:34 root@m01 ~]# ansible all -m cron -a 'name="sync time" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
3.再添加一个定时任务
[09:55 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
4.把此定时任务删除:
[09:55 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" state=absent'
查看一下
[09:56 root@m01 ~]# ansible all -a 'crontab -l'
5.再添加一个定时任务
[09:57 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
6.给定时任务添加注释:
[10:00 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=yes'
查看一下
[10:00 root@m01 ~]# ansible all -a 'crontab -l'
172.16.1.31 | CHANGED | rc=0 >>
#Ansible: guoav-date
#*/30 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
7.取消注释:
[10:02 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=no'
查看一下
[10:02 root@m01 ~]# ansible all -a 'crontab -l'
172.16.1.7 | CHANGED | rc=0 >>
#Ansible: guoav-date
*/30 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
远程启动/关闭定时任务服务
二、mount 挂载模块
1.首先保证nfs01客户端的nfs服务开启
[10:12 root@nfs01 ~]# systemctl is-active rpcbind
active
[10:13 root@nfs01 ~]# systemctl is-active nfs
active
2.将/nfs共享目录挂载到web01服务端上
[10:09 root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs 172.16.1.0/24
/upload 172.16.1.0/24
[10:09 root@web01 ~]# mount -t nfs 172.16.1.31:/nfs/ /mnt/
[10:09 root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.7G 18G 9% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/nfs 19G 1.7G 18G 9% /mnt
测试完把/mnt卸载掉:
umount /mnt
3.用ansible将/nfs挂载到web01上的/upload
1.查看挂载信息
2.挂载 state=mounted
3.查看
[10:27 root@m01 ~]# ansible 172.16.1.7 -a 'showmount -e 172.16.1.31'
172.16.1.7 | CHANGED | rc=0 >>
Export list for 172.16.1.31:
/nfs 172.16.1.0/24
/upload 172.16.1.0/24
[10:26 root@m01 ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=mounted'
172.16.1.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/upload",
"opts": "defaults",
"passno": "0",
"src": "172.16.1.31:/nfs"
}
[10:26 root@m01 ~]# ansible 172.16.1.7 -a 'df -h'
172.16.1.7 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
....
172.16.1.31:/nfs 19G 1.7G 18G 9% /upload
4.不挂载,只添加到fstab文件
state=present
[10:46 root@m01 ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/date path=/tmp state=present'
5.卸载,会删除/etc/fstab
state=absent
[11:04 root@m01 ~]# ansible 172.16.1.7 -m mount -a ' src=172.16.1.31:/nfs path=/upload state=absent'
三、查看命令的返回值(命令执行是否成功)
echo $?
查看命令的返回值 (命令执行是否成功)
返回0 正确 非0 错误
[11:21 root@m01 ~]# ls
10.0.0.61 anaconda-ks.cfg dead.letter hosts.txt put
[11:21 root@m01 ~]# echo $?
0 \\命令执行成功
[11:21 root@m01 ~]# touch -p aaa.txt
touch: invalid option -- 'p'
Try 'touch --help' for more information.
[11:21 root@m01 ~]# echo $?
1 \\命令执行失败
※四、palybook剧本 ※
不会先看图:
playbook的优势:
1.playbook剧本初体验:
1.书写剧本 |
---|
2.检查剧本 |
3.执行剧本 |
1.创建一个递归目录 名称注释为guoav
[1:43 root@m01 /etc/ansible]# vim touch.yml
▽
---
- hosts: all
tasks:
- name: make guoav
command: mkdir -p /tmp/a/b/c/d/e/f/g/
2.命令示范:
3.playbook检查或执行的显示过程我们可以自己设置:
可以安装cowsay(奶牛说) yum install -y cowsay
也可以改为直观的显示过程:如下取消注释
vim /etc/ansible/ansible.cfg
检查剧本:
ansible-playbook -C touch.yml
[11:49 root@m01 /etc/ansible]# ansible-playbook -C touch.yml
PLAY [all] ***********************************************************************
TASK [Gathering Facts] ***********************************************************
ok: [172.16.1.31]
ok: [172.16.1.7]
ok: [172.16.1.41]
TASK [make guoav] ****************************************************************
skipping: [172.16.1.41]
skipping: [172.16.1.31]
skipping: [172.16.1.7]
PLAY RECAP ***********************************************************************
172.16.1.31 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.16.1.41 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.16.1.7 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
3.执行剧本
ansible-playbook touch.yml
[11:45 root@m01 /etc/ansible]# ansible-playbook touch.yml
PLAY [all] ******************************************************************************************
TASK [Gathering Facts] ******************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.7]
ok: [172.16.1.41]
TASK [make guoav] ***********************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you
need to use command because file is insufficient you can add 'warn: false' to this command task or
set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [172.16.1.7]
changed: [172.16.1.31]
changed: [172.16.1.41]
PLAY RECAP ******************************************************************************************
172.16.1.31 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
※五、playbook添加定时任务※
我们之前用ansible的cron模块添加定时任务的格式是这样的:
ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=yes'
如何用剧本写出来呢:
第一种:
[12:07 root@m01 /etc/ansible]# cat cron.yml
---
- hosts: all
tasks:
- name: sync time
cron: name="playbook" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" state=present
执行剧本并查看定时任务
[12:07 root@m01 /etc/ansible]# #ansible-playbook cron.yml
[12:07 root@m01 /etc/ansible]#
[12:07 root@m01 /etc/ansible]# ansible all -a 'crontab -l'
172.16.1.41 | CHANGED | rc=0 >>
#Ansible: playbook
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
172.16.1.31 | CHANGED | rc=0 >>
#Ansible: playbook
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
172.16.1.7 | CHANGED | rc=0 >>
#Ansible: playbook
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
第二种:
脚本可以再改造一下,变成每一行的格式:
[2:08 root@m01 /etc/ansible]# vim cron.yml
---
- hosts: all
tasks:
- name: sync time
cron:
name: playbook v2
minute: */5
job: /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
state: present
※六、playbook部署nfs服务 ※
nfs服务的一键脚本格式:
#!/bin/bash
#one key install nfs
#package install?
yum install -y rpcbind nfs-utils
#configure
cp /etc/exports{,.bak}
cat >/etc/exports<<EOF
#nfs01
/nfs 172.16.1.0/24(rw,all_squash)
EOF
#dir owner
mkdir -p /nfs
chown nfsnobody.nfsnobody /nfs
#start rpcbind nfs
systemctl start rpcbind nfs
systemctl enable rpcbind nfs
nfs01服务端: