来自python API的ansible本地连接

时间:2015-01-11 10:05:26

标签: python ansible

我想用python API编写ansible脚本来做:     #ansible 127.0.0.1 --connection = local -m service -a' name = lxc-net state = started enabled = yes'

127.0.0.1 | success >> {
    "changed": true,
    "enabled": true,
    "name": "lxc-net",
    "state": "started"
}

2 个答案:

答案 0 :(得分:0)

您已经指定了connection = local。查看module_args='....

答案 1 :(得分:0)

Here我看到了transport = C.DEFAULT_TRANSPORT,#' ssh',' paramiko',' local'

#!/usr/bin/python
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
# example of getting the uptime of all hosts, 10 at a time

import ansible.runner
import sys

# construct the ansible runner and execute on all hosts
results = ansible.runner.Runner(
    transport='local',
    pattern='*', forks=10,
    module_name='command', module_args='/usr/bin/uptime',
).run()

if results is None:
   print "No hosts found"
   sys.exit(1)

print "UP ***********"
for (hostname, result) in results['contacted'].items():
    if not 'failed' in result:
        print "%s >>> %s" % (hostname, result['stdout'])

print "FAILED *******"
for (hostname, result) in results['contacted'].items():
    if 'failed' in result:
        print "%s >>> %s" % (hostname, result['msg'])

print "DOWN *********"
for (hostname, result) in results['dark'].items():
    print "%s >>> %s" % (hostname, result)