-
Notifications
You must be signed in to change notification settings - Fork 23
Gather cloud factsv2 #1869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GavinHeff
wants to merge
1
commit into
stackhpc/2025.1
Choose a base branch
from
gatherCloudFactsv2
base: stackhpc/2025.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Gather cloud factsv2 #1869
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,87 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from ansible.errors import AnsibleFilterError | ||
|
||
class FilterModule(object): | ||
def filters(self): | ||
return { | ||
'group_hostvars_by_var': | ||
self.group_hostvars_by_var, | ||
'get_hostvars_by_host': | ||
self.get_hostvars_by_host | ||
} | ||
|
||
def group_hostvars_by_var(self, hostvars, var, subkey=None): | ||
""" | ||
Returns a dictionary where the keys are values for the | ||
specified var in hostvars, and the values are the hosts | ||
that match that value. | ||
|
||
For example, a grouping of hosts by OS release might look like: | ||
distribution_release: | ||
noble: | ||
- node1 | ||
- node2 | ||
jammy: | ||
- node3 | ||
- node4 | ||
- node5 | ||
|
||
Some Ansible commands, such as ansible.builtin.command, return a | ||
dict rather than a single value. So 'subkey' is used for these cases | ||
to access the desired value. | ||
""" | ||
result = {} | ||
|
||
for host in hostvars.keys(): | ||
try: | ||
indiv_host_var = hostvars[host][var] | ||
if subkey is not None: | ||
indiv_host_var = indiv_host_var[subkey] | ||
result.setdefault(indiv_host_var, []).append(host) | ||
except KeyError as e: | ||
raise AnsibleFilterError(f"Variable {var} not found for host {host} in hostvars: {e}") | ||
|
||
return result | ||
|
||
def get_hostvars_by_host(self, hostvars, var, subkey=None): | ||
""" | ||
Returns a dictionary where the keys are hosts and the values | ||
are the values for the specified var in hostvars. | ||
|
||
For example, the deployed containers by host might look like: | ||
deployed_containers: | ||
node1: | ||
- grafana | ||
- glance | ||
- nova | ||
- prometheus | ||
node2: | ||
- designate | ||
- neutron | ||
- nova | ||
|
||
Some Ansible commands, such as ansible.builtin.command, return a | ||
dict rather than a single value. So 'subkey' is used for these cases | ||
to access the desired value. | ||
""" | ||
result = {} | ||
for host in hostvars.keys(): | ||
try: | ||
indiv_host_var = hostvars[host][var] | ||
for key in indiv_host_var.keys(): | ||
# Check if task to assign value was skipped | ||
if key == "skipped": | ||
result[host] = "No data" | ||
continue | ||
if subkey is not None: | ||
indiv_host_var = indiv_host_var[subkey] | ||
if indiv_host_var: | ||
result[host] = indiv_host_var | ||
else: | ||
result[host] = [] | ||
|
||
except KeyError as e: | ||
raise AnsibleFilterError(f"Variable {var} not found for host {host} in hostvars: {e}") | ||
|
||
return result |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.