Skip to content
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

feat: ara_playbook_labels from inventory-vars #581

Open
NiceRath opened this issue Jan 20, 2025 · 3 comments
Open

feat: ara_playbook_labels from inventory-vars #581

NiceRath opened this issue Jan 20, 2025 · 3 comments

Comments

@NiceRath
Copy link

NiceRath commented Jan 20, 2025

What is the issue ?

It seems this might be feature-request as the current handling of ara_playbook_labels does not handle hostvars aka inventory-vars.

We try to pass some inventory-specific & group-specific labels to the ARA-client.

To ensure these are set as expected - we assert it inside our playbook:

- name: Test
  hosts: all
  become: true
  gather_facts: true
  pre_tasks:
    - ansible.builtin.assert:
        that:
          - ara_playbook_labels is defined
          - ara_playbook_labels | length > 0

But when I check-out the play_vars - the variable is not set?

...
raise ValueError("ara_playbook_labels" in play_vars, play_vars.keys())
if "ara_playbook_labels" in play_vars:
    ...

At: https://github.com/ansible-community/ara/blob/master/ara/plugins/callback/ara_default.py#L511

It Prints:

(False, dict_keys([<USER-SPECIFIC-VARS-REDACTED>, 'playbook_dir', 'ansible_playbook_python',
'ansible_config_file', 'ansible_role_names', 'ansible_play_role_names', 'ansible_dependent_role_names', 'role_names',
'ansible_play_name', 'groups', 'ansible_play_hosts_all', 'ansible_play_hosts', 'ansible_play_batch', 'play_hosts', 'omit',
'ansible_version', 'ansible_check_mode', 'ansible_diff_mode', 'ansible_forks', 'ansible_inventory_sources',
'ansible_skip_tags', 'ansible_limit', 'ansible_run_tags', 'ansible_verbosity', 'hostvars']))

When in the play_vars - the variable exists. But it seems to be nested under hostvars .

...
for k, v in play_vars.items():
    if v is not None and str(v).find("ara_playbook_labels") != -1:
        raise ValueError(k, v)
...

What should be happening ?

The documentation does only mention variables that are set in the playbook-context. But as Ansible allows also to set this variable in other ways - I would have expected it to work either way.

Two solutions come to my mind:

  • Either update the documentation, so it explicitly mentions that inventory-vars DO NOT work.
  • Or check the hostvars for ara_playbook_labels - I might supply a PR that enables this behaviour
@NiceRath NiceRath changed the title ara_playbook_labels not added ara_playbook_labels from inventory-vars not added Jan 20, 2025
@NiceRath NiceRath changed the title ara_playbook_labels from inventory-vars not added feat: ara_playbook_labels from inventory-vars Jan 20, 2025
@NiceRath
Copy link
Author

I understand that these labels are not host-specific, but specific to the execution.
But we should be able to iterate through the hostvars and extend the list of labels by all that we find..

@superstes
Copy link
Contributor

PR: #582

@dmsimard
Copy link
Contributor

Hi and thanks for the issue.

There has been discussions about this kind of feature in the past and whether we would do like the PR #582 suggests or introduce something like host labels.

For the use cases we had discussed at the time, it made more sense to have Ansible set a custom fact for the hosts and then have ara pick them up naturally like every other fact.

The answer depends on the use case so I would be curious to know about yours.

By having Ansible set a custom fact the operation is done once when running a playbook, then picked up automatically with every other fact when running the setup module or gather_facts.

By taking an approach where we want it to be a label set via an inventory variable, we take an approach like the one suggested in the PR where we have to iterate across every host, then look up if the variable exist, many times per playbook (once per play?). This operation is expensive in terms of performance and scales terribly when running across large playbooks with a lot of hosts.

We generally try to keep the "computing" that ara's ansible callback does to a minimum in order to have the smallest possible performance overhead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants