ansible api操作某个IP

发布于 2022-09-01 23:56:40 字数 207 浏览 21 评论 0

命令行下

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2022-09-08 23:56:40
#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文