diff --git a/.github/workflows/publish-to-ansible-galaxy.yml b/.github/workflows/publish-to-ansible-galaxy.yml index 505aa56..302d509 100644 --- a/.github/workflows/publish-to-ansible-galaxy.yml +++ b/.github/workflows/publish-to-ansible-galaxy.yml @@ -3,6 +3,21 @@ on: release: types: [published] jobs: + update-version: + permissions: + contents: write + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Update version + run: | + echo "Updating version" + sed -i "s/version: .*/version: ${{ github.event.release.tag_name }}/g" dominion_solutions/netbird/galaxy.yml + - uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "Update version to ${{ github.event.release.tag_name }}" + branch: "main" + ansible-publish: runs-on: ubuntu-latest steps: diff --git a/plugins/inventory/netbird.py b/plugins/inventory/netbird.py index d28be49..5dcafe7 100644 --- a/plugins/inventory/netbird.py +++ b/plugins/inventory/netbird.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# netbird inventory Ansible plugin # Copyright: (c) 2024, Dominion Solutions LLC (https://dominion.solutions) # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) @@ -50,6 +51,10 @@ options: type: list required: False elements: string + netbird_connected: + description: Filter the inventory by connected peers. + default: True + type: boolean strict: description: Whether or not to fail if a group or variable is not found. compose: @@ -64,6 +69,21 @@ options: ''' EXAMPLES = r""" +# This is an inventory that finds the All Group and creates groups for the connected and ssh_enabled peers. +--- +plugin: dominion_solutions.netbird +api_key: << api_key >> +api_url: << api_url >> +netbird_groups: +- "All" +groups: + connected: connected + ssh_hosts: ssh_enabled +strict: No +compose: + ansible_ssh_host: label + ansible_ssh_port: 22 + """ from ansible.errors import AnsibleError @@ -76,7 +96,6 @@ import json try: import requests - import jsonpickle except ImportError: HAS_NETBIRD_API_LIBS = False else: @@ -126,10 +145,16 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def _filter_by_config(self): """Filter peers by user specified configuration.""" + connected = self.get_option('netbird_connected') groups = self.get_option('netbird_groups') + if connected: + self.peers = [ + peer for peer in self.peers if peer.data.get('connected') + ] if groups: 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. + # If you can fix it and make it more readable, please feel free to make a PR. peer for peer in self.peers if any( group @@ -214,10 +239,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): self._add_peers_to_group() 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: variables = self.inventory.get_host(peer.label).get_vars() self._add_host_to_composed_groups( @@ -225,17 +246,18 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): variables, peer.label, strict=strict) - raise AnsibleError(f"self.inventory:\n {jsonpickle.encode(self.inventory,indent=True)}") - # self._add_host_to_keyed_groups( - # self.get_option('keyed_groups'), - # variables, - # peer.label, - # strict=strict) - # self._set_composite_vars( - # self.get_option('compose'), - # variables, - # peer.label, - # strict=strict) + + self._add_host_to_keyed_groups( + self.get_option('keyed_groups'), + variables, + peer.label, + strict=strict) + + self._set_composite_vars( + self.get_option('compose'), + variables, + peer.label, + strict=strict) # This is a very limited wrapper for the netbird API. diff --git a/tests/unit/module_utils/inventories/fixtures/netbird.yml b/tests/unit/module_utils/inventories/fixtures/netbird.yml index 647062e..2e0144a 100644 --- a/tests/unit/module_utils/inventories/fixtures/netbird.yml +++ b/tests/unit/module_utils/inventories/fixtures/netbird.yml @@ -3,12 +3,14 @@ plugin: dominion_solutions.netbird api_key: nbp_1234567890123456789012345678901234567 api_url: https://netbird.example.com/api/v1 ip_style: plain +netbird_connected: False +leading_separator: No netbird_groups: - "All" groups: - connected_hosts: connected + connected: connected ssh_hosts: ssh_enabled -strict: Yes +strict: No keyed_groups: compose: ansible_ssh_host: label diff --git a/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml b/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml new file mode 100644 index 0000000..f76a09a --- /dev/null +++ b/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml @@ -0,0 +1,11 @@ +--- +plugin: dominion_solutions.netbird +api_key: nbp_1234567890123456789012345678901234567 +api_url: https://netbird.example.com/api/v1 +ip_style: plain +netbird_connected: True +netbird_groups: +groups: +strict: No +keyed_groups: +compose: diff --git a/tests/unit/module_utils/inventories/fixtures/peers.json b/tests/unit/module_utils/inventories/fixtures/peers.json index f07ad26..a8e13ed 100644 --- a/tests/unit/module_utils/inventories/fixtures/peers.json +++ b/tests/unit/module_utils/inventories/fixtures/peers.json @@ -20,7 +20,7 @@ "login_expired": false, "name": "apple", "os": "Linux Mint 21.3", - "ssh_enabled": false, + "ssh_enabled": true, "ui_version": "netbird-desktop-ui/0.25.7", "user_id": "auth0|ABC123xyz4567890", "version": "0.25.7" diff --git a/tests/unit/plugins/inventory/test_netbird.py b/tests/unit/plugins/inventory/test_netbird.py index 93852ec..e3ecb95 100644 --- a/tests/unit/plugins/inventory/test_netbird.py +++ b/tests/unit/plugins/inventory/test_netbird.py @@ -7,10 +7,6 @@ __metaclass__ = type import pytest - -# TODO: Reenable this if needed. -# import sys - from ansible.errors import AnsibleError from ansible.inventory.data import InventoryData from ansible.parsing.dataloader import DataLoader @@ -21,7 +17,6 @@ from ansible_collections.dominion_solutions.netbird.plugins.inventory.netbird im from unittest.mock import MagicMock import json -# import jsonpickle display = Display() @@ -75,4 +70,17 @@ def test_get_peer_data(inventory, netbird_api): inventory.parse(InventoryData(), loader, path, False) assert inventory.inventory is not None assert inventory.inventory.hosts is not None + assert len(inventory.inventory.groups.get('ssh_hosts').hosts) == 2 + assert len(inventory.inventory.groups.get('connected').hosts) == 1 + + +def test_get_only_connected_peers(inventory, netbird_api): + loader = DataLoader() + path = 'tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml' + inventory._build_client = MagicMock() + inventory.client = netbird_api + inventory.parse(InventoryData(), loader, path, False) + assert inventory.inventory is not None + assert inventory.inventory.hosts is not None assert len(inventory.inventory.hosts) == 1 + assert list(inventory.inventory.hosts.values())[0].get_vars().get('connected') is True