ansible api简单应用
2015-11-23 00:00
148 查看
1.uptime #!/usr/bin/python #encoding:utf-8 import ansible.runner import sys # construct the ansible runner and execute on all hosts results = ansible.runner.Runner( pattern='client', forks=10, module_name='command', module_args='/usr/bin/uptime', ).run() if results is None: print "No hosts found" sys.exit(1) for (hostname, result) in results['contacted'].items(): if not 'failed' in result: print "UP ***********" print "%s >>> %s" % (hostname, result['stdout']) for (hostname, result) in results['contacted'].items(): if 'failed' in result: print "FAILED *******" print "%s >>> %s" % (hostname, result['msg']) for (hostname, result) in results['dark'].items(): print "DOWN *********" print "%s >>> %s" % (hostname, result) UP *********** client1.info.com >>> 21:54:00 up 10 min, 1 user, load average: 0.02, 0.14, 0.15 UP *********** puppetmaster.info.com >>> 21:54:00 up 11 min, 1 user, load average: 0.30, 1.56, 1.41 2.ping #!/usr/bin/python #encoding:utf-8 import ansible.runner import sys # construct the ansible runner and execute on all hosts results = ansible.runner.Runner( pattern='client', forks=10, module_name='ping', #module_args='ping', ).run() if results is None: print "No hosts found" sys.exit(1) for (hostname, result) in results['contacted'].items(): if not 'failed' in result: print "%s >>>ping:%s" % (hostname, result['ping']) for (hostname, result) in results['dark'].items(): if 'failed' in result: print "%s >>>ping:%s" % (hostname, result['msg']) client1.info.com >>>ping:pong puppetmaster.info.com >>>ping:pong 3.command #!/usr/bin/python #encoding:utf-8 import ansible.runner import sys command = sys.argv[1] results = ansible.runner.Runner( pattern='client', forks=10, module_name='shell', module_args=command, ). 3ff0 run() if results is None: print "No hosts found" sys.exit(1) for (hostname, result) in results['contacted'].items(): if not 'failed' in result: print "UP ***********" print "%s" % (hostname) print "%s" %(result['stdout']) print '' for (hostname, result) in results['contacted'].items(): if 'failed' in result: print "FAILED *******" print "%s >>> %s" % (hostname, result['msg']) print '' for (hostname, result) in results['dark'].items(): print "DOWN *********" print "%s >>> %s" % (hostname, result) print '' UP *********** client1.info.com 2015-11-23-21:56:12 UP *********** puppetmaster.info.com 2015-11-23-21:56:11 4.install ftp(yum) #!/usr/bin/python #encoding:utf-8 import ansible.runner import sys def check_ftp(): check_ftp = ansible.runner.Runner( pattern='client', forks=10, module_name='shell', module_args='rpm -q vsftpd', ).run() if check_ftp is None: print "No hosts found" sys.exit(1) for (hostname, result) in check_ftp['contacted'].items(): if not 'failed' in result: print "%s" % (hostname) print "%s" %(result['stdout']) print '' return 1 for (hostname, result) in check_ftp['contacted'].items(): if 'failed' in result: print "%s >>> %s" % (hostname, result['msg']) print '' for (hostname, result) in check_ftp['dark'].items(): print "%s >>> %s" % (hostname, result) print '' def install(): install_ftp = ansible.runner.Runner( pattern='client', forks=10, module_name='yum', module_args='name=vsftpd state=latest', ).run() if check_ftp is None: print "No hosts found" sys.exit(1) for (hostname, result) in install_ftp['contacted'].items(): if not 'failed' in result: print "%s" % (hostname) for v in result['results']: print v return 1 for (hostname, result) in install_ftp['contacted'].items(): if 'failed' in result: print "%s >>> %s" % (hostname, result['msg']) print '' for (hostname, result) in install_ftp['dark'].items(): print "%s >>> %s" % (hostname, result) print '' def start(): start_ftp = ansible.runner.Runner( pattern='client', forks=10, module_name='shell', module_args='/etc/init.d/vsftpd start', ).run() if start_ftp is None: print "No hosts found" sys.exit(1) for (hostname, result) in start_ftp['contacted'].items(): if not 'failed' in result: print "%s" % (hostname) print "%s" %(result['stdout']) print '' return 1 for (hostname, result) in start_ftp['contacted'].items(): if 'failed' in result: print "%s >>> %s" % (hostname, result['msg']) print '' for (hostname, result) in start_ftp['dark'].items(): print "%s >>> %s" % (hostname, result) print '' if __name__ == '__main__': check_ftp() install() start() client1.info.com package vsftpd is not installed client1.info.com Loaded plugins: product-id, subscription-manager Updating certificate-based repositories. Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package vsftpd.x86_64 0:2.2.2-11.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vsftpd x86_64 2.2.2-11.el6 rhel-iso 151 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 151 k Installed size: 331 k Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : vsftpd-2.2.2-11.el6.x86_64 1/1 Installed products updated. Verifying : vsftpd-2.2.2-11.el6.x86_64 1/1 Installed: vsftpd.x86_64 0:2.2.2-11.el6 Complete! client1.info.com All packages providing vsftpd are up to date client1.info.com Starting vsftpd for vsftpd: [ OK ]
相关文章推荐
- Day 9:(12)用户定义函数实训参考答案
- authentication vs authorization 验证与授权的区别
- scala中函数调用
- scala中默认擦描述和带名参数
- scala可变长度参数(一)
- scala可变长度参数(二)
- Android开发之listview 的使用中Item获取焦点的处理
- Android 开发之自定义滑动开关
- Android开发之细说ListView上拉加载,下拉刷新过程
- how to setup c develop environment for windows
- golang windows 交叉编译 Linux
- 使用Adobe Reader控件结合Java实现PDF打印功能
- SimpleDateFormat和DateFormat类不是线程安全的。
- php执行msyql脚本 sql文件
- ViewPager如下效果你研究过吗?
- vbox 中复制不可以用
- android 开发环境 ubuntu 12.04
- python-02:学习路线
- python-03:用最简单快速的方法入门
- ABC输出BCA