Skip to content

Commit c6df6be

Browse files
authored
Feature: Added module for FHRP groups (#957)
* Feature: Added module for FHRP groups * Added tests for the FHRP groups module * Updated documentation for fhrp_group module
1 parent 3477e82 commit c6df6be

File tree

9 files changed

+465
-0
lines changed

9 files changed

+465
-0
lines changed

plugins/module_utils/netbox_ipam.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
NB_AGGREGATES = "aggregates"
2222
NB_ASNS = "asns"
23+
NB_FHRP_GROUPS = "fhrp_groups"
2324
NB_IP_ADDRESSES = "ip_addresses"
2425
NB_PREFIXES = "prefixes"
2526
NB_IPAM_ROLES = "roles"
@@ -150,6 +151,7 @@ def run(self):
150151
Supported endpoints:
151152
- aggregates
152153
- asns
154+
- fhrp_groups
153155
- ipam_roles
154156
- ip_addresses
155157
- l2vpns
@@ -186,6 +188,8 @@ def run(self):
186188
name = data.get("prefix")
187189
elif self.endpoint == "asns":
188190
name = data.get("asn")
191+
elif self.endpoint == "fhrp_groups":
192+
name = data.get("group_id")
189193
else:
190194
name = data.get("name")
191195

plugins/module_utils/netbox_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
ipam=[
8888
"aggregates",
8989
"asns",
90+
"fhrp_groups",
9091
"ip_addresses",
9192
"l2vpns",
9293
"l2vpn_terminations",
@@ -130,6 +131,7 @@
130131
device_type="slug",
131132
export_targets="name",
132133
export_template="name",
134+
fhrp_groups="group_id",
133135
group="slug",
134136
installed_device="name",
135137
inventory_item_role="name",
@@ -313,6 +315,7 @@
313315
"device_roles": "device_role",
314316
"device_types": "device_type",
315317
"export_templates": "export_template",
318+
"fhrp_groups": "fhrp_group",
316319
"front_ports": "front_port",
317320
"front_port_templates": "front_port_template",
318321
"interfaces": "interface",
@@ -413,6 +416,9 @@
413416
"device_role": set(["slug"]),
414417
"device_type": set(["slug"]),
415418
"export_template": set(["name"]),
419+
"fhrp_group": set(
420+
["id", "group_id", "interface_type", "device", "virtual_machine"]
421+
),
416422
"front_port": set(["name", "device", "rear_port"]),
417423
"front_port_template": set(["name", "device_type", "rear_port"]),
418424
"installed_device": set(["name"]),

plugins/modules/netbox_fhrp_group.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2023, Andrii Konts (@andrii-konts) <andrew.konts@uk2group.com>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
from __future__ import absolute_import, division, print_function
7+
8+
__metaclass__ = type
9+
10+
DOCUMENTATION = r"""
11+
---
12+
module: netbox_fhrp_group
13+
short_description: Create, update or delete FHRP groups within NetBox
14+
description:
15+
- Creates, updates or removes FHRP groups from NetBox
16+
notes:
17+
- Tags should be defined as a YAML list
18+
- This should be ran with connection C(local) and hosts C(localhost)
19+
author:
20+
- Andrii Konts (@andrii-konts)
21+
requirements:
22+
- pynetbox
23+
seealso:
24+
- name: FHRP Group Model reference
25+
description: NetBox Documentation for FHRP Group Model.
26+
link: https://docs.netbox.dev/en/stable/models/ipam/fhrpgroup/
27+
version_added: '3.12.0'
28+
extends_documentation_fragment:
29+
- netbox.netbox.common
30+
options:
31+
data:
32+
type: dict
33+
description:
34+
- Defines the FHRP group configuration
35+
suboptions:
36+
protocol:
37+
description:
38+
- Protocol
39+
required: False
40+
type: str
41+
choices:
42+
- vrrp2
43+
- vrrp3
44+
- carp
45+
- clusterxl
46+
- hsrp
47+
- glbp
48+
- other
49+
group_id:
50+
description:
51+
- Group ID (0 .. 32767)
52+
type: int
53+
required: true
54+
auth_type:
55+
description:
56+
- Authentication type
57+
choices:
58+
- plaintext
59+
- md5
60+
type: str
61+
auth_key:
62+
description:
63+
- Authentication key (max length 255)
64+
type: str
65+
description:
66+
description:
67+
- Description (max length 200)
68+
required: false
69+
type: str
70+
tags:
71+
description:
72+
- Any tags that the FHRP group may need to be associated with
73+
required: false
74+
type: list
75+
elements: raw
76+
custom_fields:
77+
description:
78+
- Must exist in NetBox
79+
required: false
80+
type: dict
81+
required: true
82+
"""
83+
84+
EXAMPLES = r"""
85+
- hosts: localhost
86+
connection: local
87+
module_defaults:
88+
group/netbox.netbox.netbox:
89+
netbox_url: "http://netbox.local"
90+
netbox_token: "thisIsMyToken"
91+
92+
tasks:
93+
- name: "Create FHRP group within netbox"
94+
netbox.netbox.netbox_fhrp_group:
95+
data:
96+
protocol: "glbp"
97+
group_id: 111
98+
auth_type: md5
99+
auth_key: 11111
100+
description: test FHRP group
101+
state: present
102+
103+
- name: Delete FHRP group within netbox
104+
netbox.netbox.netbox_fhrp_group:
105+
data:
106+
group_id: 111
107+
state: absent
108+
"""
109+
110+
RETURN = r"""
111+
fhrp_group:
112+
description: Serialized object as created or already existent within NetBox
113+
returned: success (when I(state=present))
114+
type: dict
115+
msg:
116+
description: Message indicating failure or info about what has been achieved
117+
returned: always
118+
type: str
119+
"""
120+
121+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
122+
NetboxAnsibleModule,
123+
NETBOX_ARG_SPEC,
124+
)
125+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import (
126+
NetboxIpamModule,
127+
NB_FHRP_GROUPS,
128+
)
129+
130+
from copy import deepcopy
131+
132+
133+
def main():
134+
"""
135+
Main entry point for module execution
136+
"""
137+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
138+
argument_spec.update(
139+
dict(
140+
data=dict(
141+
type="dict",
142+
required=True,
143+
options=dict(
144+
protocol=dict(
145+
type="str",
146+
choices=[
147+
"vrrp2",
148+
"vrrp3",
149+
"carp",
150+
"clusterxl",
151+
"hsrp",
152+
"glbp",
153+
"other",
154+
],
155+
),
156+
group_id=dict(required=True, type="int"),
157+
auth_type=dict(type="str", choices=["plaintext", "md5"]),
158+
auth_key=dict(type="str", no_log=True),
159+
description=dict(type="str"),
160+
tags=dict(required=False, type="list", elements="raw"),
161+
custom_fields=dict(required=False, type="dict"),
162+
),
163+
),
164+
)
165+
)
166+
required_if = [("state", "present", ["protocol"])]
167+
module = NetboxAnsibleModule(argument_spec=argument_spec, required_if=required_if)
168+
netbox_fhrp_group = NetboxIpamModule(module, NB_FHRP_GROUPS)
169+
netbox_fhrp_group.run()
170+
171+
172+
if __name__ == "__main__":
173+
main()

tests/integration/targets/v3.2/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,6 @@
238238

239239
- name: "NETBOX_ASN TESTS"
240240
include_tasks: "netbox_asn.yml"
241+
242+
- name: "NETBOX_FHRP_GROUP TESTS"
243+
include_tasks: "netbox_fhrp_group.yml"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
##
3+
##
4+
### NETBOX_FHRP_GROUP
5+
##
6+
##
7+
- name: "FHRP group 1: Test FHRP group creation"
8+
netbox.netbox.netbox_fhrp_group:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
protocol: "glbp"
13+
group_id: 111
14+
state: present
15+
register: test_one
16+
17+
- name: "FHRP group: ASSERT - Necessary info creation"
18+
ansible.builtin.assert:
19+
that:
20+
- test_one is changed
21+
- test_one['diff']['before']['state'] == "absent"
22+
- test_one['diff']['after']['state'] == "present"
23+
- test_one['fhrp_group']['group_id'] == 111
24+
- test_one['fhrp_group']['protocol'] == "glbp"
25+
- test_one['msg'] == "fhrp_group 111 created"
26+
27+
- name: "FHRP group 2: Create duplicate"
28+
netbox.netbox.netbox_fhrp_group:
29+
netbox_url: http://localhost:32768
30+
netbox_token: 0123456789abcdef0123456789abcdef01234567
31+
data:
32+
protocol: "glbp"
33+
group_id: 111
34+
state: present
35+
register: test_two
36+
37+
- name: "FHRP group 2: ASSERT - Create duplicate"
38+
ansible.builtin.assert:
39+
that:
40+
- not test_two['changed']
41+
- test_two['fhrp_group']['group_id'] == 111
42+
- test_two['fhrp_group']['protocol'] == "glbp"
43+
- test_two['msg'] == "fhrp_group 111 already exists"
44+
45+
- name: "FHRP group 3: Update FHRP group with other fields"
46+
netbox.netbox.netbox_fhrp_group:
47+
netbox_url: http://localhost:32768
48+
netbox_token: 0123456789abcdef0123456789abcdef01234567
49+
data:
50+
protocol: "glbp"
51+
group_id: 111
52+
auth_type: md5
53+
auth_key: 11111
54+
description: Test description
55+
tags:
56+
- "Schnozzberry"
57+
state: present
58+
register: test_three
59+
60+
- name: "FHRP group 3: ASSERT - Update FHRP group with other fields"
61+
ansible.builtin.assert:
62+
that:
63+
- test_three is changed
64+
- test_three['diff']['after']['auth_type'] == "md5"
65+
- test_three['diff']['after']['auth_key'] == "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
66+
- test_three['diff']['after']['description'] == "Test description"
67+
- test_three['diff']['after']['tags'][0] == 4
68+
- test_three['fhrp_group']['group_id'] == 111
69+
- test_three['fhrp_group']['protocol'] == "glbp"
70+
- test_three['fhrp_group']['auth_type'] == "md5"
71+
- test_three['fhrp_group']['auth_key'] == "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
72+
- test_three['fhrp_group']['description'] == "Test description"
73+
- test_three['fhrp_group']['tags'][0] == 4
74+
- test_three['msg'] == "fhrp_group 111 updated"
75+
76+
- name: "FHRP group 4: ASSERT - Delete"
77+
netbox.netbox.netbox_fhrp_group:
78+
netbox_url: http://localhost:32768
79+
netbox_token: 0123456789abcdef0123456789abcdef01234567
80+
data:
81+
group_id: 111
82+
state: absent
83+
register: test_four
84+
85+
- name: "FHRP group 4: ASSERT - Delete"
86+
ansible.builtin.assert:
87+
that:
88+
- test_four is changed
89+
- test_four['diff']['before']['state'] == "present"
90+
- test_four['diff']['after']['state'] == "absent"
91+
- test_four['msg'] == "fhrp_group 111 deleted"

tests/integration/targets/v3.3/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,6 @@
267267

268268
- name: "NETBOX_ASN TESTS"
269269
include_tasks: "netbox_asn.yml"
270+
271+
- name: "NETBOX_FHRP_GROUP TESTS"
272+
include_tasks: "netbox_fhrp_group.yml"

0 commit comments

Comments
 (0)