summaryrefslogtreecommitdiff
path: root/test/integration
diff options
context:
space:
mode:
authorMatt Martz <matt@sivel.net>2023-03-23 13:04:09 -0500
committerGitHub <noreply@github.com>2023-03-23 13:04:09 -0500
commit42355d181a11b51ebfc56f6f4b3d9c74e01cb13b (patch)
treee81d9e92c30e59ca3f8d87b5db31cdbf909c3433 /test/integration
parentfafb23094e77a619066a92a7fa99a7045292e473 (diff)
downloadansible-42355d181a11b51ebfc56f6f4b3d9c74e01cb13b.tar.gz
Do not double calculate loops and `delegate_to` (#80171)
Diffstat (limited to 'test/integration')
-rwxr-xr-xtest/integration/targets/delegate_to/runme.sh4
-rw-r--r--test/integration/targets/delegate_to/test_random_delegate_to_with_loop.yml26
-rw-r--r--test/integration/targets/delegate_to/test_random_delegate_to_without_loop.yml13
3 files changed, 43 insertions, 0 deletions
diff --git a/test/integration/targets/delegate_to/runme.sh b/test/integration/targets/delegate_to/runme.sh
index 1bdf27cfb2..e0dcc746aa 100755
--- a/test/integration/targets/delegate_to/runme.sh
+++ b/test/integration/targets/delegate_to/runme.sh
@@ -76,3 +76,7 @@ ansible-playbook test_delegate_to_lookup_context.yml -i inventory -v "$@"
ansible-playbook delegate_local_from_root.yml -i inventory -v "$@" -e 'ansible_user=root'
ansible-playbook delegate_with_fact_from_delegate_host.yml "$@"
ansible-playbook delegate_facts_loop.yml -i inventory -v "$@"
+ansible-playbook test_random_delegate_to_with_loop.yml -i inventory -v "$@"
+
+# Run playbook multiple times to ensure there are no false-negatives
+for i in $(seq 0 10); do ansible-playbook test_random_delegate_to_without_loop.yml -i inventory -v "$@"; done;
diff --git a/test/integration/targets/delegate_to/test_random_delegate_to_with_loop.yml b/test/integration/targets/delegate_to/test_random_delegate_to_with_loop.yml
new file mode 100644
index 0000000000..cd7b888b6e
--- /dev/null
+++ b/test/integration/targets/delegate_to/test_random_delegate_to_with_loop.yml
@@ -0,0 +1,26 @@
+- hosts: localhost
+ gather_facts: false
+ tasks:
+ - add_host:
+ name: 'host{{ item }}'
+ groups:
+ - test
+ loop: '{{ range(10) }}'
+
+ # This task may fail, if it does, it means the same thing as if the assert below fails
+ - set_fact:
+ dv: '{{ ansible_delegated_vars[ansible_host]["ansible_host"] }}'
+ delegate_to: '{{ groups.test|random }}'
+ delegate_facts: true
+ # Purposefully smaller loop than group count
+ loop: '{{ range(5) }}'
+
+- hosts: test
+ gather_facts: false
+ tasks:
+ - assert:
+ that:
+ - dv == inventory_hostname
+ # The small loop above means we won't set this var for every host
+ # and a smaller loop is faster, and may catch the error in the above task
+ when: dv is defined
diff --git a/test/integration/targets/delegate_to/test_random_delegate_to_without_loop.yml b/test/integration/targets/delegate_to/test_random_delegate_to_without_loop.yml
new file mode 100644
index 0000000000..9527862891
--- /dev/null
+++ b/test/integration/targets/delegate_to/test_random_delegate_to_without_loop.yml
@@ -0,0 +1,13 @@
+- hosts: localhost
+ gather_facts: false
+ tasks:
+ - add_host:
+ name: 'host{{ item }}'
+ groups:
+ - test
+ loop: '{{ range(10) }}'
+
+ - set_fact:
+ dv: '{{ ansible_delegated_vars[ansible_host]["ansible_host"] }}'
+ delegate_to: '{{ groups.test|random }}'
+ delegate_facts: true