欢迎来到代码驿站!

Shell

当前位置:首页 > 脚本语言 > Shell

ansible执行shell脚本的方法

时间:2021-11-17 13:17:45|栏目:Shell|点击:

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$ 

上一篇:Shell脚本处理浮点数的运算和比较实例

栏    目:Shell

下一篇:Shell脚本自动删除指定大小以上的备份文件

本文标题:ansible执行shell脚本的方法

本文地址:http://www.codeinn.net/misctech/183836.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有