mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
implements python logging for network connection plugin (#22817)
* enables logging for network_cli and paramiko * enables logging for ansible-connection * enabled logging for netconf connection
This commit is contained in:
parent
c254618d4f
commit
ed7cace425
3 changed files with 61 additions and 45 deletions
|
@ -23,6 +23,7 @@ import re
|
|||
import socket
|
||||
import json
|
||||
import signal
|
||||
import logging
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleConnectionFailure, AnsibleError
|
||||
|
@ -37,11 +38,8 @@ try:
|
|||
except ImportError:
|
||||
raise AnsibleError("ncclient is not installed")
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
except ImportError:
|
||||
from ansible.utils.display import Display
|
||||
display = Display()
|
||||
logger = logging.getLogger('ansible.netconf')
|
||||
logging.getLogger('ncclient').setLevel(logging.INFO)
|
||||
|
||||
class Connection(ConnectionBase):
|
||||
''' NetConf connections '''
|
||||
|
@ -53,14 +51,20 @@ class Connection(ConnectionBase):
|
|||
super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
|
||||
|
||||
self._network_os = self._play_context.network_os or 'default'
|
||||
display.vvv('network_os is set to %s' % self._network_os, play_context.remote_addr)
|
||||
self.log('network_os is set to %s' % self._network_os)
|
||||
|
||||
self._manager = None
|
||||
self._connected = False
|
||||
|
||||
def log(self, msg):
|
||||
msg = 'h=%s u=%s %s' % (self._play_context.remote_addr, self._play_context.remote_user, msg)
|
||||
logger.debug(msg)
|
||||
|
||||
def _connect(self):
|
||||
super(Connection, self)._connect()
|
||||
|
||||
self.log('ssh connection done, stating ncclient')
|
||||
|
||||
allow_agent = True
|
||||
if self._play_context.password is not None:
|
||||
allow_agent = False
|
||||
|
@ -69,6 +73,9 @@ class Connection(ConnectionBase):
|
|||
if self._play_context.private_key_file:
|
||||
key_filename = os.path.expanduser(self._play_context.private_key_file)
|
||||
|
||||
if not self._network_os:
|
||||
raise AnsibleConnectionError('network_os must be set for netconf connections')
|
||||
|
||||
try:
|
||||
self._manager = manager.connect(
|
||||
host=self._play_context.remote_addr,
|
||||
|
@ -88,6 +95,8 @@ class Connection(ConnectionBase):
|
|||
if not self._manager.connected:
|
||||
return (1, '', 'not connected')
|
||||
|
||||
self.log('ncclient manager object created successfully')
|
||||
|
||||
self._connected = True
|
||||
return (0, self._manager.session_id, '')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue