Skip to content

Commit 2521d5d

Browse files
Bugfix: Update modules/lookup plugin to work with pynetbox >=5.0.0 (#269)
1 parent c39aa39 commit 2521d5d

File tree

7 files changed

+21
-11
lines changed

7 files changed

+21
-11
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- cd ..
3131
- pip install -U pip "setuptools>=46.1.3"
3232
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0 "coverage<5"
33-
- pip install pynetbox==4.3.3 cryptography codecov jmespath jsondiff ansible
33+
- pip install pynetbox cryptography codecov jmespath jsondiff ansible
3434

3535
# Stick to python 3.7 instead of 3.8, as ansible-test sanity is not compatible with 3.7
3636
# https://github.com/ansible/ansible/issues/67118
@@ -49,7 +49,7 @@ jobs:
4949
- cd ..
5050
- pip install -U pip "setuptools>=46.1.3"
5151
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0 "coverage<5"
52-
- pip install pynetbox==4.3.3 cryptography codecov jmespath jsondiff ansible
52+
- pip install pynetbox cryptography codecov jmespath jsondiff ansible
5353

5454
# Latest development versions of Netbox and Ansible, newest Python
5555
# This may be broken sometimes by changes in the netbox & ansible projects
@@ -67,7 +67,7 @@ jobs:
6767
- cd ..
6868
- pip install -U pip "setuptools>=46.1.3"
6969
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0 "coverage<5"
70-
- pip install pynetbox==4.3.3 cryptography jmespath jsondiff
70+
- pip install pynetbox cryptography jmespath jsondiff
7171
- git clone https://github.com/ansible/ansible.git
7272
- cd ansible
7373
- source hacking/env-setup

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ To keep the code simple, we only officially support the two latest releases of N
1111
## Requirements
1212

1313
- NetBox 2.6+ or the two latest NetBox releases
14-
- **pynetbox 4.2.5+**
1514
- Python 3.6+
15+
- Python modules: **pynetbox 5.0.4+**
1616
- Ansible 2.9+
17-
- NetBox write-enabled token
17+
- NetBox write-enabled token when using modules or read-only token for `nb_lookup/nb_inventory`
1818

1919
## Existing Modules
2020

plugins/lookup/nb_lookup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ansible.utils.display import Display
2020

2121
import pynetbox
22+
import requests
2223

2324
__metaclass__ = type
2425

@@ -204,12 +205,15 @@ def run(self, terms, variables=None, **kwargs):
204205
terms = [terms]
205206

206207
try:
208+
session = requests.Session()
209+
session.verify = netbox_ssl_verify
210+
207211
netbox = pynetbox.api(
208212
netbox_api_endpoint,
209213
token=netbox_api_token if netbox_api_token else None,
210-
ssl_verify=netbox_ssl_verify,
211214
private_key_file=netbox_private_key_file,
212215
)
216+
netbox.http_session = session
213217
except FileNotFoundError:
214218
raise AnsibleError(
215219
"%s cannot be found. Please make sure file exists."

plugins/module_utils/netbox_ipam.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def _get_new_available_ip_address(self, nb_app, data, endpoint_name):
8585
self.nb_object, diff = self._create_netbox_object(
8686
prefix.available_ips, data
8787
)
88+
self.nb_object = self.nb_object.serialize()
8889
self.result["changed"] = True
8990
self.result["msg"] = "%s %s created" % (
9091
endpoint_name,
@@ -110,6 +111,7 @@ def _get_new_available_prefix(self, data, endpoint_name):
110111
self.nb_object, diff = self._create_netbox_object(
111112
self.nb_object.available_prefixes, data
112113
)
114+
self.nb_object = self.nb_object.serialize()
113115
self.result["changed"] = True
114116
self.result["msg"] = "%s %s created" % (
115117
endpoint_name,

plugins/module_utils/netbox_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
PYNETBOX_IMP_ERR = None
2727
try:
2828
import pynetbox
29+
import requests
2930

3031
HAS_PYNETBOX = True
3132
except ImportError:
@@ -459,7 +460,10 @@ def __init__(self, module, endpoint, nb_client=None):
459460

460461
def _connect_netbox_api(self, url, token, ssl_verify):
461462
try:
462-
nb = pynetbox.api(url, token=token, ssl_verify=ssl_verify)
463+
session = requests.Session()
464+
session.verify = ssl_verify
465+
nb = pynetbox.api(url, token=token)
466+
nb.http_session = session
463467
try:
464468
self.version = float(nb.version)
465469
except AttributeError:

tests/integration/targets/latest/tasks/netbox_ip_address.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@
220220
- test_ten['diff']['after']['state'] == "present"
221221
- test_ten['msg'] == "ip_address 10.10.0.1/16 created"
222222
- test_ten['ip_address']['address'] == "10.10.0.1/16"
223-
- test_ten['ip_address']['family']['value'] == 4
224-
- test_ten['ip_address']['interface']['id'] == 4
223+
- test_ten['ip_address']['family'] == 4
224+
- test_ten['ip_address']['interface'] == 4
225225

226226
- name: "11 - Create IP address on GigabitEthernet2 - test100 - State: present"
227227
netbox.netbox.netbox_ip_address:

tests/integration/targets/latest/tasks/netbox_prefix.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@
221221
- test_ten['diff']['after']['state'] == "present"
222222
- test_ten['msg'] == "prefix 10.157.0.0/24 created"
223223
- test_ten['prefix']['prefix'] == "10.157.0.0/24"
224-
- test_ten['prefix']['site']['id'] == 1
225-
- test_ten['prefix']['vrf']['id'] == 1
224+
- test_ten['prefix']['site'] == 1
225+
- test_ten['prefix']['vrf'] == 1
226226

227227
- name: "11 - Get a new /24 inside 10.156.0.0/19 within Netbox"
228228
netbox.netbox.netbox_prefix:

0 commit comments

Comments
 (0)