def main(): """main entry point for module execution """ argument_spec = dict( # { command: <str>, prompt: <str>, response: <str> } commands=dict(type='list', required=True), wait_for=dict(type='list', elements='str'), match=dict(default='all', choices=['all', 'any']), retries=dict(default=10, type='int'), interval=dict(default=1, type='int') ) argument_spec.update(os6_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) commands = parse_commands(module, warnings) result['warnings'] = warnings wait_for = module.params['wait_for'] or list() conditionals = [Conditional(c) for c in wait_for] retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] while retries > 0: responses = run_commands(module, commands) for item in list(conditionals): if item(responses): if match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(interval) retries -= 1 if conditionals: failed_conditions = [item.raw for item in conditionals] msg = 'One or more conditional statements have not been satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) result.update({ 'changed': False, 'stdout': responses, 'stdout_lines': list(to_lines(responses)) }) module.exit_json(**result)
def main(): """main entry point for module execution """ argument_spec = dict(gather_subset=dict(default=['!config'], type='list')) argument_spec.update(os6_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) gather_subset = module.params['gather_subset'] runable_subsets = set() exclude_subsets = set() for subset in gather_subset: if subset == 'all': runable_subsets.update(VALID_SUBSETS) continue if subset.startswith('!'): subset = subset[1:] if subset == 'all': exclude_subsets.update(VALID_SUBSETS) continue exclude = True else: exclude = False if subset not in VALID_SUBSETS: module.fail_json(msg='Bad subset') if exclude: exclude_subsets.add(subset) else: runable_subsets.add(subset) if not runable_subsets: runable_subsets.update(VALID_SUBSETS) runable_subsets.difference_update(exclude_subsets) runable_subsets.add('default') facts = dict() facts['gather_subset'] = list(runable_subsets) instances = list() for key in runable_subsets: instances.append(FACT_SUBSETS[key](module)) for inst in instances: inst.populate() facts.update(inst.facts) ansible_facts = dict() for key, value in iteritems(facts): key = 'ansible_net_%s' % key ansible_facts[key] = value warnings = list() check_args(module, warnings) module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
def main(): backup_spec = dict(filename=dict(), dir_path=dict(type='path')) argument_spec = dict(lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), src=dict(type='path'), before=dict(type='list'), after=dict(type='list'), match=dict( default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block']), update=dict(choices=['merge', 'check'], default='merge'), save=dict(type='bool', default=False), config=dict(), backup=dict(type='bool', default=False), backup_options=dict(type='dict', options=backup_spec)) argument_spec.update(os6_argument_spec) mutually_exclusive = [('lines', 'src'), ('parents', 'src')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) parents = module.params['parents'] or list() match = module.params['match'] replace = module.params['replace'] warnings = list() check_args(module, warnings) result = dict(changed=False, saved=False, warnings=warnings) candidate, want_banners = get_candidate(module) if module.params['backup']: if not module.check_mode: result['__backup__'] = get_config(module) commands = list() if any((module.params['lines'], module.params['src'])): if match != 'none': config, have_banners = get_running_config(module) config = NetworkConfig(contents=config, indent=0) if parents: config = get_sublevel_config(config, module) configobjs = candidate.difference(config, match=match, replace=replace) else: configobjs = candidate.items have_banners = {} diffbanners = diff_banners(want_banners, have_banners) banners = list() if diffbanners: banners.append(diffbanners) if configobjs or banners: commands = dumps(configobjs, 'commands') if ((isinstance(module.params['lines'], list)) and (isinstance(module.params['lines'][0], dict)) and set(['prompt', 'answer']).issubset( module.params['lines'][0])): cmd = { 'command': commands, 'prompt': module.params['lines'][0]['prompt'], 'answer': module.params['lines'][0]['answer'] } commands = [module.jsonify(cmd)] else: if commands: commands = commands.split('\n') if module.params['before']: commands[:0], before_banners = extract_banners( module.params['before']) if before_banners: banners.insert(0, before_banners) if module.params['after']: commands_after, after_banners = extract_banners( module.params['after']) commands.extend(commands_after) if after_banners: banners.insert(len(banners), after_banners) if not module.check_mode and module.params['update'] == 'merge': if commands: load_config(module, commands) if banners: result_banners = load_banners(module, banners) else: result_banners = [] result['changed'] = True result['commands'] = commands result['updates'] = commands if commands else [] result['banners'] = result_banners if result['banners']: result['updates'].extend(result_banners) if module.params['save']: result['changed'] = True if not module.check_mode: cmd = { 'command': 'copy running-config startup-config', 'prompt': r'\(y/n\)\s?$', 'answer': 'y' } run_commands(module, [cmd]) result['saved'] = True else: module.warn('Skipping command `copy running-config startup-config`' 'due to check_mode. Configuration not copied to ' 'non-volatile storage') module.exit_json(**result)