首页 > ansible api操作某个IP

ansible api操作某个IP

命令行下

[root@localhost facts]# ansible -i "10.100.151.2," all -m ping
10.100.151.2 | success >> {
    "changed": false, 
    "ping": "pong"
}

请问用api如何实现命令行的指定ip功能?


#!/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',
          pattern='10.100.151.2'
          ).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
【热门文章】
【热门文章】