forked from netbox-community/ansible_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Added module for FHRP groups (netbox-community#957)
* Feature: Added module for FHRP groups * Added tests for the FHRP groups module * Updated documentation for fhrp_group module
- Loading branch information
1 parent
4117a89
commit 028924d
Showing
9 changed files
with
465 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2023, Andrii Konts (@andrii-konts) <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
DOCUMENTATION = r""" | ||
--- | ||
module: netbox_fhrp_group | ||
short_description: Create, update or delete FHRP groups within NetBox | ||
description: | ||
- Creates, updates or removes FHRP groups from NetBox | ||
notes: | ||
- Tags should be defined as a YAML list | ||
- This should be ran with connection C(local) and hosts C(localhost) | ||
author: | ||
- Andrii Konts (@andrii-konts) | ||
requirements: | ||
- pynetbox | ||
seealso: | ||
- name: FHRP Group Model reference | ||
description: NetBox Documentation for FHRP Group Model. | ||
link: https://docs.netbox.dev/en/stable/models/ipam/fhrpgroup/ | ||
version_added: '3.12.0' | ||
extends_documentation_fragment: | ||
- netbox.netbox.common | ||
options: | ||
data: | ||
type: dict | ||
description: | ||
- Defines the FHRP group configuration | ||
suboptions: | ||
protocol: | ||
description: | ||
- Protocol | ||
required: False | ||
type: str | ||
choices: | ||
- vrrp2 | ||
- vrrp3 | ||
- carp | ||
- clusterxl | ||
- hsrp | ||
- glbp | ||
- other | ||
group_id: | ||
description: | ||
- Group ID (0 .. 32767) | ||
type: int | ||
required: true | ||
auth_type: | ||
description: | ||
- Authentication type | ||
choices: | ||
- plaintext | ||
- md5 | ||
type: str | ||
auth_key: | ||
description: | ||
- Authentication key (max length 255) | ||
type: str | ||
description: | ||
description: | ||
- Description (max length 200) | ||
required: false | ||
type: str | ||
tags: | ||
description: | ||
- Any tags that the FHRP group may need to be associated with | ||
required: false | ||
type: list | ||
elements: raw | ||
custom_fields: | ||
description: | ||
- Must exist in NetBox | ||
required: false | ||
type: dict | ||
required: true | ||
""" | ||
|
||
EXAMPLES = r""" | ||
- hosts: localhost | ||
connection: local | ||
module_defaults: | ||
group/netbox.netbox.netbox: | ||
netbox_url: "http://netbox.local" | ||
netbox_token: "thisIsMyToken" | ||
tasks: | ||
- name: "Create FHRP group within netbox" | ||
netbox.netbox.netbox_fhrp_group: | ||
data: | ||
protocol: "glbp" | ||
group_id: 111 | ||
auth_type: md5 | ||
auth_key: 11111 | ||
description: test FHRP group | ||
state: present | ||
- name: Delete FHRP group within netbox | ||
netbox.netbox.netbox_fhrp_group: | ||
data: | ||
group_id: 111 | ||
state: absent | ||
""" | ||
|
||
RETURN = r""" | ||
fhrp_group: | ||
description: Serialized object as created or already existent within NetBox | ||
returned: success (when I(state=present)) | ||
type: dict | ||
msg: | ||
description: Message indicating failure or info about what has been achieved | ||
returned: always | ||
type: str | ||
""" | ||
|
||
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import ( | ||
NetboxAnsibleModule, | ||
NETBOX_ARG_SPEC, | ||
) | ||
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import ( | ||
NetboxIpamModule, | ||
NB_FHRP_GROUPS, | ||
) | ||
|
||
from copy import deepcopy | ||
|
||
|
||
def main(): | ||
""" | ||
Main entry point for module execution | ||
""" | ||
argument_spec = deepcopy(NETBOX_ARG_SPEC) | ||
argument_spec.update( | ||
dict( | ||
data=dict( | ||
type="dict", | ||
required=True, | ||
options=dict( | ||
protocol=dict( | ||
type="str", | ||
choices=[ | ||
"vrrp2", | ||
"vrrp3", | ||
"carp", | ||
"clusterxl", | ||
"hsrp", | ||
"glbp", | ||
"other", | ||
], | ||
), | ||
group_id=dict(required=True, type="int"), | ||
auth_type=dict(type="str", choices=["plaintext", "md5"]), | ||
auth_key=dict(type="str", no_log=True), | ||
description=dict(type="str"), | ||
tags=dict(required=False, type="list", elements="raw"), | ||
custom_fields=dict(required=False, type="dict"), | ||
), | ||
), | ||
) | ||
) | ||
required_if = [("state", "present", ["protocol"])] | ||
module = NetboxAnsibleModule(argument_spec=argument_spec, required_if=required_if) | ||
netbox_fhrp_group = NetboxIpamModule(module, NB_FHRP_GROUPS) | ||
netbox_fhrp_group.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tests/integration/targets/v3.2/tasks/netbox_fhrp_group.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
## | ||
## | ||
### NETBOX_FHRP_GROUP | ||
## | ||
## | ||
- name: "FHRP group 1: Test FHRP group creation" | ||
netbox.netbox.netbox_fhrp_group: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
protocol: "glbp" | ||
group_id: 111 | ||
state: present | ||
register: test_one | ||
|
||
- name: "FHRP group: ASSERT - Necessary info creation" | ||
ansible.builtin.assert: | ||
that: | ||
- test_one is changed | ||
- test_one['diff']['before']['state'] == "absent" | ||
- test_one['diff']['after']['state'] == "present" | ||
- test_one['fhrp_group']['group_id'] == 111 | ||
- test_one['fhrp_group']['protocol'] == "glbp" | ||
- test_one['msg'] == "fhrp_group 111 created" | ||
|
||
- name: "FHRP group 2: Create duplicate" | ||
netbox.netbox.netbox_fhrp_group: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
protocol: "glbp" | ||
group_id: 111 | ||
state: present | ||
register: test_two | ||
|
||
- name: "FHRP group 2: ASSERT - Create duplicate" | ||
ansible.builtin.assert: | ||
that: | ||
- not test_two['changed'] | ||
- test_two['fhrp_group']['group_id'] == 111 | ||
- test_two['fhrp_group']['protocol'] == "glbp" | ||
- test_two['msg'] == "fhrp_group 111 already exists" | ||
|
||
- name: "FHRP group 3: Update FHRP group with other fields" | ||
netbox.netbox.netbox_fhrp_group: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
protocol: "glbp" | ||
group_id: 111 | ||
auth_type: md5 | ||
auth_key: 11111 | ||
description: Test description | ||
tags: | ||
- "Schnozzberry" | ||
state: present | ||
register: test_three | ||
|
||
- name: "FHRP group 3: ASSERT - Update FHRP group with other fields" | ||
ansible.builtin.assert: | ||
that: | ||
- test_three is changed | ||
- test_three['diff']['after']['auth_type'] == "md5" | ||
- test_three['diff']['after']['auth_key'] == "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" | ||
- test_three['diff']['after']['description'] == "Test description" | ||
- test_three['diff']['after']['tags'][0] == 4 | ||
- test_three['fhrp_group']['group_id'] == 111 | ||
- test_three['fhrp_group']['protocol'] == "glbp" | ||
- test_three['fhrp_group']['auth_type'] == "md5" | ||
- test_three['fhrp_group']['auth_key'] == "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" | ||
- test_three['fhrp_group']['description'] == "Test description" | ||
- test_three['fhrp_group']['tags'][0] == 4 | ||
- test_three['msg'] == "fhrp_group 111 updated" | ||
|
||
- name: "FHRP group 4: ASSERT - Delete" | ||
netbox.netbox.netbox_fhrp_group: | ||
netbox_url: http://localhost:32768 | ||
netbox_token: 0123456789abcdef0123456789abcdef01234567 | ||
data: | ||
group_id: 111 | ||
state: absent | ||
register: test_four | ||
|
||
- name: "FHRP group 4: ASSERT - Delete" | ||
ansible.builtin.assert: | ||
that: | ||
- test_four is changed | ||
- test_four['diff']['before']['state'] == "present" | ||
- test_four['diff']['after']['state'] == "absent" | ||
- test_four['msg'] == "fhrp_group 111 deleted" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.