您的位置:首页 > 其它

实例学习ansible系列(11)常用模块之get_url/cron/synchronize

2016-07-31 07:48 1311 查看
知识点:cron模块用于管理对象节点cron任务

知识点: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 cron get-url