nautobot / nautobot-ansible

Ansible Collection for managing Nautobot Data
https://nautobot-ansible.readthedocs.io/en/latest/
GNU General Public License v3.0
45 stars 31 forks source link

unexpected keyword argument 'private_key_file' #14

Closed cs-1 closed 3 years ago

cs-1 commented 3 years ago
ISSUE TYPE
SOFTWARE VERSIONS
pynautobot

1.0.1

Ansible:

2.10.4

Nautobot:

v1.0.0b1 (developer branch from github 2021-03-08)

Collection:
Collection             Version
---------------------- -------
ansible.netcommon      1.4.1
ansible.posix          1.1.1
community.general      1.3.4
community.kubernetes   1.1.1
community.mysql        1.1.0
google.cloud           1.0.1
netbox.netbox          2.0.0
networktocode.nautobot 1.0.1
SUMMARY

I'm trying to convert a playbook that works fine using NetBox and the NetBox Ansible module to Nautobot. The data has successfully been migrated from NetBox to Nautobot. The playbook uses a lookup in a task which yields and "unexpected keyword" error.

STEPS TO REPRODUCE
- name: Query Nautobot for virtual chassis host information
  set_fact:
    tmp_nautobot_host_info: "{{ query('networktocode.nautobot.lookup', 'devices', api_filter='name__isw=' + inventory_hostname_short + '/', api_endpoint=nautobot.url, token=nautobot.token) }}"
EXPECTED RESULTS

The lookup should simply find all devices that contain the short inventory hostname + "/" and write them to tmp_nautobot_host_info.

ACTUAL RESULTS
fatal: [XXXXXXXX]: FAILED! => {}

MSG:

An unhandled exception occurred while running the lookup plugin 'networktocode.nautobot.lookup'. Error was a <class 'TypeError'>, original message: __init__() got an unexpected keyword argument 'private_key_file'
cs-1 commented 3 years ago

I just checked, in pynautobot's core/api.py there's no attribute for key_file or private_key_file. The lookup, however, seems to use the attribute anyhow:

https://github.com/nautobot/nautobot-ansible/blob/f410d806b5657d8acccd2b4b990b63c32f1cda2f/plugins/lookup/lookup.py#L320

Also, the lookup module references "secrets" attributes which aren't present either:

https://github.com/nautobot/nautobot-ansible/blob/f410d806b5657d8acccd2b4b990b63c32f1cda2f/plugins/lookup/lookup.py#L196-L197

Removing the private_key_file line and also both lines referring to secrets in the lookup module fixes the issue and the query works. However, I'm not entirely sure if this is a good fix but since Nautobot doesn't support NetBox's secrets / keyfile notion this seemed like the right thing to do. If the private_key_file is never used anywhere, all other remainders in plugings/lookup/lookup.py should be removed, too.

I've tried creating a patch which I'll attach to this comment. Please check it thoroughly if it makes sense.

--- plugins/lookup/lookup.py.orig   2021-03-08 09:55:16.000000000 +0100
+++ plugins/lookup/lookup.py    2021-03-08 09:51:52.000000000 +0100
@@ -32,7 +32,6 @@
     description:
         - Queries Nautobot via its API to return virtually any information
           capable of being held in Nautobot.
-        - If wanting to obtain the plaintext attribute of a secret, key_file must be provided.
     options:
         _terms:
             description:
@@ -68,10 +67,6 @@
                 - Whether or not to validate SSL of the Nautobot instance
             required: False
             default: True
-        key_file:
-            description:
-                - The location of the private key tied to user account.
-            required: False
         raw_data:
             description:
                 - Whether to return raw API data with the lookup/query or whether to return a key/value dict
@@ -106,12 +101,6 @@
                     api_filter='role=management tag=Dell'),
                     token='<redacted>') }}"

-# Obtain a secret for R1-device
-tasks:
-  - name: "Obtain secrets for R1-Device"
-    debug:
-      msg: "{{ query('networktocode.nautobot.lookup', 'secrets', api_filter='device=R1-Device', api_endpoint='http://localhost/', token='<redacted>', key_file='~/.ssh/id_rsa') }}"
-
 # Fetch bgp sessions for R1-device
 tasks:
   - name: "Obtain bgp sessions for R1-Device"
@@ -121,8 +110,6 @@
                      api_endpoint='http://localhost/',
                      token='<redacted>',
                      plugin='mycustomstuff') }}"
-
-      msg: "{{ query('networktocode.nautobot.lookup', 'secrets', api_filter='device=R1-Device', api_endpoint='http://localhost/', token='<redacted>', key_file='~/.ssh/id_rsa') }}"
 """

 RETURN = """
@@ -193,8 +180,6 @@
         "reports": {"endpoint": nautobot.extras.reports},
         "rirs": {"endpoint": nautobot.ipam.rirs},
         "roles": {"endpoint": nautobot.ipam.roles},
-        "secret-roles": {"endpoint": nautobot.secrets.secret_roles},
-        "secrets": {"endpoint": nautobot.secrets.secrets},
         "services": {"endpoint": nautobot.ipam.services},
         "sites": {"endpoint": nautobot.dcim.sites},
         "tags": {"endpoint": nautobot.extras.tags},
@@ -302,7 +287,6 @@
             or os.getenv("NAUTOBOT_URL")
         )
         ssl_verify = kwargs.get("validate_certs", True)
-        private_key_file = kwargs.get("key_file")
         api_filter = kwargs.get("api_filter")
         raw_return = kwargs.get("raw_data")
         plugin = kwargs.get("plugin")
@@ -317,12 +301,11 @@
             nautobot = pynautobot.api(
                 api_endpoint,
                 token=api_token if api_token else None,
-                private_key_file=private_key_file,
             )
             nautobot.http_session = session
-        except FileNotFoundError:
+        except Exception as e:
             raise AnsibleError(
-                "%s cannot be found. Please make sure file exists." % private_key_file
+                "Encountered error: " + e
             )

         results = []
@@ -375,4 +358,4 @@
                     result = {key: data}
                     results.extend(self._flatten_hash_to_list(result))

-        return results
\ No newline at end of file
+        return results