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

Cannot access groups of a host from within a callback plugin #15538

Closed
rawbertp opened this issue Apr 22, 2016 · 5 comments
Closed

Cannot access groups of a host from within a callback plugin #15538

rawbertp opened this issue Apr 22, 2016 · 5 comments
Labels
affects_2.1 This issue/PR affects Ansible v2.1 bug This issue/PR relates to a bug. c:plugins/callback feature This issue/PR relates to a feature request. support:community This issue/PR relates to code supported by the Ansible community.

Comments

@rawbertp
Copy link

rawbertp commented Apr 22, 2016

ISSUE TYPE
  • Bug Report
COMPONENT NAME

callback plugins

ANSIBLE VERSION
ansible 2.1.0 (devel 0ced20951f) last updated 2016/04/12 14:12:44 (GMT +200)
  lib/ansible/modules/core: (detached HEAD c52f475c64) last updated 2016/04/12 14:12:50 (GMT +200)
  lib/ansible/modules/extras: (detached HEAD 5abb914315) last updated 2016/04/12 14:12:53 (GMT +200)
  config file = /home/robert/Documents/sandbox/ansible_callback_plugin_sandbox/ansible.cfg
  configured module search path = Default w/o overrides
CONFIGURATION
OS / ENVIRONMENT
SUMMARY

In a callback plugin, in the method v2_runner_on_failed(self, result, ignore_errors=False) I want to retrieve the groups of a host but it always returns an empty list.

STEPS TO REPRODUCE

Please see https://github.com/rawbertp/ansible_callback_plugin_issue/blob/master/plugins/callback_plugins/dummy.py and code below.

The group var "foo" is set in group_vars/all (="all"), group_vars/prod (="prod") and group_vars/test (="test").

Now I run my playbook as follows:

ansible-playbook -i inventory/test main.yml
ansible-playbook -i inventory/prod main.yml

Running it for "inventory/test" I'd expect the callback plugin to return "test" as a value for foo whereas for "inventory/prod" it should be prod. However, it always returns "all".

The line
self.play._variable_manager.get_vars(self.loader, play=self.play, task=result._task, host=result._host, use_cache=False)

only returns group vars set in "all".

The problem is that result._host.get_groups() always returns an empty [] list.

class CallbackModule(CallbackBase):
    def __init__(self):
        super(CallbackModule, self).__init__()
        logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

    def v2_runner_on_failed(self, result, ignore_errors=False):
        logging.debug("+++++ v2_runner_on_failed")

        logging.debug("host.get_groups(): ->")
        logging.debug(result._host.get_groups())

        #epdb.st()

        # Haven't managed it yet to get the actual effective group vars...
        # The problem is that result._host.get_groups() returns an empty list!
        # (Epdb) result._host
        # localhost
        # (Epdb) result._host.get_groups()
        # [] <---

        x = self.play._variable_manager.get_vars(self.loader, play=self.play, task=result._task, host=result._host, use_cache=False)

        logging.debug("Var from 'all' group_vars: " + x.get("foo"))


    def v2_playbook_on_start(self, playbook):
        logging.debug("+++++ v2_playbook_on_start")
        self.playbook = playbook


    def v2_playbook_on_play_start(self, *args, **kwargs):
        logging.debug("v2_playbook_on_play_start(self, *args, **kwargs)")
        self.play = args[0]  # Workaround for https://github.com/ansible/ansible/issues/13948
        self.loader = args[0]._loader
EXPECTED RESULTS

In my example (https://github.com/rawbertp/ansible_callback_plugin_issue/) I'd expect result._host.get_groups()
to return [frontend, prod] or [frontend, test] depending on the inventory chosen and self.play._variable_manager.get_vars(self.loader, play=self.play, task=result._task, host=result._host, use_cache=False)
to return the actually effective vars for the task.

inventory/prod -> foo == "prod"
inventory/test -> foo == "test"

ACTUAL RESULTS

result._host.get_groups() always returns an empty [] list.
self.play._variable_manager.get_vars(self.loader, play=self.play, task=result._task, host=result._host, use_cache=False) only returns the "all" group vars.

inventory/prod -> foo == "all"
inventory/test -> foo == "all"

@jhawkesworth
Copy link
Contributor

Not sure if this is what you need, but fairly certain it will get get group vars from groups other than the 'all' group like this.

    def v2_playbook_on_play_start(self, *args, **kwargs):
        hosts = args[0].get_variable_manager()._inventory.get_hosts()
        for host in hosts:
            host_grp_vars = host.get_group_vars()

@rawbertp
Copy link
Author

rawbertp commented Apr 22, 2016

Thanks for this hint. This seems to do the trick as a workaround.

    def v2_playbook_on_play_start(self, *args, **kwargs):
        ...
        self.hosts = args[0].get_variable_manager()._inventory.get_hosts()
    def v2_runner_on_failed(self, result, ignore_errors=False):
        ...
        host_name = result._host.name

        for host in self.hosts:
            if host.name == host_name:
                foo = host.get_group_vars().get("foo")

        logging.debug(foo)

Outcome:

  • "foo" set in group var file: var taken from group_vars/
  • "foo" not set in group var file: var taken from group_vars/all

Still, I wonder why result._host in v2_runner_on_failed returns an empty group list.

@bcoca
Copy link
Member

bcoca commented Apr 22, 2016

not a bug, this info is not supplied on purpose, callbacks are given a narrow access to data.

The 'hack' above is not guaranteed to always work and is unsupported.

@ansibot ansibot added the affects_2.1 This issue/PR affects Ansible v2.1 label Sep 8, 2016
@ansibot ansibot added needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Mar 29, 2017
@ansibot ansibot removed needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Apr 3, 2017
@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@ansibot
Copy link
Contributor

ansibot commented Sep 23, 2017

@ansibot ansibot added bug This issue/PR relates to a bug. feature This issue/PR relates to a feature request. and removed bug_report labels Mar 1, 2018
@ansibot ansibot added support:community This issue/PR relates to code supported by the Ansible community. and removed support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Sep 18, 2018
@ansibot ansibot added the needs_triage Needs a first human triage before being processed. label May 17, 2020
@sivel
Copy link
Member

sivel commented Aug 17, 2020

Closing per above.

If you have further questions please stop by IRC or the mailing list:

@sivel sivel closed this as completed Aug 17, 2020
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Aug 17, 2020
@ansible ansible locked and limited conversation to collaborators Sep 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.1 This issue/PR affects Ansible v2.1 bug This issue/PR relates to a bug. c:plugins/callback feature This issue/PR relates to a feature request. support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

No branches or pull requests

6 participants