From 361f3999ea155a3d679763b01e87491974fbc6a9 Mon Sep 17 00:00:00 2001
From: Peter Sprygada <privateip@users.noreply.github.com>
Date: Thu, 22 Sep 2016 19:52:42 -0400
Subject: [PATCH] fixes issue where junos shared module was ignoring
 ssh_keyfile (#17712)

This fixes a problem with the Netconf transport in which the ssh keyfile
wasn't being used if it was defined.  The ref issue is filed against 2.1.1
but have been unable to replicate the problem in that version

ref: ansible/ansible-modules-core#4966
---
 lib/ansible/module_utils/junos.py | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/lib/ansible/module_utils/junos.py b/lib/ansible/module_utils/junos.py
index 5f7d3cc66dc..9cf36da4ce6 100644
--- a/lib/ansible/module_utils/junos.py
+++ b/lib/ansible/module_utils/junos.py
@@ -23,6 +23,7 @@ from distutils.version import LooseVersion
 
 from ansible.module_utils.pycompat24 import get_exception
 from ansible.module_utils.network import register_transport, to_list
+from ansible.module_utils.network import NetworkError
 from ansible.module_utils.shell import CliBase
 from ansible.module_utils.six import string_types
 
@@ -93,14 +94,22 @@ class Netconf(object):
 
     def connect(self, params, **kwargs):
         host = params['host']
-        port = params.get('port') or 830
 
-        user = params['username']
-        passwd = params['password']
+        kwargs = dict()
+        kwargs['port'] = params.get('port') or 830
+
+        kwargs['user'] = params['username']
+
+        if params['password']:
+            kwargs['passwd'] = params['password']
+
+        if params['ssh_keyfile']:
+            kwargs['ssh_private_key_file'] = params['ssh_keyfile']
+
+        kwargs['gather_facts'] = False
 
         try:
-            self.device = Device(host, user=user, passwd=passwd, port=port,
-                                 gather_facts=False)
+            self.device = Device(host, **kwargs)
             self.device.open()
         except ConnectError:
             exc = get_exception()