ansible python api 2.0使用[ 转载 ]

最近想利用python来调用anbile来实现一些功能,发现ansible的api已经升级到了2.0,使用上比以前复杂了许多。

 这里我参考了官方文档的例子,做了一些整改,写了一个python调用ansible的函数,执行过程中输出执行结果。函数返回执行结果,便于筛选和存储所需的数据:


# vim exec_ansible.py
#!codint:utf-8
def exec_ansible(module,args,host):    
    import json
    from collections import namedtuple
    from ansible.parsing.dataloader import DataLoader
    from ansible.vars import VariableManager
    from ansible.inventory import Inventory
    from ansible.playbook.play import Play
    from ansible.executor.task_queue_manager import TaskQueueManager
    from ansible.plugins.callback import CallbackBase
 
    class ResultCallback(CallbackBase):
        def v2_runner_on_ok(self, result, **kwargs):
            host = result._host
            self.data = json.dumps({host.name: result._result}, indent=4)
            print(self.data)
 
    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
# initialize needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    #module_path参数指定本地ansible模块包的路径
    options = Options(connection='smart', module_path='/usr/local/lib/python3.5/site-packages/ansible/modules/', forks=100, become=None, become_method=None, become_user='root', check=False)
    passwords = dict(vault_pass='secret')
 
# Instantiate our ResultCallback for handling results as they come in
    results_callback = ResultCallback()
 
# create inventory and pass to var manager
    #host_list指定本地ansible的hosts文件
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts')
    variable_manager.set_inventory(inventory)
 
# create play with tasks
    play_source =  dict(
            name = "Ansible Play",
            #hosts可以指定inventory中的一组主机,也可指定单台主机
            hosts = host,
            gather_facts = 'no',
            #task执行列表,如果想一次执行多个任务,可以在列表中添加任务
            tasks = [
                dict(action=dict(module=module, args=args), register='shell_out'),
             ]
        )
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
 
# actually run it
    tqm = None
    try:
        tqm = TaskQueueManager(
                  inventory=inventory,
                  variable_manager=variable_manager,
                  loader=loader,
                  options=options,
                  passwords=passwords,
                  stdout_callback=results_callback,
              )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup
        return json.loads(results_callback.data)


2、本地host


# more /etc/ansible/hosts
[testserver]
192.168.52.128
192.168.52.135
3、调用如下:
  先调用testserver一组主机批量执行date命令:



>>> from exec_ansible import exec_ansible                             
>>> test1 = exec_ansible(module='shell',args='date',host='testserver')
{
    "192.168.52.135": {
        "warnings": [],
        "stderr": "",
        "delta": "0:00:00.003688",
        "_ansible_no_log": false,
        "stdout": "Sat Nov  5 18:54:17 CST 2016",
        "cmd": "date",
        "_ansible_parsed": true,
        "rc": 0,
        "invocation": {
            "module_args": {
                "removes": null,
                "executable": null,
                "creates": null,
                "chdir": null,
                "warn": true,
                "_raw_params": "date",
                "_uses_shell": true
            },
            "module_name": "command"
        },
        "start": "2016-11-05 18:54:17.563525",
        "changed": true,
        "end": "2016-11-05 18:54:17.567213",
        "stdout_lines": [
            "Sat Nov  5 18:54:17 CST 2016"
        ]
    }
}
{
    "192.168.52.128": {
        "warnings": [],
        "stderr": "",
        "delta": "0:00:00.003244",
        "_ansible_no_log": false,
        "stdout": "Sat Nov  5 21:48:38 CST 2016",
        "cmd": "date",
        "_ansible_parsed": true,
        "rc": 0,
        "invocation": {
            "module_args": {
                "removes": null,
                "executable": null,
                "creates": null,
                "chdir": null,
                "warn": true,
                "_raw_params": "date",
                "_uses_shell": true
            },
            "module_name": "command"
        },
        "start": "2016-11-05 21:48:38.252785",
        "changed": true,
        "end": "2016-11-05 21:48:38.256029",
        "stdout_lines": [
            "Sat Nov  5 21:48:38 CST 2016"
        ]
    }
}
4、指定单台执行命令:



test2 = exec_ansible(module='shell',args='free -m',host='192.168.52.128')
{
    "192.168.52.128": {
        "warnings": [],
        "changed": true,
        "invocation": {
            "module_args": {
                "_raw_params": "free -m",
                "executable": null,
                "chdir": null,
                "creates": null,
                "removes": null,
                "_uses_shell": true,
                "warn": true
            },
            "module_name": "command"
        },
        "rc": 0,
        "start": "2016-11-05 21:53:10.738545",
        "_ansible_parsed": true,
        "delta": "0:00:00.002871",
        "stdout_lines": [
            "             total       used       free     shared    buffers     cached",
            "Mem:          1869       1786         83          3        312        512",
            "-/+ buffers/cache:        961        908 ",
            "Swap:         4047          3       4044 "
        ],
        "stderr": "",
        "end": "2016-11-05 21:53:10.741416",
        "cmd": "free -m",
        "_ansible_no_log": false,
        "stdout": "             total       used       free     shared    buffers     cached\nMem:          1869       1786         83          3        312        512\n-/+ buffers/cache:        961        908 \nSwap:         4047          3       4044 "
    }
}


转载于:http://icenycmh.blog.51cto.com/4077647/1870642

本博客所有文章如无特别注明均为原创。作者:serven复制或转载请以超链接形式注明转自 ServenBlog
原文地址《ansible python api 2.0使用[ 转载 ]
分享到:更多

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)