Skip to content

special_vars are a flavor of host specific task vars - #85424

Draft
sivel wants to merge 5 commits into
ansible:develfrom
sivel:issue/36978-3
Draft

special_vars are a flavor of host specific task vars#85424
sivel wants to merge 5 commits into
ansible:develfrom
sivel:issue/36978-3

Conversation

@sivel

@sivel sivel commented Jul 3, 2025

Copy link
Copy Markdown
Member
SUMMARY

special_vars are a flavor of host specific task vars. ci_complete

The issue here is that IncludedVars uses 5 things for uniqueness:

  1. Included filename
  2. Include args
  3. "special" vars (primarily loop related variables)
  4. task uuid
  5. parent task uuid

There is currently a 1:1 relationship between IncludedFile and special_vars. So if the loop is dependent on a host variable, special_vars will differ per host, creating a 1:1 relationship between IncludedFile and Host. Whereas the expectation is to have a 1:many relationship.

This PR creates a new dataclass specifically for handling this, and removing the vars comparison, but continuing to link it with the host, but without changing the existing variable precedence.

So this change tracks the vars with the host, but doesn't use the vars for comparisons. It then creates copies of the task for the hosts, and merges the vars.

Another solution may be to add a new step in the variable precendence rules for this use, instead of copying the task, and merging the task vars.

Reproducer:

---
- hosts: all
  gather_facts: false
  vars:
    test_variable:
      - "{{ inventory_hostname[-1]|int }}"
  tasks:
    - include_tasks: do_something.yml
      loop: "{{ test_variable }}"
localhost0 ansible_connection=local
localhost1 ansible_connection=local
localhost2 ansible_connection=local
localhost3 ansible_connection=local
localhost4 ansible_connection=local

[all:vars]
ansible_python_interpreter={{ansible_playbook_python}}
ISSUE TYPE
  • Bugfix Pull Request
Addditional Context

Outputs:

devel...

TASK [include_tasks] ***********************************************************
included: /Users/sivel/projects/ansibledev/playbooks/36978/do_something.yml for localhost0
included: /Users/sivel/projects/ansibledev/playbooks/36978/do_something.yml for localhost1 => (item=1)
included: /Users/sivel/projects/ansibledev/playbooks/36978/do_something.yml for localhost2 => (item=2)
included: /Users/sivel/projects/ansibledev/playbooks/36978/do_something.yml for localhost3 => (item=3)
included: /Users/sivel/projects/ansibledev/playbooks/36978/do_something.yml for localhost4 => (item=4)

TASK [debug] *******************************************************************
ok: [localhost0] => {
    "msg": "0 something"
}

TASK [debug] *******************************************************************
ok: [localhost1] => {
    "msg": "1 something"
}

TASK [debug] *******************************************************************
ok: [localhost2] => {
    "msg": "2 something"
}

TASK [debug] *******************************************************************
ok: [localhost3] => {
    "msg": "3 something"
}

TASK [debug] *******************************************************************
ok: [localhost4] => {
    "msg": "4 something"
}

This PR...

TASK [include_tasks] ***********************************************************
included: /Users/sivel/projects/ansibledev/playbooks/36978/do_something.yml for localhost0, localhost1, localhost2, localhost3, localhost4 => (item=<various>)

TASK [debug] *******************************************************************
ok: [localhost0] => {
    "msg": "0 something"
}
ok: [localhost1] => {
    "msg": "1 something"
}
ok: [localhost2] => {
    "msg": "2 something"
}
ok: [localhost3] => {
    "msg": "3 something"
}
ok: [localhost4] => {
    "msg": "4 something"
}

@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. labels Jul 3, 2025
@s-hertel s-hertel removed the needs_triage Needs a first human triage before being processed. label Jul 15, 2025
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 15, 2025
@bcoca

bcoca commented Jul 16, 2025

Copy link
Copy Markdown
Member

+1 for this approach, I really don't think we should add yet another level of variable precedence ...

@sivel
sivel force-pushed the issue/36978-3 branch 4 times, most recently from 2d07432 to 70f16f4 Compare August 4, 2025 20:16
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Aug 4, 2025
@ansibot

This comment was marked as outdated.

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Aug 4, 2025
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Aug 19, 2025
@ansibot ansibot added the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Sep 9, 2025
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Apr 13, 2026
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Apr 13, 2026
@sivel
sivel force-pushed the issue/36978-3 branch 2 times, most recently from 851a822 to 1f5352c Compare April 13, 2026 21:19
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 27, 2026
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 30, 2026
@sivel
sivel marked this pull request as ready for review April 30, 2026 19:56
fb_copy = final_block.copy(exclude_parent=True)
fb_copy._parent = final_block._parent
fb_copy.vars |= hv.vars
all_blocks[hv.host].append(fb_copy)

@sivel sivel Apr 30, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to call attention to this change. This can be a performance degradation, but without making copies of the block for each host, this approach falls apart. Generally speaking it is not likely to be a source of trouble, but worth reviewing this carefully.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing at least the wall clock time dropped with this PR.

We could probably (micro-)optimize the Block.copy() method as well (which I started playing with in #86936).

@sivel
sivel requested a review from mkrizek May 4, 2026 19:13
mkrizek
mkrizek previously approved these changes May 5, 2026

@mkrizek mkrizek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor suggestions but 👍🏻 overall.

Comment thread lib/ansible/playbook/handler.py Outdated
Comment thread lib/ansible/playbook/included_file.py Outdated
fb_copy = final_block.copy(exclude_parent=True)
fb_copy._parent = final_block._parent
fb_copy.vars |= hv.vars
all_blocks[hv.host].append(fb_copy)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing at least the wall clock time dropped with this PR.

We could probably (micro-)optimize the Block.copy() method as well (which I started playing with in #86936).

@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. pending_ci and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. pending_ci labels May 5, 2026
Comment thread lib/ansible/plugins/strategy/__init__.py
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels May 15, 2026
@sivel
sivel force-pushed the issue/36978-3 branch 2 times, most recently from 9ae5660 to ea290a9 Compare May 19, 2026 19:34
@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label May 19, 2026
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label May 20, 2026
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jun 2, 2026
@ansibot ansibot removed the stale_review Updates were made after the last review and the last review is more than 7 days old. label Jun 9, 2026
@sivel
sivel marked this pull request as draft June 16, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug This issue/PR relates to a bug. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants