strategy fix invalid variables being registered (#79706) (#79818)

also added tests for normal and bad cases

(cherry picked from commit 281474e809)
pull/80019/head
Brian Coca 2 years ago committed by GitHub
parent 02d9336b32
commit 58d66a7568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- strategy plugins now correctly identify bad registered variables, even on skip.

@ -54,7 +54,7 @@ from ansible.template import Templar
from ansible.utils.display import Display from ansible.utils.display import Display
from ansible.utils.fqcn import add_internal_fqcns from ansible.utils.fqcn import add_internal_fqcns
from ansible.utils.unsafe_proxy import wrap_var from ansible.utils.unsafe_proxy import wrap_var
from ansible.utils.vars import combine_vars from ansible.utils.vars import combine_vars, isidentifier
from ansible.vars.clean import strip_internal_keys, module_response_deepcopy from ansible.vars.clean import strip_internal_keys, module_response_deepcopy
display = Display() display = Display()
@ -750,6 +750,10 @@ class StrategyBase:
# register final results # register final results
if original_task.register: if original_task.register:
if not isidentifier(original_task.register):
raise AnsibleError("Invalid variable name in 'register' specified: '%s'" % original_task.register)
host_list = self.get_task_hosts(iterator, original_host, original_task) host_list = self.get_task_hosts(iterator, original_host, original_task)
clean_copy = strip_internal_keys(module_response_deepcopy(task_result._result)) clean_copy = strip_internal_keys(module_response_deepcopy(task_result._result))

@ -0,0 +1,2 @@
shippable/posix/group3
context/controller # this "module" is actually an action that runs on the controller

@ -0,0 +1,21 @@
- hosts: testhost
gather_facts: false
tasks:
- name: test registering
debug: msg='does nothing really but register this'
register: debug_msg
- name: validate registering
assert:
that:
- debug_msg is defined
- name: test registering skipped
debug: msg='does nothing really but register this'
when: false
register: debug_skipped
- name: validate registering
assert:
that:
- debug_skipped is defined

@ -0,0 +1,11 @@
- hosts: testhost
gather_facts: false
tasks:
- name: test registering
debug: msg='does nothing really but register this'
register: 200
- name: never gets here
assert:
that:
- 200 is not defined

@ -0,0 +1,12 @@
- hosts: testhost
gather_facts: false
tasks:
- name: test registering bad var when skipped
debug: msg='does nothing really but register this'
when: false
register: 200
- name: never gets here
assert:
that:
- 200 is not defined

@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eux
# does it work?
ansible-playbook can_register.yml -i ../../inventory -v "$@"
# ensure we do error when it its apprpos
set +e
result="$(ansible-playbook invalid.yml -i ../../inventory -v "$@" 2>&1)"
set -e
grep -q "Invalid variable name in " <<< "${result}"
Loading…
Cancel
Save