Skip to content

expose play.strategy as ansible_play_strategy - #82871

Open
utoddl wants to merge 1 commit into
ansible:develfrom
utoddl:devel
Open

utoddl wants to merge 1 commit into
ansible:develfrom
utoddl:devel

Conversation

@utoddl

@utoddl utoddl commented Mar 21, 2024

Copy link
Copy Markdown
Contributor
SUMMARY

Create the magic variable ansible_play_strategy which makes the current play's strategy visible to tasks.

With this information, it becomes possible to take active precautions against the unexpected behavior ansible-lint warns about when combining strategy: free and run_once: true. For example, one could fail the play with a message like, "Don't run this play with strategy: free because it uses run_once." Or one could include a set of tasks more appropriate for the current strategy, a decision which isn't limited to the strategy: free case.

Without this info (i.e. currently), the only possible action is to ignore ansible-lint's warning, either with a noqa or tweaking the ansible-lint config file. That gets past linting, but there's nothing more you can do. If someone subsequently sets strategy: free, then they'll suffer the consequences that would have been mentioned in the suppressed warnings.

Note: you'll have to suppress ansible-lint's warnings in either case to pass linting. But at least with ansible_play_strategy exposed as a magic variable, you can take additional safeguards in the play itself, an option which isn't available to you without ansible_play_strategy.

ISSUE TYPE
  • Feature Pull Request
ADDITIONAL INFORMATION

This change has an extremely small footprint. There's not much more to say about it.

Here's a simple playbook with two plays, the first with strategy: free and the second without specifying a strategy.

---
# strategy.yml
- name: See if ansible_play_strategy is a variable
  hosts: localhost
  gather_facts: false
  strategy: free
  tasks:
    - name: Show the first play ansible_play_strategy
      ansible.builtin.debug:
        msg: 'ansible_play_strategy: {{ ansible_play_strategy }}'

- name: See if ansible_play_strategy changed
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Show the second play ansible_play_strategy
      ansible.builtin.debug:
        msg: 'ansible_play_strategy: {{ ansible_play_strategy }}'

And here's the output:

utoddl@tango:~/ansible$ ansible-playbook strategy.yml -vv
[WARNING]: You are running the development version of Ansible. You should only run Ansible from "devel" if you are modifying the Ansible engine, or trying out features under development.
This is a rapidly changing source of code and can become unstable at any point.
ansible-playbook [core 2.17.0.dev0] (devel 2b1a5dd7a0) last updated 2024/03/21 01:50:34 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/utoddl/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/utoddl/src/ansible-on-github/lib/ansible
  ansible collection location = /home/utoddl/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/utoddl/src/ansible-on-github/bin/ansible-playbook
  python version = 3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)] (/usr/bin/python)
  jinja version = 3.1.3
  libyaml = True
Using /etc/ansible/ansible.cfg as config file
redirecting (type: callback) ansible.builtin.yaml to community.general.yaml
redirecting (type: callback) ansible.builtin.yaml to community.general.yaml
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: strategy.yml *********************************************************************
2 plays in strategy.yml

PLAY [See if ansible_play_strategy is a variable] ******************************************

TASK [Show the first play ansible_play_strategy] *******************************************
task path: /home/utoddl/ansible/strategy.yml:8
ok: [localhost] => 
  msg: 'ansible_play_strategy: free'

PLAY [See if ansible_play_strategy changed] ************************************************

TASK [Show the second play ansible_play_strategy] ******************************************
task path: /home/utoddl/ansible/strategy.yml:16
ok: [localhost] => 
  msg: 'ansible_play_strategy: linear'

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

@ansibot ansibot added feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Mar 21, 2024
Comment thread lib/ansible/vars/hostvars.py Outdated
'ansible_dependent_role_names',
'ansible_play_role_names',
'ansible_role_names',
'ansible_play_strategy',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this should be undefined in hostvars context, no need to add it here

@webknjaz

This comment was marked as resolved.

@azure-pipelines

This comment was marked as resolved.

@nitzmahone nitzmahone added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Mar 21, 2024
@nitzmahone

Copy link
Copy Markdown
Member

We're trying to cut down on the use of top-level names for these things, so rather than making another magic var, we should define global objects to expose these things, and also make it clear that they're not settable and effectively reserved names.

@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 4, 2024
@utoddl

utoddl commented Apr 18, 2024 •

Copy link
Copy Markdown
Contributor Author

@nitzmahone - That sounds like reasonable goals. But I don't know how to translate that into working code. Are there existing instances of such global objects I can refer to for examples?

@nitzmahone

Copy link
Copy Markdown
Member

@utoddl There will be, early-ish in the 2.18 timeframe... Once the new infrastructure for creating and exposing them is in place, my hope is to re-implement a bunch of the "magic vars" and siloed state management that's currently scattered all over the codebase on top of these new contextvars + context manager accessors. There'd be a handful of them used as the actual state storage for things like PlayIterator, TaskQueueManager, TaskExecutor, etc, then we do a little bit of dynamic vars magic to plumb them through to our vars system and Jinja (hopefully with a more sustainable/extensible access and naming hierarchy than discrete ansible_X keys, hence my "let's just wait a bit"). That way, we're not manually sampling and copying all these vars from their various state stores for every host/task just in case someone asks for them- they'd be fully dynamic and directly consulting the underlying source of truth on demand. Better performance and less maintenance, yay!

@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label Mar 24, 2025
@ansibot ansibot removed stale_pr This PR has not been pushed to for more than one year. 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 4, 2025
@utoddl

utoddl commented May 4, 2025

Copy link
Copy Markdown
Contributor Author

Does this count as "early-ish in the 2.18 timeframe" yet, @nitzmahone? My one-line 13-month-old PR – which I just updated to work with current devel – exposes ansible_play_strategy in the same way that ansible_play_name is currently exposed.

I appreciate not wanting to dig a technical debt hole any deeper, but it seems whatever means will be used to transition ansible_play_name to use these long-awaited context accessors will be equally applicable to ansible_play_strategy.

Meanwhile, we're still faced with the same (reasonable) warning from ansible-lint with no practical means for Ansible users to effectively address it.

@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 May 18, 2025
@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data_tagging feature This issue/PR relates to a feature request. P3 Priority 3 - Approved, No Time Limitation stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_pr This PR has not been pushed to for more than one year.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants