本文整理汇总了Python中ansible.module_utils.network.NetworkModule.from_json方法的典型用法代码示例。如果您正苦于以下问题:Python NetworkModule.from_json方法的具体用法?Python NetworkModule.from_json怎么用?Python NetworkModule.from_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ansible.module_utils.network.NetworkModule
的用法示例。
在下文中一共展示了NetworkModule.from_json方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import from_json [as 别名]
def main():
spec = dict(gather_subset=dict(default=["!config"], type="list"))
module = NetworkModule(argument_spec=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)
runner = CommandRunner(module)
instances = list()
for key in runable_subsets:
instances.append(FACT_SUBSETS[key](runner))
runner.run()
try:
for inst in instances:
inst.populate()
facts.update(inst.facts)
except Exception:
module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict()
for key, value in facts.items():
key = "ansible_net_%s" % key
ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts)
示例2: main
# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import from_json [as 别名]
def main():
spec = dict(
gather_subset=dict(default=['!config'], type='list'),
# the next two arguments are legacy from pre 2.2 ops_facts
# these will be deprecated and ultimately removed
config=dict(default=False, type='bool'),
endpoints=dict(type='list'),
transport=dict(default='cli', choices=['cli', 'rest'])
)
module = NetworkModule(argument_spec=spec, supports_check_mode=True)
gather_subset = module.params['gather_subset']
warnings = list()
check_args(module, warnings)
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')
runable_subsets.add('legacy')
facts = dict()
facts['gather_subset'] = list(runable_subsets)
runner = CommandRunner(module)
instances = list()
for key in runable_subsets:
instances.append(FACT_SUBSETS[key](module, runner))
if module.params['transport'] == 'cli':
runner.run()
try:
for inst in instances:
inst.populate()
facts.update(inst.facts)
except Exception:
module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict()
for key, value in iteritems(facts):
# this is to maintain capability with ops_facts 2.1
if key.startswith('_'):
ansible_facts[key[1:]] = value
else:
key = 'ansible_net_%s' % key
ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
示例3: main
# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import from_json [as 别名]
def main():
""" main entry point for module execution
"""
argument_spec = dict(
src=dict(type='str'),
force=dict(default=False, type='bool'),
backup=dict(default=False, type='bool'),
config=dict(type='dict'),
)
mutually_exclusive = [('config', 'backup'), ('config', 'force')]
module = NetworkModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
if not module.params['transport'] and not HAS_OPS:
module.fail_json(msg='unable to import ops.dc library')
result = dict(changed=False)
contents = get_config(module)
result['_backup'] = contents
if module.params['transport'] in ['ssh', 'rest']:
config = contents
try:
src = module.from_json(module.params['src'])
except ValueError:
module.fail_json(msg='unable to load src due to json parsing error')
changeset = diff(src, config)
candidate = merge(changeset, config)
updates = dict()
for path, key, new_value, old_value in changeset:
path = '%s.%s' % ('.'.join(path), key)
updates[path] = str(new_value)
result['updates'] = updates
if changeset:
if not module.check_mode:
module.config(config)
result['changed'] = True
else:
candidate = NetworkConfig(contents=module.params['src'], indent=4)
if contents:
config = NetworkConfig(contents=contents, indent=4)
if not module.params['force']:
commands = candidate.difference(config)
commands = dumps(commands, 'commands').split('\n')
commands = [str(c) for c in commands if c]
else:
commands = str(candidate).split('\n')
if commands:
if not module.check_mode:
response = module.config(commands)
result['responses'] = response
result['changed'] = True
result['updates'] = commands
module.exit_json(**result)
示例4: main
# 需要导入模块: from ansible.module_utils.network import NetworkModule [as 别名]
# 或者: from ansible.module_utils.network.NetworkModule import from_json [as 别名]
def main():
spec = dict(
gather_subset=dict(default=['!config'], type='list')
)
module = NetworkModule(argument_spec=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)
runner = CommandRunner(module)
instances = list()
for key in runable_subsets:
instances.append(FACT_SUBSETS[key](module, runner))
try:
runner.run()
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc), **exc.kwargs)
try:
for inst in instances:
inst.populate()
facts.update(inst.facts)
except Exception:
raise
module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict()
for key, value in iteritems(facts):
# this is to maintain capability with nxos_facts 2.1
if key.startswith('_'):
ansible_facts[key[1:]] = value
else:
key = 'ansible_net_%s' % key
ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts)