<s> DOCUMENTATION = '''<STR_LIT>''' <EOL> EXAMPLES = '''<STR_LIT>''' <EOL> from ansible . module_utils . basic import * <EOL> from collections import defaultdict <EOL> try : <EOL> from nelsnmp . snmp import SnmpHandler <EOL> from nelsnmp . vendors . cisco . oids import CiscoOids <EOL> o = CiscoOids ( ) <EOL> has_nelsnmp = True <EOL> except : <EOL> has_nelsnmp = False <EOL> NELSNMP_PARAMETERS = ( <EOL> '<STR_LIT:host>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:version>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> PORT_MODE = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> } <EOL> def changed_status ( changed , has_changed ) : <EOL> if changed == True : <EOL> has_changed = True <EOL> return has_changed <EOL> def set_state ( dev , oid , desired_state , module ) : <EOL> try : <EOL> current_state = dev . get_value ( oid ) <EOL> except Exception , err : <EOL> module . fail_json ( msg = str ( err ) ) <EOL> if current_state == desired_state : <EOL> return False <EOL> else : <EOL> try : <EOL> dev . set ( oid , desired_state ) <EOL> except : <EOL> module . fail_json ( msg = '<STR_LIT>' ) <EOL> return True <EOL> def main ( ) : <EOL> module = AnsibleModule ( <EOL> argument_spec = dict ( <EOL> host = dict ( required = True ) , <EOL> version = dict ( required = True , choices = [ '<STR_LIT>' , '<STR_LIT:3>' ] ) , <EOL> community = dict ( required = False , default = False ) , <EOL> username = dict ( required = False ) , <EOL> level = dict ( required = False , choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> integrity = dict ( required = False , choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> privacy = dict ( required = False , choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> authkey = dict ( required = False ) , <EOL> privkey = dict ( required = False ) , <EOL> mode = dict ( required = True , choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> interface_id = dict ( required = False ) , <EOL> interface_name = dict ( required = False ) , <EOL> access_vlan = dict ( required = False ) , <EOL> native_vlan = dict ( required = False ) , <EOL> removeplaceholder = dict ( required = False ) , <EOL> ) , <EOL> mutually_exclusive = ( [ '<STR_LIT>' , '<STR_LIT>' ] , ) , <EOL> required_one_of = ( [ '<STR_LIT>' , '<STR_LIT>' ] , ) , <EOL> required_together = ( <EOL> [ '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ) , <EOL> supports_check_mode = False ) <EOL> m_args = module . params <EOL> if not has_nelsnmp : <EOL> module . fail_json ( msg = '<STR_LIT>' ) <EOL> if m_args [ '<STR_LIT:version>' ] == "<STR_LIT>" : <EOL> if m_args [ '<STR_LIT>' ] == False : <EOL> module . fail_json ( msg = '<STR_LIT>' ) <EOL> if m_args [ '<STR_LIT:version>' ] == "<STR_LIT:3>" : <EOL> if m_args [ '<STR_LIT:username>' ] == None : <EOL> module . fail_json ( msg = '<STR_LIT>' ) <EOL> if m_args [ '<STR_LIT>' ] == "<STR_LIT>" and m_args [ '<STR_LIT>' ] == None : <EOL> module . fail_json ( msg = '<STR_LIT>' ) <EOL> nelsnmp_args = { } <EOL> for key in m_args : <EOL> if key in NELSNMP_PARAMETERS and m_args [ key ] != None : <EOL> nelsnmp_args [ key ] = m_args [ key ] <EOL> try : <EOL> dev = SnmpHandler ( ** nelsnmp_args ) <EOL> except Exception , err : <EOL> module . fail_json ( msg = str ( err ) ) <EOL> has_changed = False <EOL> if m_args [ '<STR_LIT>' ] : <EOL> try : <EOL> interface = False <EOL> vartable = dev . getnext ( o . ifDescr ) <EOL> for varbinds in vartable : <EOL> for oid , val in varbinds : <EOL> if m_args [ '<STR_LIT>' ] == val : <EOL> interface = oid . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> if interface == False : <EOL> module . fail_json ( msg = '<STR_LIT>' ) <EOL> except Exception , err : <EOL> module . fail_json ( msg = str ( err ) ) <EOL> if m_args [ '<STR_LIT>' ] : <EOL> interface = m_args [ '<STR_LIT>' ] <EOL> if m_args [ '<STR_LIT>' ] : <EOL> oid = o . vlanTrunkPortDynamicState + "<STR_LIT:.>" + str ( interface ) <EOL> desired_state = PORT_MODE [ m_args [ '<STR_LIT>' ] ] <EOL> changed = set_state ( dev , oid , desired_state , module ) <EOL> has_changed = changed_status ( changed , has_changed ) <EOL> if m_args [ '<STR_LIT>' ] : <EOL> oid = o . vmVlan + "<STR_LIT:.>" + str ( interface ) <EOL> desired_state = int ( m_args [ '<STR_LIT>' ] ) <EOL> changed = set_state ( dev , oid , desired_state , module ) <EOL> has_changed = changed_status ( changed , has_changed ) <EOL> if m_args [ '<STR_LIT>' ] : <EOL> oid = o . vlanTrunkPortNativeVlan + "<STR_LIT:.>" + str ( interface ) <EOL> desired_state = int ( m_args [ '<STR_LIT>' ] ) <EOL> changed = set_state ( dev , oid , desired_state , module ) <EOL> has_changed = changed_status ( changed , has_changed ) <EOL> return_status = { '<STR_LIT>' : has_changed } <EOL> module . exit_json ( ** return_status ) <EOL> main ( ) </s>
