zoukankan      html  css  js  c++  java
  • 当我们在外部使用``–list``参数调用这个脚本时,这个脚本必须返回一个JSON散列/字典

    [root@node01 ansible]# python  /etc/ansible/p1.py  -list
    ["192.168.137.3", "192.168.137.4"]
    [root@node01 ansible]# ansible -i  /etc/ansible/p1.py all -m raw -a "hostname"
     [WARNING]:  * Failed to parse /etc/ansible/p1.py with ini plugin: /etc/ansible/p1.py:3: Expected key=value host variable assignment, got: cx_Oracle
    
     [WARNING]: Unable to parse /etc/ansible/p1.py as an inventory source
    
     [WARNING]: No inventory was parsed, only implicit localhost is available
    
     [WARNING]: Could not match supplied host pattern, ignoring: all
    
     [WARNING]: provided hosts list is empty, only localhost is available
    
     [WARNING]: No hosts matched, nothing to do
    
    [root@node01 ansible]# cat p1.py 
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import cx_Oracle
    from pprint import pprint
    import csv
    import time
    import re
    import binascii
    import json
    
    conn = cx_Oracle.connect('system/oracle@192.168.137.2/serv')
    cursor = conn.cursor()
    xsql='select * from tlcb_mon_device'
    r = cursor.execute(xsql)
    #print r
    arr=[]
    for x in r:
        #print x[1]
       #print x[1].decode('gbk').encode('utf-8')
        #print len(x[1])
        y=x[1].decode('gbk').encode('utf-8')
        #print len(y)
        #print  type(x)
        arr.append(x[0])
        #arr.append(y)
    b=json.dumps(arr)
    #print arr
    #print type(arr)
    print b
    #print type(b)
    
    [root@node01 ansible]# python  /etc/ansible/p1.py  -list
    ["192.168.137.3", "192.168.137.4"]
    [root@node01 ansible]# ansible -i  /etc/ansible/p1.py all -m raw -a "hostname"
     [WARNING]:  * Failed to parse /etc/ansible/p1.py with ini plugin: /etc/ansible/p1.py:3: Expected key=value host variable assignment, got: cx_Oracle
    
     [WARNING]: Unable to parse /etc/ansible/p1.py as an inventory source
    
     [WARNING]: No inventory was parsed, only implicit localhost is available
    
     [WARNING]: Could not match supplied host pattern, ignoring: all
    
     [WARNING]: provided hosts list is empty, only localhost is available
    
     [WARNING]: No hosts matched, nothing to do
    
    
    
    当我们在外部使用``–list``参数调用这个脚本时,这个脚本必须返回一个JSON散列/字典
    
    当前返回的额json是数组,是不行的:
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a22.py
    ['192.168.137.3', '192.168.137.4']
    <type 'list'>
    ["192.168.137.3", "192.168.137.4"]
    <type 'str'>

  • 相关阅读:
    win7系统宽带自动断开怎么解决【系统天地】
    Win10系统修复受损文件教程【系统天地】
    win10如何加快开机速度【系统天地】
    Netty核心概念(5)之Channel
    Netty核心概念(4)之Bootstrap
    漫谈NIO(3)之Netty实现
    漫谈NIO(2)之Java的NIO
    漫谈NIO(1)之计算机IO实现
    机器学习实战(二)决策树
    机器学习实战(一)k-近邻算法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349487.html
Copyright © 2011-2022 走看看