ansible-playbook剧本案例6远程批量添加定时任务

案例6: 远程批量添加定时任务

管理端:192.168.171.128

[root@localhost ~]# cat /etc/ansible/hosts

[test]                    #添加一个组名

192.168.171.129           #添加被管理主机的IP

192.168.171.130           #添加被管理主机的IP

[root@localhost ~]# cat cron_add.yaml

#hosts: 指定要操作的主机清单

#task: 任务列表

#name:描述注释信息

#cron:cron模块,添加定时任务,分时日月周,不写的默认是*,下面只关添加定时任务,具体要执行,需要本地有相应的脚本才行

- hosts: test

  tasks:

    - name: Crontab Scripts chuangjian

      cron: name='dellog scripts' minute=00 hour=01 job="/bin/sh /server/scripts/delete_log.sh &>/dev/null"

[root@localhost ~]# ansible-playbook cron_add.yaml   #执行远程添加

注意:若下面是:删除定时任务:

cron: name='backup scripts' minute=00 hour=01 job="/bin/sh /server/scripts/delete_log.sh &>/dev/null" state=absent

若下面则是:注释定时任务:

cron: name='backup scripts' minute=00 hour=01 job="/bin/sh /server/scripts/delete_log.sh &>/dev/null" disabled=yes

所有被管理端:192.168.171.129和192.168.130

[root@localhost ~]# crontab -l

#Ansible: dellog scripts

00 01 * * * /bin/sh /server/scripts/delete_log.sh &>/dev/null

注意事项

1.当管理端向被管理端发送文件时候(被管理端)安装:libselinux-python,才能将管理端(ansible机器)上本地文件批量发送给被管理端,即: yum install libselinux-python -y  

2.ansible机器也可不用创建密钥对,进行无秘钥连接被管理机器,只需要在配置文件/etc/ansible/hosts中添加各个被管理机器的密码也可(不过第一次连接时需要输入yes确认,后面就不需要了)

格式例子如:

[maya]

keeper-01 ansible_ssh_host="192.168.14.128"  ansible_ssh_user="root" ansible_ssh_pass="123456"

maya-001-129 ansible_ssh_host="192.168.14.129"  ansible_ssh_user="root" ansible_ssh_pass="123456"

[mem]

mem1 ansible_ssh_host="192.168.14.130"   ansible_ssh_user="root" ansible_ssh_pass="123456"

mem2 ansible_ssh_host="192.168.14.131"   ansible_ssh_user="root" ansible_ssh_pass="123456"

3.ansible机器可以对定义的整个模块批量操作管理机器,也可对某模块中定义的某个被管理机器单独进行操作

例子如:

[root@keeper-01 ~]# vim /etc/ansible/hosts

[maya]

keeper-01 ansible_ssh_host="192.168.14.128"  ansible_ssh_user="root" ansible_ssh_pass="123456"

maya-001-129 ansible_ssh_host="192.168.14.129"  ansible_ssh_user="root" ansible_ssh_pass="123456"

[mem]

mem1 ansible_ssh_host="192.168.14.130"   ansible_ssh_user="root" ansible_ssh_pass="123456"

mem2 ansible_ssh_host="192.168.14.131"   ansible_ssh_user="root" ansible_ssh_pass="123456"

#根据上面的模块,ansible想单独给mem1机器发送文件:

[root@keeper-01 ~]# ansible mem1 -m copy -a "src=/root/jenkins.war dest=/tmp/ROOT.war"

注意上面各模块下的第一个并不一定非要是主机名,不一定就要能解析,也不用必须要在/etc/hosts文件中将该名和对应的IP对应,而只是自己模块下定义的指定被管理机器的一个别名,只是为了好区分,将该别名一般和主机名设置的一样而已。

4.ansible批量发送文件时,远端机器ssh的端口号不是22,而是已经改变了的22115时候的配置

[root@keeper-01 ~]# vim /etc/ansible/hosts

[app-girl]

app-girl1 ansible_ssh_host="172.17.133.212"  ansible_ssh_user="root" ansible_ssh_pass="b6eMWV2VQQ" ansible_ssh_port=22115

app-girl2 ansible_ssh_host="172.17.133.213"  ansible_ssh_user="root" ansible_ssh_pass="C4NMcSyBrQ" ansible_ssh_port=22115

[root@keeper-01 ~]# ansible app-girl1 -m copy -a "src=/root/a.txt dest=/tmp/" #给机器app-girl1发送文件

ansible批量管理客户端的命令总结:

1)检查主机连接

# ansible 单独模块名 -m ping                    //管理单独模块的ping,是否能通

# ansible all -m ping                           //管理所有模块的ping,是否能通

# ansible 192.168.40.134 -m ping                //管理某个机器IP的ping,是否能通

3)执行远程主机的脚本
# ansible 单独模块名 -m shell -a 'sh shell脚本名   或 python  python脚本名' 

//管理单独模块下机器,执行远程机器脚本

# ansible all -m shell -a 'sh shell脚本名   或 python  python脚本名' 

//管理所有模块下机器,执行远程机器脚本

4)复制文件到远程服务器:
# ansible 单独模块名 -m copy -a "src=/路径/…ansible机器文件名  dest=/路径/…远端机器文件名"

                                                 //管理单独模块下机器,将复制文件到远端机器

# ansible  all -m copy -a "src=/路径/…ansible机器文件名  dest=/路径/…远端文件名"

                                                 //管理所有模块下机器,将复制文件到远端机器

注意:所有被管理端需要安装:libselinux-python即:yum -y install libselinux-python

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

运维实战课程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值