实例学习ansible系列(11)常用模块之get_url/cron/synchronize
2016-07-31 07:48
1311 查看
知识点:cron模块用于管理对象节点cron任务
知识点:get_url模块类似于wget和curl的功能,可以进行下载以及webapi交互等操作
知识点:synchronize模块使用rsync用于控制节点和管理对象节点之间的内容同步操作。
利用cron模块向对象节点添加一个叫做sayhellojob的一个无聊job,此job每2分钟说一次hello
事后对象节点的crontab内容
事后输出log的确认
下载确认
将此目录结构完整同步到对象机器的/opt下
同步后确认
知识点:get_url模块类似于wget和curl的功能,可以进行下载以及webapi交互等操作
知识点:synchronize模块使用rsync用于控制节点和管理对象节点之间的内容同步操作。
cron模块使用实例
事前对象节点cron信息确认[root@host31 ~]# ansible host32 -m shell -a "crontab -l" host32 | SUCCESS | rc=0 >> 10 * * * * echo `date` >> /tmp/cronlog.log [root@host31 ~]#
利用cron模块向对象节点添加一个叫做sayhellojob的一个无聊job,此job每2分钟说一次hello
[root@host31 ~]# ansible host32 -m cron -a 'name=sayhellojob minute=*/2 hour=* day=* month=* weekday=* job="echo hello `date` >> /tmp/cronlog.log"' host32 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "sayhellojob" ] } [root@host31 ~]#
事后对象节点的crontab内容
[root@host31 ~]# ansible host32 -m shell -a "crontab -l" host32 | SUCCESS | rc=0 >> 10 * * * * echo `date` >> /tmp/cronlog.log #Ansible: sayhellojob ->jobname是作为注释管理的 */2 * * * * echo hello `date` >> /tmp/cronlog.log [root@host31 ~]#
事后输出log的确认
[root@host31 ~]# ansible host32 -m shell -a "cat /tmp/cronlog.log" host32 | SUCCESS | rc=0 >> hello Sat Jul 30 19:06:01 EDT 2016 hello Sat Jul 30 19:08:01 EDT 2016 Sat Jul 30 19:10:01 EDT 2016 hello Sat Jul 30 19:10:01 EDT 2016 hello Sat Jul 30 19:12:02 EDT 2016 hello Sat Jul 30 19:14:01 EDT 2016 hello Sat Jul 30 19:16:01 EDT 2016 hello Sat Jul 30 19:18:01 EDT 2016 hello Sat Jul 30 19:20:01 EDT 2016 [root@host31 ~]#
get_url使用实例
使用get_url下载baidu的index[root@host31 ~]# ansible host31 -m get_url -a "url=http://www.baidu.com dest=/tmp" host31 | SUCCESS => { "changed": true, "checksum_dest": null, "checksum_src": "2fccd8d95deeee8058575bd727057f56bde82875", "dest": "/tmp/index.html", "gid": 0, "group": "root", "md5sum": "903a4edc8fef9db948f2fb5533a66f6c", "mode": "0644", "msg": "OK (unknown bytes)", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 99451, "src": "/tmp/tmp84dCzW", "state": "file", "uid": 0, "url": "http://www.baidu.com" } [root@host31 ~]#
下载确认
[root@host31 ~]# ll /tmp/index.html -rw-r--r--. 1 root root 99451 Jul 30 19:35 /tmp/index.html [root@host31 ~]#
synchronize使用实例
同步内容准备和确认[root@host31 ~]# mkdir -p /tmp/tst-syn /tmp/tst-syn/src /tmp/tst-syn/target /tmp/tst-syn/target/bin [root@host31 ~]# echo "hello" > /tmp/tst-syn/target/bin/hello [root@host31 ~]# [root@host31 ~]# ssh host32 ls -l /opt total 0 drwxr-xr-x. 2 root root 6 Mar 26 2015 rh [root@host31 ~]#
将此目录结构完整同步到对象机器的/opt下
[root@host31 ~]# ansible host32 -m synchronize -a "src=/tmp/tst-syn dest=/opt/dst-syn" host32 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/tmp/tst-syn\" \"host32:/opt/dst-syn\"", "msg": "cd+++++++++ tst-syn/\ncd+++++++++ tst-syn/src/\ncd+++++++++ tst-syn/target/\ncd+++++++++ tst-syn/target/bin/\n<f+++++++++ tst-syn/target/bin/hello\n", "rc": 0, "stdout_lines": [ "cd+++++++++ tst-syn/", "cd+++++++++ tst-syn/src/", "cd+++++++++ tst-syn/target/", "cd+++++++++ tst-syn/target/bin/", "<f+++++++++ tst-syn/target/bin/hello" ] } [root@host31 ~]#
同步后确认
[root@host31 ~]# ssh host32 ls -l /opt total 0 drwxr-xr-x. 3 root root 20 Jul 30 19:44 dst-syn drwxr-xr-x. 2 root root 6 Mar 26 2015 rh [root@host31 ~]# ssh host32 find /opt/dst-syn -type f /opt/dst-syn/tst-syn/target/bin/hello [root@host31 ~]# ssh host32 find /opt/dst-syn -type d /opt/dst-syn /opt/dst-syn/tst-syn /opt/dst-syn/tst-syn/src /opt/dst-syn/tst-syn/target /opt/dst-syn/tst-syn/target/bin [root@host31 ~]#
相关文章推荐
- 基于 ANSIBLE 自动化运维实践
- PHP cron中的批处理
- php版本的cron定时任务执行器使用实例
- 集群运维自动化工具ansible之使用playbook安装zabbix客户端
- linux 自动化运维工具ansible的使用详细教程
- 让Linux下的cron以秒为单位执行shell脚本的3种方法
- Linux使用Cron+AT实现在某个确定的时间段内随机执行命令
- Python利用ansible分发处理任务
- linux下用cron定时执行任务的方法
- Cron表达式
- 如何让django方法自动地定期执行
- Puppet,Chef,Ansible的共性
- Ansible中Playbook方法的标准输出定义
- Ansible之playbook自动完成硬盘分区,格式化,挂盘,并远程创建用户
- 关于ansible2.0版本的ansible-playbook方法的调用
- Ansible
- 自动化运维学习--python
- Linux/UNIX 定时任务 cron 详解
- ansible常用模块用法