update ops_template module using refactored network shared modules

This updates the ops_template module to work with the network shared
modules introduced in Ansible 2.2

Tested with OpenSwitch 0.4.0
pull/18777/head
Peter Sprygada 8 years ago committed by Matt Clay
parent 54232c3b2d
commit 018cdf33a2

@ -97,40 +97,14 @@ responses:
""" """
import copy import copy
from ansible.module_utils.netcfg import NetworkConfig, dumps
def compare(this, other): from ansible.module_utils.openswitch import NetworkModule
parents = [item.text for item in this.parents]
for entry in other:
if this == entry:
return None
return this
def expand(obj, queue):
block = [item.raw for item in obj.parents]
block.append(obj.raw)
current_level = queue
for b in block:
if b not in current_level:
current_level[b] = collections.OrderedDict()
current_level = current_level[b]
for c in obj.children:
if c.raw not in current_level:
current_level[c.raw] = collections.OrderedDict()
def flatten(data, obj):
for k, v in data.items():
obj.append(k)
flatten(v, obj)
return obj
def get_config(module): def get_config(module):
config = module.params['config'] or dict() config = module.params['config'] or dict()
if not config and not module.params['force']: if not config and not module.params['force']:
config = module.config config = module.config.get_config()
return config return config
@ -170,7 +144,6 @@ def merge(changeset, config=None):
current_level[key] = value current_level[key] = value
return config return config
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
@ -184,18 +157,25 @@ def main():
mutually_exclusive = [('config', 'backup'), ('config', 'force')] mutually_exclusive = [('config', 'backup'), ('config', 'force')]
module = get_module(argument_spec=argument_spec, module = NetworkModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive, mutually_exclusive=mutually_exclusive,
supports_check_mode=True) 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) result = dict(changed=False)
contents = get_config(module) contents = get_config(module)
result['_backup'] = copy.deepcopy(module.config) result['_backup'] = contents
if module.params['transport'] in ['ssh', 'rest']: if module.params['transport'] in ['ssh', 'rest']:
config = contents config = contents
src = module.from_json(module.params['src'])
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) changeset = diff(src, config)
candidate = merge(changeset, config) candidate = merge(changeset, config)
@ -208,45 +188,32 @@ def main():
if changeset: if changeset:
if not module.check_mode: if not module.check_mode:
module.configure(config) module.config(config)
result['changed'] = True result['changed'] = True
else: else:
config = module.parse_config(config) candidate = NetworkConfig(contents=module.params['src'], indent=4)
candidate = module.parse_config(module.params['src'])
commands = collections.OrderedDict() if contents:
toplevel = [c.text for c in config] config = NetworkConfig(contents=contents, indent=4)
for line in candidate: if not module.params['force']:
if line.text in ['!', '']: commands = candidate.difference(config)
continue commands = dumps(commands, 'commands').split('\n')
commands = [str(c) for c in commands if c]
if not line.parents: else:
if line.text not in toplevel: commands = str(candidate).split('\n')
expand(line, commands)
else:
item = compare(line, config)
if item:
expand(item, commands)
commands = flatten(commands, list())
if commands: if commands:
if not module.check_mode: if not module.check_mode:
commands = [str(c).strip() for c in commands] response = module.config(commands)
response = module.configure(commands)
result['responses'] = response result['responses'] = response
result['changed'] = True result['changed'] = True
result['updates'] = commands result['updates'] = commands
module.exit_json(**result) module.exit_json(**result)
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
from ansible.module_utils.netcfg import *
from ansible.module_utils.shell import *
from ansible.module_utils.openswitch import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save