• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ansible执行shell脚本的方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1. 编写脚本:pb_shell.sh

wfq@ubuntu:~/playbook$ cat pb_shell.sh 
#!bin/sh
echo "today(`date`) is a nice day, since I do some nice demo about ansible and excute very well." > /home/wfq/playbook/pb.log
wfq@ubuntu:~/playbook$

2. 执行ansible命令,将脚本copy到其他几台服务器上

wfq@ubuntu:~/playbook$ ansible asb -m copy -a "src=/home/wfq/playbook/pb_shell.sh dest=/home/wfq/playbook/"
192.168.174.134 | success >> {
    "changed": true, 
    "dest": "/home/wfq/playbook/pb_shell.sh", 
    "gid": 1000, 
    "group": "wfq", 
    "md5sum": "79744ccf5b60c7d020bb261d11516252", 
    "mode": "0664", 
    "owner": "wfq", 
    "size": 135, 
    "src": "/home/wfq/.ansible/tmp/ansible-tmp-1523198762.69-211691352620479/source", 
    "state": "file", 
    "uid": 1000
}

192.168.174.131 | success >> {
    "changed": true, 
    "dest": "/home/wfq/playbook/pb_shell.sh", 
    "gid": 1000, 
    "group": "wfq", 
    "md5sum": "79744ccf5b60c7d020bb261d11516252", 
    "mode": "0664", 
    "owner": "wfq", 
    "size": 135, 
    "src": "/home/wfq/.ansible/tmp/ansible-tmp-1523198762.69-274155692570175/source", 
    "state": "file", 
    "uid": 1000
}

192.168.174.133 | success >> {
    "changed": true, 
    "dest": "/home/wfq/playbook/pb_shell.sh", 
    "gid": 1000, 
    "group": "wfq", 
    "md5sum": "79744ccf5b60c7d020bb261d11516252", 
    "mode": "0664", 
    "owner": "wfq", 
    "size": 135, 
    "src": "/home/wfq/.ansible/tmp/ansible-tmp-1523198764.85-163098225256987/source", 
    "state": "file", 
    "uid": 1000
}

192.168.174.132 | success >> {
    "changed": true, 
    "dest": "/home/wfq/playbook/pb_shell.sh", 
    "gid": 1000, 
    "group": "wfq", 
    "md5sum": "79744ccf5b60c7d020bb261d11516252", 
    "mode": "0664", 
    "owner": "wfq", 
    "size": 135, 
    "src": "/home/wfq/.ansible/tmp/ansible-tmp-1523198764.89-251052794120091/source", 
    "state": "file", 
    "uid": 1000
}

wfq@ubuntu:~/playbook$ 

3. 执行ansible命令,在每天服务器上执行pb_shell.sh脚本

wfq@ubuntu:~/playbook$ ansible asb -m shell  -a "sh /home/wfq/playbook/pb_shell.sh chdir=/home/wfq/playbook"
192.168.174.134 | success | rc=0 >>
192.168.174.131 | success | rc=0 >>
192.168.174.132 | success | rc=0 >>
192.168.174.133 | success | rc=0 >>

4. 比如,在131服务器,查看执行生成的日志:

wfq@ubuntu:~/playbook$ cat pb.log 
today(2018年 04月 08日 星期日 22:46:19 CST) is a nice day, since I do some nice demo about ansible and excute very well.
wfq@ubuntu:~/playbook$ 
wfq@ubuntu:~/playbook$ ls
pb.log  pb_shell.sh
wfq@ubuntu:~/playbook$ 

到此这篇关于ansible执行shell脚本的方法的文章就介绍到这了,更多相关ansible执行shell脚本内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Shell获取当前正在执行脚本的绝对路径发布时间:2022-02-11
下一篇:
解析spark源码yarn-cluster模式任务提交发布时间:2022-02-11
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap