ansible在远程执行本地脚本

<span style="font-family: Arial, Helvetica, sans-serif;">#!/usr/bin/python</span>
import ansible.runner
import sys,os

#print len(sys.argv)
if len(sys.argv) != 3:
        print "usage: ansible-script <host-pattern> [scriptname and parms]"
        exit(1)
group_name=sys.argv[1]
script_name=sys.argv[2]

if not os.path.exists(script_name.split()[0]):
        print "%s:no such file" % (script_name.split()[0])
        exit(1)
results = ansible.runner.Runner(
pattern=group_name, forks=5,
module_name='script', module_args=script_name,
).run()

exit_code=0

#print results
#print results['contacted'].items()
if not results['contacted'].items() :
        print "No hosts found"
        sys.exit(1)
print "RESULTS ***********"
for (hostname, result) in results['contacted'].items():
#       print result
#       print "%s >>> %s" % (hostname, result['stdout'])
        if result['rc'] != 0:
                exit_code = result['rc']
                status =  "failed"
                print '\033[1;31;40m' #change frontcolor to red
                print "%s >>> %s \n%s" % (hostname,status,result['stdout'])
        else:
                status =  "success"
                print '\033[1;32;40m' #change frontcolor to green
                print "%s >>> %s \n%s" % (hostname,status,result['stdout'])
print '\033[0m'  #change frontcolor to default
sys.exit(exit_code)


使用方法:比如保存文件为ansible-script,运行命令为

 ansible-script <host-pattern> [scriptname and parms]

exp:  ansible-script testgroup test.sh



阅读更多
文章标签: 脚本
个人分类: Linux
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭