def test_device_by_name(self, j_open, i_open): config_filepath = os.path.join(FIXTURES_DIR, '.ntc.conf.sample') nxos_device = ntc_device_by_name('test_nxos', filename=config_filepath) self.assertIsInstance(nxos_device, NXOSDevice) eos_device = ntc_device_by_name('test_eos', filename=config_filepath) self.assertIsInstance(eos_device, EOSDevice) ios_device = ntc_device_by_name('test_ios', filename=config_filepath) self.assertIsInstance(ios_device, IOSDevice)
def check_device(module, username, password, host, timeout, kwargs): success = False attempts = timeout / 30 counter = 0 atomic = False while counter < attempts and not success: try: if module.params['ntc_host'] is not None: device = ntc_device_by_name(module.params['ntc_host'], module.params['ntc_conf_file']) else: device_type = module.params['platform'] device = ntc_device(device_type, host, username, password, **kwargs) success = True atomic = True try: device.close() except: atomic = False pass except: time.sleep(30) counter += 1 return success, atomic
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), checkpoint_file=dict(required=False), rollback_to=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ['checkpoint_file', 'rollback_to'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=False ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) checkpoint_file = module.params['checkpoint_file'] rollback_to = module.params['rollback_to'] device.open() status = None filename = None changed = False try: if checkpoint_file: device.checkpoint(checkpoint_file) status = 'checkpoint file created' elif rollback_to: device.rollback(rollback_to) status = 'rollback executed' changed = True filename = rollback_to or checkpoint_file except Exception as e: module.fail_json(msg=str(e)) device.close() module.exit_json(changed=changed, status=status, filename=filename)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=False ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) device.open() facts = device.facts device.close() module.exit_json(ansible_facts=facts)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), remote_file=dict(required=False), local_file=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=False ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) remote_file = module.params['remote_file'] local_file = module.params['local_file'] device.open() if remote_file: remote_save_successful = device.save(remote_file) else: remote_save_successful = device.save() changed = remote_save_successful if local_file: device.backup_running_config(local_file) changed = True device.close() remote_file = remote_file or '(Startup Config)' module.exit_json(changed=changed, remote_save_successful=remote_save_successful, remote_file=remote_file, local_file=local_file)
def main(): connection_argument_spec = dict( platform=dict(choices=[ PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS ], required=False), host=dict(required=False), port=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str', no_log=True), secret=dict(required=False, type='str', no_log=True), transport=dict(required=False, choices=['http', 'https']), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), ) base_argument_spec = dict( checkpoint_file=dict(required=False), rollback_to=dict(required=False), ) argument_spec = base_argument_spec argument_spec.update(connection_argument_spec) argument_spec["provider"] = dict(required=False, type="dict", options=connection_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ['checkpoint_file', 'rollback_to'], ], required_one_of=[['host', 'ntc_host', 'provider']], supports_check_mode=False) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') provider = module.params['provider'] or {} # allow local params to override provider for param, pvalue in provider.items(): if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) checkpoint_file = module.params['checkpoint_file'] rollback_to = module.params['rollback_to'] argument_check = { 'host': host, 'username': username, 'platform': platform, 'password': password } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") device.open() status = None filename = None changed = False try: if checkpoint_file: device.checkpoint(checkpoint_file) status = 'checkpoint file created' elif rollback_to: device.rollback(rollback_to) status = 'rollback executed' changed = True filename = rollback_to or checkpoint_file except Exception as e: module.fail_json(msg=str(e)) device.close() module.exit_json(changed=changed, status=status, filename=filename)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict( choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_F5, PLATFORM_ASA], required=False, ), host=dict(required=False), username=dict(required=False, type="str"), password=dict(required=False, type="str", no_log=True), secret=dict(required=False, no_log=True), transport=dict(required=False, choices=["http", "https"]), port=dict(required=False, type="int"), provider=dict(type="dict", required=False), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), system_image_file=dict(required=True), kickstart_image_file=dict(required=False), volume=dict(required=False, type="str"), ), mutually_exclusive=[ ["host", "ntc_host"], ["ntc_host", "secret"], ["ntc_host", "transport"], ["ntc_host", "port"], ["ntc_conf_file", "secret"], ["ntc_conf_file", "transport"], ["ntc_conf_file", "port"], ], required_one_of=[["host", "ntc_host", "provider"]], required_if=[["platform", PLATFORM_F5, ["volume"]]], supports_check_mode=True, ) if not HAS_PYNTC: module.fail_json(msg="pyntc Python library not found.") provider = module.params["provider"] or {} no_log = ["password", "secret"] for param in no_log: if provider.get(param): module.no_log_values.update(return_values(provider[param])) # allow local params to override provider for param, pvalue in provider.items(): if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue platform = module.params["platform"] host = module.params["host"] username = module.params["username"] password = module.params["password"] ntc_host = module.params["ntc_host"] ntc_conf_file = module.params["ntc_conf_file"] transport = module.params["transport"] port = module.params["port"] secret = module.params["secret"] argument_check = { "host": host, "username": username, "platform": platform, "password": password, } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs["transport"] = transport if port is not None: kwargs["port"] = port if secret is not None: kwargs["secret"] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) system_image_file = module.params["system_image_file"] kickstart_image_file = module.params["kickstart_image_file"] volume = module.params["volume"] if kickstart_image_file == "null": kickstart_image_file = None device.open() pre_install_boot_options = device.get_boot_options() changed = False if not already_set( boot_options=pre_install_boot_options, system_image_file=system_image_file, kickstart_image_file=kickstart_image_file, volume=volume, device=device, ): changed = True if not module.check_mode and changed == True: if device.device_type == "nxos": timeout = 600 device.set_timeout(timeout) try: start_time = time.time() device.set_boot_options(system_image_file, kickstart=kickstart_image_file) except: pass elapsed_time = time.time() - start_time device.set_timeout(30) try: install_state = device.get_boot_options() except: install_state = {} while elapsed_time < timeout and not install_state: try: install_state = device.get_boot_options() except: time.sleep(10) elapsed_time += 10 else: device.set_boot_options( system_image_file, kickstart=kickstart_image_file, volume=volume ) install_state = device.get_boot_options() if not already_set( boot_options=install_state, system_image_file=system_image_file, kickstart_image_file=kickstart_image_file, volume=volume, device=device, ): module.fail_json(msg="Install not successful", install_state=install_state) else: install_state = pre_install_boot_options device.close() module.exit_json(changed=changed, install_state=install_state)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), confirm=dict(required=False, default=False, type='bool', choices=BOOLEANS), timer=dict(requred=False, type='int'), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=False ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform_to_device_type[platform] device = ntc_device(device_type, host, username, password, **kwargs) confirm = module.params['confirm'] timer = module.params['timer'] if not confirm: module.fail_json(msg='confirm must be set to true for this module to work.') supported_timer_platforms = [PLATFORM_IOS] if timer is not None \ and platform not in supported_timer_platforms: module.fail_json(msg='Timer parameter not supported on platform %s.' % platform) device.open() changed = False rebooted = False if timer is not None: device.reboot(confirm=True, timer=timer) else: device.reboot(confirm=True) changed = True rebooted = True device.close() module.exit_json(changed=changed, rebooted=rebooted)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict( choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_F5, PLATFORM_ASA], required=False, ), host=dict(required=False), username=dict(required=False, type="str"), password=dict(required=False, type="str", no_log=True), secret=dict(required=False, no_log=True), transport=dict(required=False, choices=["http", "https"]), port=dict(required=False, type="int"), provider=dict(type="dict", required=False), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), system_image_file=dict(required=True), kickstart_image_file=dict(required=False), volume=dict(required=False, type="str"), reboot=dict(required=False, type="bool", default=False), ), mutually_exclusive=[ ["host", "ntc_host"], ["ntc_host", "secret"], ["ntc_host", "transport"], ["ntc_host", "port"], ["ntc_conf_file", "secret"], ["ntc_conf_file", "transport"], ["ntc_conf_file", "port"], ], required_one_of=[["host", "ntc_host", "provider"]], required_if=[["platform", PLATFORM_F5, ["volume"]]], supports_check_mode=True, ) if not HAS_PYNTC: module.fail_json(msg="pyntc Python library not found.") # TODO: Change to fail_json when deprecating older pyntc if not HAS_PYNTC_VERSION: module.warn("Support for pyntc version < 0.0.9 is being deprecated; please upgrade pyntc") # TODO: Remove warning when deprecating reboot option on non-F5 devices module.warn("Support for installing the OS without rebooting may be deprecated in the future") provider = module.params["provider"] or {} no_log = ["password", "secret"] for param in no_log: if provider.get(param): module.no_log_values.update(return_values(provider[param])) # allow local params to override provider for param, pvalue in provider.items(): # TODO: Figure out exactly the purpose of this and correct truthiness or noneness if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue platform = module.params["platform"] host = module.params["host"] username = module.params["username"] password = module.params["password"] ntc_host = module.params["ntc_host"] ntc_conf_file = module.params["ntc_conf_file"] transport = module.params["transport"] port = module.params["port"] secret = module.params["secret"] reboot = module.params["reboot"] # TODO: Remove checks if we require reboot for non-F5 devices if platform == "cisco_nxos_nxapi" and not reboot: module.fail_json(msg='NXOS requires setting the "reboot" parameter to True') if platform != "cisco_nxos_nxapi" and reboot and not HAS_PYNTC_VERSION: module.fail_json( msg='Using the "reboot" parameter for non-NXOS devices' "requires pyntc version > 0.0.8" ) argument_check = { "host": host, "username": username, "platform": platform, "password": password, } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs["transport"] = transport if port is not None: kwargs["port"] = port if secret is not None: kwargs["secret"] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) system_image_file = module.params["system_image_file"] kickstart_image_file = module.params["kickstart_image_file"] volume = module.params["volume"] if kickstart_image_file == "null": kickstart_image_file = None device.open() pre_install_boot_options = device.get_boot_options() if not module.check_mode: # TODO: Remove conditional when deprecating older pyntc if HAS_PYNTC_VERSION: try: # TODO: Remove conditional if we require reboot for non-F5 devices if reboot or device.device_type == "f5_tmos_icontrol": changed = device.install_os( image_name=system_image_file, kickstart=kickstart_image_file, volume=volume ) else: # TODO: Remove support if we require reboot for non-F5 devices changed = device.set_boot_options(system_image_file) except ( CommandError, CommandListError, FileSystemNotFoundError, NotEnoughFreeSpaceError, NTCFileNotFoundError, OSInstallError, RebootTimeoutError, ) as e: module.fail_json(msg=e.message) except Exception as e: module.fail_json(msg=str(e)) if ( reboot and device.device_type == "f5_tmos_icontrol" and pre_install_boot_options["active_volume"] != volume ): try: changed = True device.reboot(confirm=True, volume=volume) except RuntimeError: module.fail_json( msg="Attempted reboot but did not boot to desired volume", original_volume=pre_install_boot_options["active_volume"], expected_volume=volume, ) install_state = device.get_boot_options() # TODO: Remove contents of else when deprecating older pyntc else: changed = False install_state = pre_install_boot_options if not already_set( boot_options=pre_install_boot_options, system_image_file=system_image_file, kickstart_image_file=kickstart_image_file, volume=volume, device=device, ): changed = True if device.device_type == "nxos": timeout = 600 device.set_timeout(timeout) try: start_time = time.time() device.set_boot_options(system_image_file, kickstart=kickstart_image_file) except: pass elapsed_time = time.time() - start_time device.set_timeout(30) try: install_state = device.get_boot_options() except: install_state = {} while elapsed_time < timeout and not install_state: try: install_state = device.get_boot_options() except: time.sleep(10) elapsed_time += 10 else: device.set_boot_options( system_image_file, kickstart=kickstart_image_file, volume=volume ) install_state = device.get_boot_options() if not already_set( boot_options=pre_install_boot_options, system_image_file=system_image_file, kickstart_image_file=kickstart_image_file, volume=volume, device=device, ): module.fail_json(msg="Install not successful", install_state=install_state) else: if HAS_PYNTC_VERSION: changed = device._image_booted( image_name=system_image_file, kickstart=kickstart_image_file, volume=volume ) # TODO: Remove contents of else when deprecating older pyntc else: changed = already_set( boot_options=pre_install_boot_options, system_image_file=system_image_file, kickstart_image_file=kickstart_image_file, volume=volume, device=device, ) install_state = pre_install_boot_options device.close() module.exit_json(changed=changed, install_state=install_state)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str', no_log=True), secret=dict(required=False, no_log=True), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), provider=dict(type='dict', required=False), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host', 'provider']], supports_check_mode=False ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') provider = module.params['provider'] or {} # allow local params to override provider for param, pvalue in provider.items(): if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] argument_check = { 'host': host, 'username': username, 'platform': platform, 'password': password } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) device.open() facts = device.facts device.close() module.exit_json(ansible_facts=facts)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[ PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS ], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), checkpoint_file=dict(required=False), rollback_to=dict(required=False), ), mutually_exclusive=[ ['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ['checkpoint_file', 'rollback_to'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=False) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) checkpoint_file = module.params['checkpoint_file'] rollback_to = module.params['rollback_to'] device.open() status = None filename = None changed = False try: if checkpoint_file: device.checkpoint(checkpoint_file) status = 'checkpoint file created' elif rollback_to: device.rollback(rollback_to) status = 'rollback executed' changed = True filename = rollback_to or checkpoint_file except Exception as e: module.fail_json(msg=str(e)) device.close() module.exit_json(changed=changed, status=status, filename=filename)
def test_no_conf_file(self): with self.assertRaises(ConfFileNotFoundError): ntc_device_by_name('test_bad_device', filename='/bad/file/path')
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), local_file=dict(required=True), remote_file=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=True ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform_to_device_type[platform] device = ntc_device(device_type, host, username, password, **kwargs) local_file = module.params['local_file'] remote_file = module.params['remote_file'] device.open() changed = False transfer_status = 'No Transfer' file_exists = True if not os.path.isfile(local_file): module.fail_json(msg="Local file {} not found".format(local_file)) if not device.file_copy_remote_exists(local_file, remote_file): changed = True file_exists = False if not module.check_mode and not file_exists: try: device.file_copy(local_file, remote_file) transfer_status = 'Sent' except Exception as e: module.fail_json(msg=str(e)) device.close() if remote_file is None: remote_file = os.path.basename(local_file) module.exit_json(changed=changed, transfer_status=transfer_status, local_file=local_file, remote_file=remote_file)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), local_file=dict(required=True), remote_file=dict(required=False), file_system=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=True ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) local_file = module.params['local_file'] remote_file = module.params['remote_file'] file_system = module.params['file_system'] device.open() changed = False transfer_status = 'No Transfer' file_exists = True if not os.path.isfile(local_file): module.fail_json(msg="Local file {} not found".format(local_file)) if file_system: remote_exists = device.file_copy_remote_exists(local_file, remote_file, file_system=file_system) else: remote_exists = device.file_copy_remote_exists(local_file, remote_file) if not remote_exists: changed = True file_exists = False if not module.check_mode and not file_exists: try: if file_system: device.file_copy(local_file, remote_file, file_system=file_system) else: device.file_copy(local_file, remote_file) transfer_status = 'Sent' except Exception as e: module.fail_json(msg=str(e)) device.close() if remote_file is None: remote_file = os.path.basename(local_file) module.exit_json(changed=changed, transfer_status=transfer_status, local_file=local_file, remote_file=remote_file, file_system=file_system)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), system_image_file=dict(required=True), kickstart_image_file=dict(required=False), ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=True ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) if device.device_type == PLATFORM_JUNOS: module.fail_json(msg='Install OS for Juniper not supported.') system_image_file = module.params['system_image_file'] kickstart_image_file = module.params['kickstart_image_file'] if kickstart_image_file == 'null': kickstart_image_file = None device.open() current_boot_options = device.get_boot_options() changed = False if not already_set(current_boot_options, system_image_file, kickstart_image_file): changed = True if not module.check_mode and changed == True: if device.device_type == 'nxos': timeout = 600 device.set_timeout(timeout) try: start_time = time.time() device.set_boot_options(system_image_file, kickstart=kickstart_image_file) except: pass elapsed_time = time.time() - start_time device.set_timeout(30) try: install_state = device.get_boot_options() except: install_state = {} while elapsed_time < timeout and not install_state: try: install_state = device.get_boot_options() except: time.sleep(10) elapsed_time += 10 else: device.set_boot_options(system_image_file, kickstart=kickstart_image_file) install_state = device.get_boot_options() if not already_set(install_state, system_image_file, kickstart_image_file): module.fail_json(msg='Install not successful', install_state=install_state) else: install_state = current_boot_options device.close() module.exit_json(changed=changed, install_state=install_state)
def main(): module = AnsibleModule(argument_spec=dict( platform=dict(choices=[ PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS ], required=False), host=dict(required=False), username=dict(required=False, type='str'), provider=dict(required=False, type='dict'), password=dict(required=False, type='str', no_log=True), secret=dict(required=False, no_log=True), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), remote_file=dict(required=False), local_file=dict(required=False), ), mutually_exclusive=[ ['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host', 'provider']], supports_check_mode=False) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') provider = module.params['provider'] or {} no_log = ['password', 'secret'] for param in no_log: if provider.get(param): module.no_log_values.update(return_values(provider[param])) # allow local params to override provider for param, pvalue in provider.items(): if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) remote_file = module.params['remote_file'] local_file = module.params['local_file'] argument_check = { 'host': host, 'username': username, 'platform': platform, 'password': password } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") device.open() if remote_file: remote_save_successful = device.save(remote_file) else: remote_save_successful = device.save() changed = remote_save_successful if local_file: device.backup_running_config(local_file) changed = True device.close() remote_file = remote_file or '(Startup Config)' module.exit_json(changed=changed, remote_save_successful=remote_save_successful, remote_file=remote_file, local_file=local_file)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[ PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS ], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), system_image_file=dict(required=True), kickstart_image_file=dict(required=False), ), mutually_exclusive=[ ['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=True) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) if device.device_type == PLATFORM_JUNOS: module.fail_json(msg='Install OS for Juniper not supported.') system_image_file = module.params['system_image_file'] kickstart_image_file = module.params['kickstart_image_file'] if kickstart_image_file == 'null': kickstart_image_file = None device.open() current_boot_options = device.get_boot_options() changed = False if not already_set(current_boot_options, system_image_file, kickstart_image_file): changed = True if not module.check_mode and changed == True: if device.device_type == 'nxos': timeout = 600 device.set_timeout(timeout) try: start_time = time.time() device.set_boot_options(system_image_file, kickstart=kickstart_image_file) except: pass elapsed_time = time.time() - start_time device.set_timeout(30) try: install_state = device.get_boot_options() except: install_state = {} while elapsed_time < timeout and not install_state: try: install_state = device.get_boot_options() except: time.sleep(10) elapsed_time += 10 else: device.set_boot_options(system_image_file, kickstart=kickstart_image_file) install_state = device.get_boot_options() if not already_set(install_state, system_image_file, kickstart_image_file): module.fail_json(msg='Install not successful', install_state=install_state) else: install_state = current_boot_options device.close() module.exit_json(changed=changed, install_state=install_state)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS], required=False), host=dict(required=False), username=dict(required=False, type='str'), provider=dict(required=False, type='dict'), password=dict(required=False, type='str', no_log=True), secret=dict(required=False, no_log=True), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), confirm=dict(required=False, default=False, type='bool', choices=BOOLEANS), timer=dict(requred=False, type='int'), timeout=dict(required=False, type='int', default=240) ), mutually_exclusive=[['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host', 'provider']], supports_check_mode=False ) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') provider = module.params['provider'] or {} # allow local params to override provider for param, pvalue in provider.items(): if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] argument_check = { 'host': host, 'username': username, 'platform': platform, 'password': password } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") kwargs = {} if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) confirm = module.params['confirm'] timer = module.params['timer'] timeout = module.params['timeout'] if not confirm: module.fail_json(msg='confirm must be set to true for this module to work.') supported_timer_platforms = [PLATFORM_IOS, PLATFORM_JUNOS] if timer is not None \ and device.device_type not in supported_timer_platforms: module.fail_json(msg='Timer parameter not supported on platform %s.' % platform) argument_check = { 'host': host, 'username': username, 'platform': platform, 'password': password } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") device.open() changed = False rebooted = False if timer is not None: device.reboot(confirm=True, timer=timer) else: device.reboot(confirm=True) time.sleep(10) reachable, atomic = check_device(module, username, password, host, timeout, kwargs) changed = True rebooted = True module.exit_json(changed=changed, rebooted=rebooted, reachable=reachable, atomic=atomic)
def main(): connection_argument_spec = dict( platform=dict(choices=[ PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS, PLATFORM_F5, PLATFORM_ASA ], required=False), host=dict(required=False), port=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str', no_log=True), secret=dict(required=False, type='str', no_log=True), use_keys=dict(required=False, default=False, type='bool'), key_file=dict(required=False, default=None), global_delay_factor=dict(default=1, required=False, type='int'), delay_factor=dict(default=1, required=False, type='int'), transport=dict(required=False, choices=['http', 'https']), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), ) base_argument_spec = dict( local_file=dict(required=False), remote_file=dict(required=False), file_system=dict(required=False), ) argument_spec = base_argument_spec argument_spec.update(connection_argument_spec) argument_spec["provider"] = dict(required=False, type="dict", options=connection_argument_spec) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host', 'provider']], supports_check_mode=True) provider = module.params['provider'] or {} # allow local params to override provider for param, pvalue in provider.items(): if module.params.get(param) != False: module.params[param] = module.params.get(param) or pvalue if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] global_delay_factor = int(module.params['global_delay_factor']) delay_factor = int(module.params['delay_factor']) secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret if global_delay_factor is not None: kwargs['global_delay_factor'] = global_delay_factor if delay_factor is not None: kwargs['delay_factor'] = delay_factor device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) local_file = module.params['local_file'] remote_file = module.params['remote_file'] file_system = module.params['file_system'] argument_check = { 'host': host, 'username': username, 'platform': platform, 'password': password, 'local_file': local_file } for key, val in argument_check.items(): if val is None: module.fail_json(msg=str(key) + " is required") device.open() changed = False transfer_status = 'No Transfer' file_exists = True if not os.path.isfile(local_file): module.fail_json(msg="Local file {} not found".format(local_file)) if file_system: remote_exists = device.file_copy_remote_exists(local_file, remote_file, file_system=file_system) else: remote_exists = device.file_copy_remote_exists(local_file, remote_file) if not remote_exists: changed = True file_exists = False if not module.check_mode and not file_exists: try: if file_system: device.file_copy(local_file, remote_file, file_system=file_system) else: device.file_copy(local_file, remote_file) transfer_status = 'Sent' except Exception as e: module.fail_json(msg=str(e)) try: device.close() atomic = True except: atomic = False if remote_file is None: remote_file = os.path.basename(local_file) module.exit_json(changed=changed, transfer_status=transfer_status, local_file=local_file, remote_file=remote_file, file_system=file_system, atomic=atomic)
def main(): module = AnsibleModule( argument_spec=dict( platform=dict(choices=[ PLATFORM_NXAPI, PLATFORM_IOS, PLATFORM_EAPI, PLATFORM_JUNOS ], required=False), host=dict(required=False), username=dict(required=False, type='str'), password=dict(required=False, type='str'), secret=dict(required=False), transport=dict(required=False, choices=['http', 'https']), port=dict(required=False, type='int'), ntc_host=dict(required=False), ntc_conf_file=dict(required=False), confirm=dict(required=False, default=False, type='bool', choices=BOOLEANS), timer=dict(requred=False, type='int'), ), mutually_exclusive=[ ['host', 'ntc_host'], ['ntc_host', 'secret'], ['ntc_host', 'transport'], ['ntc_host', 'port'], ['ntc_conf_file', 'secret'], ['ntc_conf_file', 'transport'], ['ntc_conf_file', 'port'], ], required_one_of=[['host', 'ntc_host']], required_together=[['host', 'username', 'password', 'platform']], supports_check_mode=False) if not HAS_PYNTC: module.fail_json(msg='pyntc Python library not found.') platform = module.params['platform'] host = module.params['host'] username = module.params['username'] password = module.params['password'] ntc_host = module.params['ntc_host'] ntc_conf_file = module.params['ntc_conf_file'] transport = module.params['transport'] port = module.params['port'] secret = module.params['secret'] if ntc_host is not None: device = ntc_device_by_name(ntc_host, ntc_conf_file) else: kwargs = {} if transport is not None: kwargs['transport'] = transport if port is not None: kwargs['port'] = port if secret is not None: kwargs['secret'] = secret device_type = platform device = ntc_device(device_type, host, username, password, **kwargs) confirm = module.params['confirm'] timer = module.params['timer'] if not confirm: module.fail_json( msg='confirm must be set to true for this module to work.') supported_timer_platforms = [PLATFORM_IOS, PLATFORM_JUNOS] if timer is not None \ and device.device_type not in supported_timer_platforms: module.fail_json(msg='Timer parameter not supported on platform %s.' % platform) device.open() changed = False rebooted = False if timer is not None: device.reboot(confirm=True, timer=timer) else: device.reboot(confirm=True) changed = True rebooted = True device.close() module.exit_json(changed=changed, rebooted=rebooted)