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

Implicit "setup" task from "gather_facts: yes" is missing attributes compared to explicit task #28451

Closed
dmsimard opened this issue Aug 20, 2017 · 3 comments · Fixed by #32650
Assignees
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@dmsimard
Copy link
Contributor

ISSUE TYPE
  • Bug Report
COMPONENT NAME

setup

ANSIBLE VERSION
ansible 2.4.0
CONFIGURATION

Not relevant

OS / ENVIRONMENT

Not relevant

SUMMARY

The implicit setup task that is issued from gather_facts differs from an explicit setup task.

The explicit setup task has a data source which makes task.get_path() (amongst other things) work.

Looking at the get_path method, you can see that we are expecting the task to have a _ds, _data_source and _line_number attributes .

STEPS TO REPRODUCE

We can reproduce this issue with a simple callback:

from __future__ import (absolute_import, division, print_function)
from ansible.plugins.callback import CallbackBase


class CallbackModule(CallbackBase):
    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'notification'
    CALLBACK_NAME = 'setup_task'

    def __init__(self):
        super(CallbackModule, self).__init__()

    def v2_playbook_on_task_start(self, task, is_conditional):
        if hasattr(task, '_ds'):
            print("This task has a data source")
            print(task.get_path())
        else:
            print("This task does not have a data source")

And the following playbook:

- name: Test playbook
  hosts: all
  gather_facts: yes
  tasks:
    - name: Explicit setup task
      setup:
EXPECTED RESULTS

I expect the gather_facts implicit setup task to have the same parameters and attributes as an explicit task.

ACTUAL RESULTS
PLAY [Test playbook] ***********************************************************

TASK [Gathering Facts] *********************************************************
This task does not have a data source
ok: [localhost]

TASK [Explicit setup task] *****************************************************
This task has a data source
/home/dmsimard/dev/ansible-sandbox/playbooks/test.yml:5
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Aug 20, 2017
openstack-gerrit pushed a commit to ansible-community/ara that referenced this issue Aug 21, 2017
Some implicit tasks (is the setup task from "gather_facts: yes" the
only one?) do not currently have a file reference as documented in
an issue upstream [1].

Work around the issue in the meantime and default to the main playbook
file if there is no references so that we don't have to handle this
edge case.

[1]: ansible/ansible#28451

Change-Id: I72526ee47bbe86022c60bc5fd23ca9627f86a4de
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Aug 21, 2017
@bcoca
Copy link
Member

bcoca commented Aug 21, 2017

the attributes ARE there, they are just not set, they should be set to the play that triggers it.

openstack-gerrit pushed a commit to ansible-community/ara that referenced this issue Aug 26, 2017
Some implicit tasks (is the setup task from "gather_facts: yes" the
only one?) do not currently have a file reference as documented in
an issue upstream [1].

Work around the issue in the meantime and default to the main playbook
file if there is no references so that we don't have to handle this
edge case.

[1]: ansible/ansible#28451

Change-Id: I72526ee47bbe86022c60bc5fd23ca9627f86a4de
(cherry picked from commit 24439f6)
@ansibot
Copy link
Contributor

ansibot commented Aug 29, 2017

cc @david_obrien
click here for bot help

@nrwahl2
Copy link
Contributor

nrwahl2 commented Sep 2, 2017

@dmsimard As @bcoca mentioned, It has a _ds attribute via task._parent._play... but the _ds attribute does not have _data_source and _linenumber attributes directly. We have to get them from ansible_pos. If core team decides they want to expose the attributes via the get_path method, one way would be to make this change in playbook/task.py.

BEFORE:

    def get_path(self):
        ''' return the absolute path of the task with its line number '''

        path = ""
        if hasattr(self, '_ds') and hasattr(self._ds, '_data_source') and hasattr(self._ds, '_line_number'):
            path = "%s:%s" % (self._ds._data_source, self._ds._line_number)
        return path

AFTER:

    def get_path(self):
        ''' return the absolute path of the task with its line number '''

        path = ""
        if hasattr(self, '_ds') and hasattr(self._ds, '_data_source') and hasattr(self._ds, '_line_number'):
            path = "%s:%s" % (self._ds._data_source, self._ds._line_number)
        elif self.action in ('setup', 'gather_facts'):
            _play = self._parent._play
            if hasattr(_play, '_ds') and hasattr(_play._ds, '_data_source') and hasattr(_play._ds, '_line_number'):
                path = "%s:%s" % (_play._ds.ansible_pos[0], _play._ds.ansible_pos[1])
        return path

You can get the attributes in a roundabout way with a callback like so:
CALLBACK:

[reid@laptop ~/git]$ cat ansible/lib/ansible/plugins/callback/setup.py
from __future__ import (absolute_import, division, print_function)
from ansible.plugins.callback import CallbackBase


class CallbackModule(CallbackBase):
    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'notification'
    CALLBACK_NAME = 'setup_task'

    def __init__(self):
        super(CallbackModule, self).__init__()

    def v2_playbook_on_task_start(self, task, is_conditional):
        if hasattr(task, '_ds'):
            print("This task has a data source")
            print(task.get_path())
        else:
            print("This task does not have a data source")
            print("%s:%s" % (task._parent._play._ds.ansible_pos[0], task._parent._play._ds.ansible_pos[1]))

RUN (your playbook):

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
This task does not have a data source
/home/reid/git/setup.yml:1
ok: [localhost]

TASK [Explicit setup task] ****************************************************************************************************************************************************************************************
This task has a data source
/home/reid/git/setup.yml:5
ok: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   

bcoca added a commit to bcoca/ansible that referenced this issue Nov 8, 2017
@bcoca bcoca removed the module This issue/PR relates to a module. label Nov 8, 2017
bcoca added a commit that referenced this issue Nov 9, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants