Fix templating task action with host-specific vars (#75600) (#75613)

Fixes #75568

(cherry picked from commit 9c2f44b884)
pull/75623/head
Martin Krizek 3 years ago committed by GitHub
parent 999c2e603d
commit a6f24c407c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Fix templating task action with host-specific vars (https://github.com/ansible/ansible/issues/75568)

@ -263,16 +263,16 @@ class StrategyModule(StrategyBase):
# sets BYPASS_HOST_LOOP to true, or if it has run_once enabled. If so, we
# will only send this task to the first host in the list.
task.action = templar.template(task.action)
task_action = templar.template(task.action)
try:
action = action_loader.get(task.action, class_only=True, collection_list=task.collections)
action = action_loader.get(task_action, class_only=True, collection_list=task.collections)
except KeyError:
# we don't care here, because the action may simply not have a
# corresponding action plugin
action = None
if task.action in C._ACTION_META:
if task_action in C._ACTION_META:
# for the linear strategy, we run meta tasks just once and for
# all hosts currently being iterated over rather than one host
results.extend(self._execute_meta(task, play_context, iterator, host))

@ -1,3 +1,3 @@
[local]
testhost ansible_connection=local
testhost2 ansible_connection=local
testhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"
testhost2 ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"

@ -3,3 +3,5 @@
set -eux
ansible-playbook test_include_file_noop.yml -i inventory "$@"
ansible-playbook task_action_templating.yml -i inventory "$@"

@ -0,0 +1,26 @@
- hosts: testhost,testhost2
gather_facts: no
tasks:
- set_fact:
module_to_run: 'debug'
when: inventory_hostname == 'testhost'
- set_fact:
module_to_run: 'ping'
when: inventory_hostname == 'testhost2'
- action:
module: '{{ module_to_run }}'
register: out
- assert:
that:
- "'msg' in out"
- "'ping' not in out"
when: inventory_hostname == 'testhost'
- assert:
that:
- "'ping' in out"
- "'msg' not in out"
when: inventory_hostname == 'testhost2'
Loading…
Cancel
Save