zoukankan      html  css  js  c++  java
  • 动态inventory脚本必须支如下两个命令行参数

    #!/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():
         a=readDb('select ip from tlcb_mon_device')
         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]}
        return host_dict[key]
    print '------------hostList-----------------------------------------'
    a=hostList('192.168.137.3')
    print a
    print type(a)
    print a['ansible_ssh_host']
    print a['ansible_ssh_desc'].decode('gbk').encode('utf-8')
    print len(a['ansible_ssh_desc'])
    print '---------------groupList--------------------------------------'
    groupList()
    
    
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a20.py
    ------------hostList-----------------------------------------
    select * from tlcb_mon_device
    {'ansible_ssh_host': '192.168.137.3', 'ansible_ssh_desc': 'xb2xe2xcaxd4xd6xf7xbbxfa'}
    <type 'dict'>
    192.168.137.3
    测试主机
    8
    ---------------groupList--------------------------------------
    select ip from tlcb_mon_device
    {"test": ["192.168.137.3", "192.168.137.4", "115.236.19.4"]}
    
    Process finished with exit code 0
    

  • 相关阅读:
    (并查集)小希的迷宫 --HDU -- 1272
    (并查集)Connections in Galaxy War -- zoj --3261 还没写
    (并查集)A Bug's Life -- POJ -- 2492
    LINQ 图解 LINQ学习第三篇 [转]
    C# 4.0 新特性-dynamic 【转】
    C#编程规范
    C#中关于DateTime的最大值和最小值
    .NET,你忘记了么?(八)—— 从dynamic到特性误用 [转]
    C#中dynamic的正确用法【转】
    ASP.NET MVC 中将FormCollection与实体间转换方法【转】
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349483.html
Copyright © 2011-2022 走看看