Sep-17-2018, 09:30 AM
hi,
im stuck at the mentioned error. guess its easily fixable, but i cant get my head around it.
error:
Traceback (most recent call last):
File "./reg2inv.py", line 38, in <module>
print("delete:affected rows = {}".format(idx))
NameError: name 'idx' is not defined
any hints?
im stuck at the mentioned error. guess its easily fixable, but i cant get my head around it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#!/usr/bin/env python ''' populates ansible inventory db from aix registry db ''' # -*- coding: utf-8 -*- import os import sys import pymysql as db try : import json from json import JSONDecodeError except ImportError: import simplejson as json from simplejson import JSONDecodeError connections = { 'conn1' : db.connect( 'remotehost' , 'aix_regread' , '***' , 'aix_registry' , charset = 'utf8' ), 'conn2' : db.connect( 'localhost' , 'ansible' , '***' , 'ansible_inv' , charset = 'utf8' ) } c1 = connections[ 'conn1' ].cursor() c2 = connections[ 'conn2' ].cursor() try : sql = """SELECT DISTINCT NODE,IP FROM ansible_hosts_view;""" c1.execute(sql) sql = """SELECT DISTINCT NODE,IP FROM ansible_hosts_view;""" c2.execute(sql) diff_to_del = ( list ( set (c2.fetchall()) - set (c1.fetchall()))) for idx,row in enumerate (diff_to_del, start = 1 ): host, hostname = row sql = """DELETE FROM host WHERE host='{}';""" . format (host) c2.execute(sql % (host)) print ( "delete:affected rows = {}" . format (idx)) for idx,row in enumerate (c1, start = 1 ): host, hostname = row var_data = { 'inventory_lparname' : host} var_json = json.dumps(var_data) ena = "1" sql = """INSERT INTO host(host, hostname, variables, enabled) VALUES ('%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE host='{}', hostname='{}',variables='{}',enabled='{}';""" . format (host, hostname, var_json, ena) c2.execute(sql % (host, hostname, var_json, ena)) print ( "insert:affected rows = {}" . format (idx)) except db.Error,e: print e[ 0 ], e[ 1 ] connections[ 'conn2' ].rollback() c1.close() c2.close() [conn.close() for key, conn in connections.items()] connections[ 'conn2' ].commit() c1.close() c2.close() [conn.close() for key, conn in connections.items()] |
Traceback (most recent call last):
File "./reg2inv.py", line 38, in <module>
print("delete:affected rows = {}".format(idx))
NameError: name 'idx' is not defined
any hints?