• ansible inventory.py 脚本


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import json
    import sys
    import cx_Oracle
    # def group():
    #     info_dict = {"all":["192.168.137.3","192.168.137.4"]}
    #     print json.dumps(info_dict,indent=4)
    # def host(ip):
    #     info_dict = {"192.168.137.3":{"ansible_ssh_host":"测试环境1"},"192.168.1.6":{"ansible_ssh_host":"测试环境2"}}
    #     print json.dumps(info_dict[ip],indent=4)
    # if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
    #     group()
    # elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
    #     host(sys.argv[2])
    # else:
    #     print "Usage: %s --list or --host <hostname>" % sys.argv[0]
    #     sys.exit(1)
    def readDb(sql):
        conn = cx_Oracle.connect('system/oracle@192.168.137.2/serv')
        cursor = conn.cursor()
        xsql=sql
        #print xsql
        r = cursor.execute(xsql)
        return r
    arr1=[]
    def groupList(sql):
         a=readDb(sql)
         for x in a:
             arr1.append(x[0])
         b={'test':arr1}
         c=json.dumps(b)
         print c
    arr2=[]
    host_dict = {}
    def hostList(key):
        a=readDb('select * from tlcb_mon_device')
        for x in a:
            host_dict[x[0]]={"ansible_ssh_host":x[0],"ansible_ssh_desc":x[1]}
        print host_dict[key]
    if len(sys.argv) == 3 and (sys.argv[1] == '--list'):
        groupList(sys.argv[2])
    elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
        hostList(sys.argv[2])
    else:
        print "Usage: %s --list sql or --host <hostname>" % sys.argv[0]
        sys.exit(1)
    
    
    group 函数:
    [root@node01 ansible]# python p3.py --list 'select ip  from tlcb_mon_device'
    {"test": ["192.168.137.3", "192.168.137.4", "115.236.19.4"]}
    [root@node01 ansible]# 
    
    
    host函数;
    [root@node01 ansible]# python inventory.py --host '192.168.137.3'
    {
        "ansible_ssh_host": "u6d4bu8bd5u73afu58831"
    }

  • 相关阅读:
    SQL Server-基础-经典SQL语句
    Web Service简介
    大学学习与班级社团管理经验总结
    arduino超声波开发模块
    CSS中关于居中的几种设置方法
    Software testing lab3
    jQuery表格排序问题
    Jquery中的data()方法、att()、prop()方法的区分
    WEB开发自己收集的资源
    Jquery插件封装---奇怪的代码
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349482.html
Copyright © 2020-2023  润新知