您的位置:首页 > 其它

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  ]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: