community.general/lib/ansible/modules/network/cloudengine/ce_netconf.py
xuxiaowei 1017f15c38 fix bugs for ce (#54750)
* Update ce.py

            while to_text(out, errors='surrogate_then_replace').strip().endswith(']'):
                display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr)
                conn.exec_command('return')
                out = conn.get_prompt()

connetion has no send_command function and ce device has no 'exit' command to return user-view(a correct context),but 'return' .command.

* Add files via upload

Some bugs fix.

* Add files via upload

fix some bugs

* fix a bug for ce_command

Running a command with prompt via ce_command, It doesn't work.The reason is that the key word for network_cli recognition is answer not response.

* fix bugs

fix bugs for ce modules

* Update ce.py

* Delete ce_ftp.py

need modify

* Delete ce_lacp.py

* Add files via upload

* Delete ce_aaa_server.py

* Delete ce_aaa_server_host.py

* Compatible with Python 3

Compatible with Python 3 and fix bugs for ce

* Update ce_aaa_server.py

* Add files via upload

modify doc

* Add files via upload

Compatible with Python 3 and fix bugs

* Add files via upload

Compatible with Python 3 and fix bugs

* Add files via upload

Cancellation of change

* Update ce_netconf.py

It is a bug that response has no xml attribute:line 183

* Add files via upload

* Add files via upload

Compatible with Python 3 and fix bugs

* updatp ce_config.py

a bug for this module.
2019-04-17 12:51:58 +05:30

200 lines
5.8 KiB
Python

#!/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 <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
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 (@QijunPan)
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: '<filter type=\"subtree\">
<vlan xmlns=\"http://www.huawei.com/netconf/vrp\" content-version=\"1.0\" format-version=\"1.0\">
<vlans>
<vlan>
<vlanId>10</vlanId>
<vlanif>
<ifName></ifName>
<cfgBand></cfgBand>
<dampTime></dampTime>
</vlanif>
</vlan>
</vlans>
</vlan>
</filter>'
provider: "{{ cli }}"
- name: "Netconf edit-config operation"
ce_netconf:
rpc: edit-config
cfg_xml: '<config>
<aaa xmlns=\"http://www.huawei.com/netconf/vrp\" content-version=\"1.0\" format-version=\"1.0\">
<authenticationSchemes>
<authenticationScheme operation=\"create\">
<authenSchemeName>default_wdz</authenSchemeName>
<firstAuthenMode>local</firstAuthenMode>
<secondAuthenMode>invalid</secondAuthenMode>
</authenticationScheme>
</authenticationSchemes>
</aaa>
</config>'
provider: "{{ cli }}"
- name: "Netconf execute-action operation"
ce_netconf:
rpc: execute-action
cfg_xml: '<action>
<l2mc xmlns=\"http://www.huawei.com/netconf/vrp\" content-version=\"1.0\" format-version=\"1.0\">
<l2McResetAllVlanStatis>
<addrFamily>ipv4unicast</addrFamily>
</l2McResetAllVlanStatis>
</l2mc>
</action>'
provider: "{{ cli }}"
'''
RETURN = '''
changed:
description: check to see if a change was made on the device
returned: always
type: bool
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.network.cloudengine.ce import get_nc_config, set_nc_config
from ansible.module_utils.network.cloudengine.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 "<data/>" in response:
end_state["result"] = "<data/>"
else:
tmp1 = response.split(r"<data>")
tmp2 = tmp1[1].split(r"</data>")
result = tmp2[0].split("\n")
end_state["result"] = result
elif rpc == "edit-config":
response = set_nc_config(module, cfg_xml)
if "<ok/>" 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 "<ok/>" 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 "<data/>" in response:
end_state["result"] = "<data/>"
else:
tmp1 = response.split(r"<data>")
tmp2 = tmp1[1].split(r"</data>")
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()