WIP! debugging nuances around the groups
This commit is contained in:
parent
d89d2bb89b
commit
32da1f6678
|
|
@ -45,10 +45,22 @@ options:
|
||||||
type: string
|
type: string
|
||||||
env:
|
env:
|
||||||
- name: NETBIRD_API_URL
|
- name: NETBIRD_API_URL
|
||||||
include_disconnected:
|
netbird_groups:
|
||||||
description: Whether or not to include disconnected peers in the inventory
|
description: A list of Netbird groups to filter the inventory by.
|
||||||
|
type: list
|
||||||
|
required: False
|
||||||
|
elements: string
|
||||||
|
strict:
|
||||||
|
description: Whether or not to fail if a group or variable is not found.
|
||||||
|
compose:
|
||||||
|
description: compose variables for Ansible based on jinja2 expression and inventory vars
|
||||||
|
default: False
|
||||||
|
required: False
|
||||||
type: boolean
|
type: boolean
|
||||||
default: false
|
keyed_groups:
|
||||||
|
description: create groups for plugins based on variable values and add the corresponding hosts to it
|
||||||
|
type: list
|
||||||
|
required: False
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = r"""
|
EXAMPLES = r"""
|
||||||
|
|
@ -60,10 +72,11 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cachea
|
||||||
from ansible.utils.display import Display
|
from ansible.utils.display import Display
|
||||||
|
|
||||||
# Specific for the NetbirdAPI Class
|
# Specific for the NetbirdAPI Class
|
||||||
import json, jsonpickle
|
import json
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import requests
|
import requests
|
||||||
|
import jsonpickle
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_NETBIRD_API_LIBS = False
|
HAS_NETBIRD_API_LIBS = False
|
||||||
else:
|
else:
|
||||||
|
|
@ -97,9 +110,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
def _add_groups(self):
|
def _add_groups(self):
|
||||||
""" Add peer groups to the inventory. """
|
""" Add peer groups to the inventory. """
|
||||||
self.netbird_groups = set(
|
self.netbird_groups = set(
|
||||||
filter(None,
|
filter(None, [group[0].get('name') for group in [item.data.get('groups') for l in self.peers for item in self.peers]]))
|
||||||
[group[0].get('name') for group in [item.data.get('groups') for l in self.peers for item in self.peers]])
|
|
||||||
)
|
|
||||||
for group in self.netbird_groups:
|
for group in self.netbird_groups:
|
||||||
self.inventory.add_group(group)
|
self.inventory.add_group(group)
|
||||||
|
|
||||||
|
|
@ -111,15 +122,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
|
|
||||||
def _get_peer_inventory(self):
|
def _get_peer_inventory(self):
|
||||||
"""Get the inventory from the Netbird API"""
|
"""Get the inventory from the Netbird API"""
|
||||||
if self.include_disconnected is False:
|
self.peers = self.client.ListPeers()
|
||||||
self.peers = [peer for peer in self.client.ListPeers() if peer.data["connected"] is True]
|
|
||||||
else:
|
|
||||||
display.vv("Including disconnected peers.")
|
|
||||||
self.peers = self.client.ListPeers()
|
|
||||||
|
|
||||||
def _filter_by_config(self):
|
def _filter_by_config(self):
|
||||||
"""Filter peers by user specified configuration."""
|
"""Filter peers by user specified configuration."""
|
||||||
groups = self.get_option('groups')
|
groups = self.get_option('netbird_groups')
|
||||||
if groups:
|
if groups:
|
||||||
self.peers = [
|
self.peers = [
|
||||||
# 202410221-MJH: This list comprehension that filters the peers is a little hard to read. I'm sorry.
|
# 202410221-MJH: This list comprehension that filters the peers is a little hard to read. I'm sorry.
|
||||||
|
|
@ -189,7 +196,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
# Check for None rather than False in order to allow
|
# Check for None rather than False in order to allow
|
||||||
# for empty sets of cached peers
|
# for empty sets of cached peers
|
||||||
if self.peers is None:
|
if self.peers is None:
|
||||||
self.include_disconnected = self.get_option('include_disconnected')
|
|
||||||
self._build_client(loader)
|
self._build_client(loader)
|
||||||
self._get_peer_inventory()
|
self._get_peer_inventory()
|
||||||
|
|
||||||
|
|
@ -207,6 +213,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
self._add_groups()
|
self._add_groups()
|
||||||
self._add_peers_to_group()
|
self._add_peers_to_group()
|
||||||
self._add_hostvars_for_peers()
|
self._add_hostvars_for_peers()
|
||||||
|
|
||||||
|
groups = self.get_option('groups')
|
||||||
|
for group_name in groups:
|
||||||
|
conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % groups[group_name]
|
||||||
|
|
||||||
for peer in self.peers:
|
for peer in self.peers:
|
||||||
variables = self.inventory.get_host(peer.label).get_vars()
|
variables = self.inventory.get_host(peer.label).get_vars()
|
||||||
self._add_host_to_composed_groups(
|
self._add_host_to_composed_groups(
|
||||||
|
|
@ -214,17 +225,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
variables,
|
variables,
|
||||||
peer.label,
|
peer.label,
|
||||||
strict=strict)
|
strict=strict)
|
||||||
self._add_host_to_keyed_groups(
|
raise AnsibleError(f"self.inventory:\n {jsonpickle.encode(self.inventory,indent=True)}")
|
||||||
self.get_option('keyed_groups'),
|
# self._add_host_to_keyed_groups(
|
||||||
variables,
|
# self.get_option('keyed_groups'),
|
||||||
peer.label,
|
# variables,
|
||||||
strict=strict)
|
# peer.label,
|
||||||
self._set_composite_vars(
|
# strict=strict)
|
||||||
self.get_option('compose'),
|
# self._set_composite_vars(
|
||||||
variables,
|
# self.get_option('compose'),
|
||||||
peer.label,
|
# variables,
|
||||||
strict=strict)
|
# peer.label,
|
||||||
raise AnsibleError(f"self.inventory: {jsonpickle.encode(self.inventory)}")
|
# strict=strict)
|
||||||
|
|
||||||
|
|
||||||
# This is a very limited wrapper for the netbird API.
|
# This is a very limited wrapper for the netbird API.
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,14 @@
|
||||||
plugin: dominion_solutions.netbird
|
plugin: dominion_solutions.netbird
|
||||||
api_key: nbp_1234567890123456789012345678901234567
|
api_key: nbp_1234567890123456789012345678901234567
|
||||||
api_url: https://netbird.example.com/api/v1
|
api_url: https://netbird.example.com/api/v1
|
||||||
include_disconnected: Yes
|
|
||||||
ip_style: plain
|
ip_style: plain
|
||||||
groups:
|
netbird_groups:
|
||||||
- "All"
|
- "All"
|
||||||
|
groups:
|
||||||
|
connected_hosts: connected
|
||||||
|
ssh_hosts: ssh_enabled
|
||||||
|
strict: Yes
|
||||||
|
keyed_groups:
|
||||||
|
compose:
|
||||||
|
ansible_ssh_host: label
|
||||||
|
ansible_ssh_port: 22
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,30 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"accessible_peers_count": 1,
|
"accessible_peers_count": 1,
|
||||||
"approval_required": false,
|
"approval_required": false,
|
||||||
"connected": false,
|
"connected": false,
|
||||||
"dns_label": "apple.netbird.cloud",
|
"dns_label": "apple.netbird.cloud",
|
||||||
"groups": [
|
"groups": [
|
||||||
{
|
{
|
||||||
"id": "2a3b4c5d6e7f8g9h0i1j",
|
"id": "2a3b4c5d6e7f8g9h0i1j",
|
||||||
"name": "All",
|
"name": "All",
|
||||||
"peers_count": 2
|
"peers_count": 2
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"hostname": "apple",
|
"hostname": "apple",
|
||||||
"id": "3a7b2c1d4e5f6g8h9i0j",
|
"id": "3a7b2c1d4e5f6g8h9i0j",
|
||||||
"ip": "100.0.0.42",
|
"ip": "100.0.0.42",
|
||||||
"last_login": "2024-02-10T22:01:27.744131502Z",
|
"last_login": "2024-02-10T22:01:27.744131502Z",
|
||||||
"last_seen": "2024-02-11T03:21:42.202104672Z",
|
"last_seen": "2024-02-11T03:21:42.202104672Z",
|
||||||
"login_expiration_enabled": true,
|
"login_expiration_enabled": true,
|
||||||
"login_expired": false,
|
"login_expired": false,
|
||||||
"name": "apple",
|
"name": "apple",
|
||||||
"os": "Linux Mint 21.3",
|
"os": "Linux Mint 21.3",
|
||||||
"ssh_enabled": false,
|
"ssh_enabled": false,
|
||||||
"ui_version": "netbird-desktop-ui/0.25.7",
|
"ui_version": "netbird-desktop-ui/0.25.7",
|
||||||
"user_id": "auth0|ABC123xyz4567890",
|
"user_id": "auth0|ABC123xyz4567890",
|
||||||
"version": "0.25.7"
|
"version": "0.25.7"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"accessible_peers_count": 1,
|
"accessible_peers_count": 1,
|
||||||
"approval_required": false,
|
"approval_required": false,
|
||||||
|
|
@ -46,7 +46,7 @@
|
||||||
"login_expired": false,
|
"login_expired": false,
|
||||||
"name": "banana",
|
"name": "banana",
|
||||||
"os": "Alpine Linux 3.19.1",
|
"os": "Alpine Linux 3.19.1",
|
||||||
"ssh_enabled": false,
|
"ssh_enabled": true,
|
||||||
"ui_version": "",
|
"ui_version": "",
|
||||||
"user_id": "",
|
"user_id": "",
|
||||||
"version": "0.25.5"
|
"version": "0.25.5"
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ from ansible.utils.display import Display
|
||||||
from ansible_collections.dominion_solutions.netbird.plugins.inventory.netbird import InventoryModule, NetbirdApi, Peer
|
from ansible_collections.dominion_solutions.netbird.plugins.inventory.netbird import InventoryModule, NetbirdApi, Peer
|
||||||
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
import json,jsonpickle
|
import json
|
||||||
|
# import jsonpickle
|
||||||
|
|
||||||
display = Display()
|
display = Display()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue