Git Product home page Git Product logo

Comments (10)

mkrizek avatar mkrizek commented on June 17, 2024 1

As already explained in #81238 (comment), your include_role task uses a loop variable which exists only during the execution of that task (and has a different value each iteration). The registered result includes that variable in the include_args key. So attempting to pass the registered result into the debug module afterwards results in templating (because in ansible the variables are evaluated lazily) the result that contains, at that point, undefined variable role.

If you run the playbook with the -v option, you can see that the undefined variable is the loop variable role:

- hosts: localhost
  gather_facts: false
  tasks:
    - include_role:
        name: "{{ role }}"
      register: result
      loop_control:
        loop_var: role
      loop:
        - "a"

    - debug:
        var: result
% ansible-playbook playbook.yml -v  
Using /Users/mkrizek/src/ansible/ansible.cfg as config file

PLAY [localhost] *************************************************************************************************************************************************************************

TASK [include_role : {{ role }}] *********************************************************************************************************************************************************
included: a for localhost => (item=a)

TASK [a : debug] *************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "A"
}

TASK [debug] *****************************************************************************************************************************************************************************
ok: [localhost] => {
    "result": "VARIABLE IS NOT DEFINED!: {'results': [{'include_args': {'name': '{{ role }}'}, 'role': 'a', 'ansible_loop_var': 'role'}], 'skipped': False, 'msg': 'All items completed', 'changed': False}: 'role' is undefined"
}

PLAY RECAP *******************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

from ansible.

abugher avatar abugher commented on June 17, 2024 1

In this case, I'm just looking for the changed status. (I would be happy to either check one cumulative value or iterate over an array, but I haven't gotten that far.) I want to include some roles, and if they changed anything, I want to perform a certain follow-up action. Specifically the included roles are expected to install apt sources lists, and the follow-up action if any are changed is to run OS updates using the new lists.

from ansible.

ansibot avatar ansibot commented on June 17, 2024

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the component bot command.

from ansible.

mkrizek avatar mkrizek commented on June 17, 2024

Duplicate of #81238

from ansible.

abugher avatar abugher commented on June 17, 2024

#81238 looks different, to me. It seems to be about include_tasks, not include_role. If I am reading it right, it is about an undefined variable, but not in the same context. It was closed as a user question, not a bug, with no resolution.

Are you saying this is also not a bug, but it looks like I ran into the same problem?

from ansible.

abugher avatar abugher commented on June 17, 2024

OK. The loop variable is only scoped to the loop. The registered result is supposed to be scoped more broadly. Registering a variable includes that temporary loop variable as a component of the registered variable. The registered variable is then unusable.

From the ansible docs for "Registering variables": "You can use registered variables in any later tasks in your play." ... "When you register a variable in a task with a loop, the registered variable contains a value for each item in the loop."

That expectation is broken in this case.

The docs also say, "The documentation for each module includes a RETURN section describing the return values for that module." The page for include_role has no such section. The page for "Return Values" says, "Each module can optionally document its own unique return values".

If include_role breaks the expected behavior of "register", that seems like a bug to me. If it has to be that way (or if we like it that way for some reason), I think at a minimum the module documentation should warn about that.

from ansible.

mkrizek avatar mkrizek commented on June 17, 2024

What is it that you are trying to achieve? What part of the result of an include_role are you wanting to inspect?

The return value of include_role is really just information for ansible internals to process the role and actually include the contents of the role.

from ansible.

mkrizek avatar mkrizek commented on June 17, 2024

include_role has no information on what tasks within has changed or not. You need to inspect the specific tasks within the role to see if any follow-up actions are needed, either via handlers or other means.

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

See this page for a complete and up to date list of communication channels and their purposes:

from ansible.

abugher avatar abugher commented on June 17, 2024

Thanks. I will investigate accomplishing my goal with handlers. I opened a bug report, not a help request, though.

If my debug statement succeeded as expected and showed me the structure of the registered result, I would be able to see that it does not contain the information I am after. The documentation as written leads me to expect that it will succeed. If the documentation is correct, then I think the behavior of the module is in error. If the behavior of the module is correct, then I think the documentation is in error.

How about this: We can add to the module documentation a section titled RETURN VALUES, and in that section we can just have a short note, something like, "The return value of include_role is expected to contain invalid references to expired variables. Attempting to register it as normal and then refer to its value will lead to errors."

from ansible.

s-hertel avatar s-hertel commented on June 17, 2024

We're thinking we ought to add a warning if someone uses register with include_role, since this isn't public API. Leaving this open until we fix that.

from ansible.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.