本文整理匯總了Python中ansible.module_utils.network.NetworkModule.config方法的典型用法代碼示例。如果您正苦於以下問題:Python NetworkModule.config方法的具體用法?Python NetworkModule.config怎麽用?Python NetworkModule.config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ansible.module_utils.network.NetworkModule
的用法示例。
在下文中一共展示了NetworkModule.config方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: main
# 需要導入模塊: from ansible.module_utils.network import NetworkModule [as 別名]
# 或者: from ansible.module_utils.network.NetworkModule import config [as 別名]
def main():
"""main entry point for module execution
"""
argument_spec = dict(
netconf_port=dict(type='int', default=830, aliases=['listens_on']),
state=dict(default='present', choices=['present', 'absent']),
transport=dict(default='cli', choices=['cli'])
)
module = NetworkModule(argument_spec=argument_spec,
supports_check_mode=True)
state = module.params['state']
port = module.params['netconf_port']
result = dict(changed=False)
instance = get_instance(module)
if state == 'present' and instance.get('state') == 'absent':
commands = 'set system services netconf ssh port %s' % port
elif state == 'present' and port != instance.get('port'):
commands = 'set system services netconf ssh port %s' % port
elif state == 'absent' and instance.get('state') == 'present':
commands = 'delete system services netconf'
else:
commands = None
if commands:
if not module.check_mode:
try:
comment = 'configuration updated by junos_netconf'
module.config(commands, comment=comment)
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc), **exc.kwargs)
result['changed'] = True
result['commands'] = commands
module.exit_json(**result)
示例2: main
# 需要導入模塊: from ansible.module_utils.network import NetworkModule [as 別名]
# 或者: from ansible.module_utils.network.NetworkModule import config [as 別名]
def main():
""" main entry point for module execution
"""
argument_spec = dict(
src=dict(),
force=dict(default=False, type='bool'),
include_defaults=dict(default=True, type='bool'),
backup=dict(default=False, type='bool'),
config=dict(),
)
mutually_exclusive = [('config', 'backup'), ('config', 'force')]
module = NetworkModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
result = dict(changed=False)
candidate = NetworkConfig(contents=module.params['src'], indent=2)
contents = get_config(module)
if contents:
config = NetworkConfig(contents=contents, indent=2)
result['_backup'] = str(contents)
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)
示例3: main
# 需要導入模塊: from ansible.module_utils.network import NetworkModule [as 別名]
# 或者: from ansible.module_utils.network.NetworkModule import config [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)