Ansible应用总结【第九篇】: Ansible之API讲解

Ansible应用总结【第九篇】: Ansible之API讲解

  Ansible API 的使用非常强大,也非常简单,只不过把模块需要使用的参数写到了脚本中。当业务比较大比较复杂的时候, 单纯的使用Ansible有时候不会很好的完成相关的运维工作, 这个时候就需要开发针对自己业务的一些模块或者ansible插件来完成这些工作。 Ansible还提供了Python接口,可以使用Python开发更为自动化的运维管理系统。

Ansible API

案例1:简单的API示例,为了增加可视性,可以将结果进行json美化输出

复制代码
[ansible@lyjl_ansible scripts]$ cat ansible_api.py 
#!/usr/bin/env python
# coding=utf-8
import ansible.runner
import json
runner = ansible.runner.Runner(
       module_name='ping',                 # 模块名称
       module_args='',              # 模块参数 
       pattern='35_db',                     # 主机组
       forks=10                             # 并发量
    )
datastructure = runner.run()
data = json.dumps(datastructure,indent=4)
print data

[ansible@lyjl_ansible scripts]$ python ansible_api.py
{
    "dark": {
        "lyjl-db-23-10.62.121.185": {
            "msg": "FAILED: [Errno 111] Connection refused",
            "failed": true
        }
    },
    "contacted": {
        "lyjl-db-80-10.62.121.53": {
            "invocation": {
                "module_name": "ping",
                "module_args": ""
            },
            "changed": false,
            "ping": "pong"
        },
        "lyjl-db-60-10.62.121.53": {
            "invocation": {
                "module_name": "ping",
                "module_args": ""
            },
            "changed": false,
            "ping": "pong"
        },
        "lyjl-db-10-10.62.121.203": {
            "invocation": {
                "module_name": "ping",
                "module_args": ""
            },
            "changed": false,
            "ping": "pong"
        },
        "lyjl-db-79-10.62.121.203": {
            "invocation": {
                "module_name": "ping",
                "module_args": ""
            },
            "changed": false,
            "ping": "pong"
        }
    }
}
复制代码

注:如果主机不通或者失败,结果将会输出到 dark部分,否则结果输出到 contacted部分

案例2:复杂点的API案例

复制代码
[ansible@lyjl_ansible scripts]$ cat 2.py 
#!/usr/bin/python

import ansible.runner
import sys

# construct the ansible runner and execute on all hosts
results = ansible.runner.Runner(
    pattern='35_db', 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)

# 结果:
[ansible@lyjl_ansible scripts]$ python 2.py 
UP ***********
lyjl-db-60-10.62.121.53 >>>  11:45:03 up 427 days, 22:24,  0 users,  load average: 0.20, 0.15, 0.11
lyjl-db-79-10.62.121.203 >>>  11:45:03 up 708 days, 36 min,  1 user,  load average: 1.61, 0.84, 0.52
lyjl-db-10-10.62.121.203 >>>  11:45:04 up 708 days, 36 min,  1 user,  load average: 1.61, 0.84, 0.52
lyjl-db-80-10.62.121.53 >>>  11:45:03 up 427 days, 22:24,  0 users,  load average: 0.18, 0.14, 0.11
FAILED *******
DOWN *********
lyjl-db-23-10.62.121.185 >>> {'msg': 'FAILED: [Errno 111] Connection refused', 'failed': True}
复制代码

注:上面的示例中对主机的输出结果进行了判断,并且对结果的输出进行了定制化,同时可以对比下API的结果和命令行执行的结果的差异

ansible_playbook API

待补充

API总结

API应用场景

1)前一次的执行结果作为后一次任务的参数输入

2)对任务的执行结果进行定制化输出或者存储

3)方便二次开发及和其他程序之间的耦合调用

出处:http://www.cnblogs.com/madsnotes/

声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

猜你喜欢

转载自blog.csdn.net/jackliu16/article/details/80585159
今日推荐