bug fixes in junos_netconf module (#4924)

* fixes exception thrown when sending commands to device
* fixes exception thrown when retrieving current resource instance
* fixes issue where netconf would be configured in some instances when state
  was set to absent
* now returns the command string sent to the remote device
* fixes argument name to be netconf_port with alias to listens_on
This commit is contained in:
Peter Sprygada 2016-09-19 22:26:56 -04:00 committed by Matt Clay
parent 319b2b28cb
commit 0acab8bd19

View file

@ -30,13 +30,15 @@ description:
resources as defined in RFC 6242. resources as defined in RFC 6242.
extends_documentation_fragment: junos extends_documentation_fragment: junos
options: options:
listens_on: netconf_port:
description: description:
- This argument specifies the port the netconf service should - This argument specifies the port the netconf service should
listen on for SSH connections. The default port as defined listen on for SSH connections. The default port as defined
in RFC 6242 is 830. in RFC 6242 is 830.
required: false required: false
default: 830 default: 830
aliases: ['listens_on']
version_added: "2.2"
state: state:
description: description:
- Specifies the state of the M(junos_netconf) resource on - Specifies the state of the M(junos_netconf) resource on
@ -53,29 +55,37 @@ EXAMPLES = """
# Note: examples below use the following provider dict to handle # Note: examples below use the following provider dict to handle
# transport and authentication to the node. # transport and authentication to the node.
vars: vars:
netconf: cli:
host: "{{ inventory_hostname }}" host: "{{ inventory_hostname }}"
username: ansible username: ansible
password: Ansible password: Ansible
transport: netconf transport: cli
- name: enable netconf service on port 830 - name: enable netconf service on port 830
junos_netconf: junos_netconf:
listens_on: 830 listens_on: 830
state: present state: present
provider: "{{ netconf }}" provider: "{{ cli }}"
- name: disable netconf service - name: disable netconf service
junos_netconf: junos_netconf:
state: absent state: absent
provider: "{{ netconf }}" provider: "{{ cli }}"
""" """
RETURN = """ RETURN = """
commands:
description: Returns the command sent to the remote device
returned: when changed is True
type: str
sample: 'set system services netconf ssh port 830'
""" """
import re import re
from ansible.module_utils.junos import NetworkModule import ansible.module_utils.junos
from ansible.module_utils.basic import get_exception
from ansible.module_utils.network import NetworkModule, NetworkError
def parse_port(config): def parse_port(config):
match = re.search(r'port (\d+)', config) match = re.search(r'port (\d+)', config)
@ -84,7 +94,7 @@ def parse_port(config):
def get_instance(module): def get_instance(module):
cmd = 'show configuration system services netconf' cmd = 'show configuration system services netconf'
cfg = module.run_commands(cmd)[0] cfg = module.cli(cmd)[0]
result = dict(state='absent') result = dict(state='absent')
if cfg: if cfg:
result = dict(state='present') result = dict(state='present')
@ -96,7 +106,7 @@ def main():
""" """
argument_spec = dict( argument_spec = dict(
listens_on=dict(type='int', default=830), netconf_port=dict(type='int', default=830, aliases=['listens_on']),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),
transport=dict(default='cli', choices=['cli']) transport=dict(default='cli', choices=['cli'])
) )
@ -105,25 +115,31 @@ def main():
supports_check_mode=True) supports_check_mode=True)
state = module.params['state'] state = module.params['state']
port = module.params['listens_on'] port = module.params['netconf_port']
result = dict(changed=False) result = dict(changed=False)
instance = get_instance(module) instance = get_instance(module)
commands = None
if state == 'present' and instance.get('state') == 'absent': if state == 'present' and instance.get('state') == 'absent':
commands = 'set system services netconf ssh port %s' % port 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': elif state == 'absent' and instance.get('state') == 'present':
commands = 'delete system services netconf' commands = 'delete system services netconf'
elif port != instance.get('port'): else:
commands = 'set system services netconf ssh port %s' % port commands = None
if commands: if commands:
if not module.check_mode: if not module.check_mode:
comment = 'configuration updated by junos_netconf' try:
module.config(commands, comment=comment) 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['changed'] = True
result['commands'] = commands
module.exit_json(**result) module.exit_json(**result)