当我们在外部使用``–list``参数调用这个脚本时,这个脚本必须返回一个JSON散列/字典,listjson


[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:\Python27\python.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'>

相关内容

    暂无相关文章