From fbeec055a4559aec22e093adf86091b44179d0bf Mon Sep 17 00:00:00 2001 From: QijunPan Date: Wed, 17 May 2017 05:43:33 +0800 Subject: [PATCH] Contributing lib/ansible/modules/network/cloudengine/ce_netconf.py module to manage HUAWEI data center CloudEngine (#22056) * add ce_netconf add ce_netconf * update * upgrade netconf * fix review issues * fix review issue. --- .../modules/network/cloudengine/ce_netconf.py | 200 ++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 lib/ansible/modules/network/cloudengine/ce_netconf.py diff --git a/lib/ansible/modules/network/cloudengine/ce_netconf.py b/lib/ansible/modules/network/cloudengine/ce_netconf.py new file mode 100644 index 00000000000..c2f3f72eb34 --- /dev/null +++ b/lib/ansible/modules/network/cloudengine/ce_netconf.py @@ -0,0 +1,200 @@ +#!/usr/bin/python +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +# + +ANSIBLE_METADATA = {'status': ['preview'], + 'supported_by': 'community', + 'metadata_version': '1.0'} + +DOCUMENTATION = ''' +--- +module: ce_netconf +version_added: "2.4" +short_description: Run an arbitrary netconf command on HUAWEI CloudEngine switches. +description: + - Sends an arbitrary netconf command on HUAWEI CloudEngine switches. +author: + - wangdezhuang (@CloudEngine-Ansible) +options: + rpc: + description: + - The type of rpc. + required: true + choices: ['get', 'edit-config', 'execute-action', 'execute-cli'] + cfg_xml: + description: + - The config xml string. + required: true +''' + +EXAMPLES = ''' + +- name: CloudEngine netconf test + hosts: cloudengine + connection: local + gather_facts: no + vars: + cli: + host: "{{ inventory_hostname }}" + port: "{{ ansible_ssh_port }}" + username: "{{ username }}" + password: "{{ password }}" + transport: cli + + tasks: + + - name: "Netconf get operation" + ce_netconf: + rpc: get + cfg_xml: ' + + + + 10 + + + + + + + + + ' + provider: "{{ cli }}" + + - name: "Netconf edit-config operation" + ce_netconf: + rpc: edit-config + cfg_xml: ' + + + + default_wdz + local + invalid + + + + ' + provider: "{{ cli }}" + + - name: "Netconf execute-action operation" + ce_netconf: + rpc: execute-action + cfg_xml: ' + + + ipv4unicast + + + ' + provider: "{{ cli }}" +''' + +RETURN = ''' +changed: + description: check to see if a change was made on the device + returned: always + type: boolean + sample: true +end_state: + description: k/v pairs of aaa params after module execution + returned: always + type: dict + sample: {"result": ["ok"]} +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.ce import get_nc_config, set_nc_config +from ansible.module_utils.ce import execute_nc_action, ce_argument_spec, execute_nc_cli + + +def main(): + """ main """ + + argument_spec = dict( + rpc=dict(choices=['get', 'edit-config', + 'execute-action', 'execute-cli'], required=True), + cfg_xml=dict(required=True) + ) + + argument_spec.update(ce_argument_spec) + module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) + + rpc = module.params['rpc'] + cfg_xml = module.params['cfg_xml'] + changed = False + end_state = dict() + + if rpc == "get": + + response = get_nc_config(module, cfg_xml) + + if "" in response: + end_state["result"] = "" + else: + tmp1 = response.split(r"") + tmp2 = tmp1[1].split(r"") + result = tmp2[0].split("\n") + + end_state["result"] = result + + elif rpc == "edit-config": + + response = set_nc_config(module, cfg_xml) + + if "" not in response: + module.fail_json(msg='rpc edit-config failed.') + + changed = True + end_state["result"] = "ok" + + elif rpc == "execute-action": + + response = execute_nc_action(module, cfg_xml) + + if "" not in response: + module.fail_json(msg='rpc execute-action failed.') + + changed = True + end_state["result"] = "ok" + + elif rpc == "execute-cli": + + response = execute_nc_cli(module, cfg_xml) + + if "" in response: + end_state["result"] = "" + else: + tmp1 = response.xml.split(r"") + tmp2 = tmp1[1].split(r"") + result = tmp2[0].split("\n") + + end_state["result"] = result + + else: + module.fail_json(msg='please input correct rpc.') + + results = dict() + results['changed'] = changed + results['end_state'] = end_state + + module.exit_json(**results) + + +if __name__ == '__main__': + main()