1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -87,53 +87,54 @@ from ansible_collections.community.general.plugins.plugin_utils.unsafe import ma
try:
import gitlab
HAS_GITLAB = True
except ImportError:
HAS_GITLAB = False
class InventoryModule(BaseInventoryPlugin, Constructable):
''' Host inventory parser for ansible using GitLab API as source. '''
"""Host inventory parser for ansible using GitLab API as source."""
NAME = 'community.general.gitlab_runners'
NAME = "community.general.gitlab_runners"
def _populate(self):
gl = gitlab.Gitlab(self.get_option('server_url'), private_token=self.get_option('api_token'))
self.inventory.add_group('gitlab_runners')
gl = gitlab.Gitlab(self.get_option("server_url"), private_token=self.get_option("api_token"))
self.inventory.add_group("gitlab_runners")
try:
if self.get_option('filter'):
runners = gl.runners.all(scope=self.get_option('filter'))
if self.get_option("filter"):
runners = gl.runners.all(scope=self.get_option("filter"))
else:
runners = gl.runners.all()
for runner in runners:
host = make_unsafe(str(runner['id']))
ip_address = runner['ip_address']
host_attrs = make_unsafe(vars(gl.runners.get(runner['id']))['_attrs'])
self.inventory.add_host(host, group='gitlab_runners')
self.inventory.set_variable(host, 'ansible_host', make_unsafe(ip_address))
if self.get_option('verbose_output', True):
self.inventory.set_variable(host, 'gitlab_runner_attributes', host_attrs)
host = make_unsafe(str(runner["id"]))
ip_address = runner["ip_address"]
host_attrs = make_unsafe(vars(gl.runners.get(runner["id"]))["_attrs"])
self.inventory.add_host(host, group="gitlab_runners")
self.inventory.set_variable(host, "ansible_host", make_unsafe(ip_address))
if self.get_option("verbose_output", True):
self.inventory.set_variable(host, "gitlab_runner_attributes", host_attrs)
# Use constructed if applicable
strict = self.get_option('strict')
strict = self.get_option("strict")
# Composed variables
self._set_composite_vars(self.get_option('compose'), host_attrs, host, strict=strict)
self._set_composite_vars(self.get_option("compose"), host_attrs, host, strict=strict)
# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
self._add_host_to_composed_groups(self.get_option('groups'), host_attrs, host, strict=strict)
self._add_host_to_composed_groups(self.get_option("groups"), host_attrs, host, strict=strict)
# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), host_attrs, host, strict=strict)
self._add_host_to_keyed_groups(self.get_option("keyed_groups"), host_attrs, host, strict=strict)
except Exception as e:
raise AnsibleParserError(f'Unable to fetch hosts from GitLab API, this was the original exception: {e}')
raise AnsibleParserError(f"Unable to fetch hosts from GitLab API, this was the original exception: {e}")
def verify_file(self, path):
"""Return the possibly of a file being consumable by this plugin."""
return (
super().verify_file(path) and
path.endswith(("gitlab_runners.yaml", "gitlab_runners.yml")))
return super().verify_file(path) and path.endswith(("gitlab_runners.yaml", "gitlab_runners.yml"))
def parse(self, inventory, loader, path, cache=True):
if not HAS_GITLAB:
raise AnsibleError('The GitLab runners dynamic inventory plugin requires python-gitlab: https://python-gitlab.readthedocs.io/en/stable/')
raise AnsibleError(
"The GitLab runners dynamic inventory plugin requires python-gitlab: https://python-gitlab.readthedocs.io/en/stable/"
)
super().parse(inventory, loader, path, cache)
self._read_config_data(path)
self._populate()