strategy fix invalid variables being registered (#79706)

also added tests for normal and bad cases
pull/79819/head
Brian Coca 2 years ago committed by GitHub
parent 7329ec6936
commit 281474e809
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.fqcn import add_internal_fqcns
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
display = Display()
@ -750,6 +750,10 @@ class StrategyBase:
# register final results
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)
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