summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/set_fact
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-14 20:03:01 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-14 20:03:01 +0000
commita453ac31f3428614cceb99027f8efbdb9258a40b (patch)
treef61f87408f32a8511cbd91799f9cececb53e0374 /test/integration/targets/set_fact
parentInitial commit. (diff)
downloadansible-a453ac31f3428614cceb99027f8efbdb9258a40b.tar.xz
ansible-a453ac31f3428614cceb99027f8efbdb9258a40b.zip
Adding upstream version 2.10.7+merged+base+2.10.8+dfsg.upstream/2.10.7+merged+base+2.10.8+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/integration/targets/set_fact')
-rw-r--r--test/integration/targets/set_fact/aliases2
-rw-r--r--test/integration/targets/set_fact/incremental.yml35
-rw-r--r--test/integration/targets/set_fact/inventory3
-rw-r--r--test/integration/targets/set_fact/nowarn_clean_facts.yml10
-rwxr-xr-xtest/integration/targets/set_fact/runme.sh30
-rw-r--r--test/integration/targets/set_fact/set_fact_bool_conv.yml35
-rw-r--r--test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml35
-rw-r--r--test/integration/targets/set_fact/set_fact_cached_1.yml324
-rw-r--r--test/integration/targets/set_fact/set_fact_cached_2.yml57
-rw-r--r--test/integration/targets/set_fact/set_fact_no_cache.yml39
10 files changed, 570 insertions, 0 deletions
diff --git a/test/integration/targets/set_fact/aliases b/test/integration/targets/set_fact/aliases
new file mode 100644
index 00000000..757c9966
--- /dev/null
+++ b/test/integration/targets/set_fact/aliases
@@ -0,0 +1,2 @@
+shippable/posix/group3
+skip/aix
diff --git a/test/integration/targets/set_fact/incremental.yml b/test/integration/targets/set_fact/incremental.yml
new file mode 100644
index 00000000..3f7aa6c4
--- /dev/null
+++ b/test/integration/targets/set_fact/incremental.yml
@@ -0,0 +1,35 @@
+- name: test set_fact incremental https://github.com/ansible/ansible/issues/38271
+ hosts: testhost
+ gather_facts: no
+ tasks:
+ - name: Generate inline loop for set_fact
+ set_fact:
+ dig_list: "{{ dig_list + [ item ] }}"
+ loop:
+ - two
+ - three
+ - four
+ vars:
+ dig_list:
+ - one
+
+ - name: verify cumulative set fact worked
+ assert:
+ that:
+ - dig_list == ['one', 'two', 'three', 'four']
+
+ - name: Generate inline loop for set_fact (FQCN)
+ ansible.builtin.set_fact:
+ dig_list_fqcn: "{{ dig_list_fqcn + [ item ] }}"
+ loop:
+ - two
+ - three
+ - four
+ vars:
+ dig_list_fqcn:
+ - one
+
+ - name: verify cumulative set fact worked (FQCN)
+ assert:
+ that:
+ - dig_list_fqcn == ['one', 'two', 'three', 'four']
diff --git a/test/integration/targets/set_fact/inventory b/test/integration/targets/set_fact/inventory
new file mode 100644
index 00000000..b0c00d32
--- /dev/null
+++ b/test/integration/targets/set_fact/inventory
@@ -0,0 +1,3 @@
+[testgroup]
+testhost ansible_connection=local # no connection is actually established with this host
+localhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"
diff --git a/test/integration/targets/set_fact/nowarn_clean_facts.yml b/test/integration/targets/set_fact/nowarn_clean_facts.yml
new file mode 100644
index 00000000..74f908d0
--- /dev/null
+++ b/test/integration/targets/set_fact/nowarn_clean_facts.yml
@@ -0,0 +1,10 @@
+- name: Test no warnings ref "http://github.com/ansible/ansible/issues/37535"
+ hosts: testhost
+ gather_facts: false
+ tasks:
+ - name: set ssh jump host args
+ set_fact:
+ ansible_ssh_common_args: "-o ProxyCommand='ssh -W %h:%p -q root@localhost'"
+ - name: set ssh jump host args (FQCN)
+ ansible.builtin.set_fact:
+ ansible_ssh_common_args: "-o ProxyCommand='ssh -W %h:%p -q root@localhost'"
diff --git a/test/integration/targets/set_fact/runme.sh b/test/integration/targets/set_fact/runme.sh
new file mode 100755
index 00000000..364798a1
--- /dev/null
+++ b/test/integration/targets/set_fact/runme.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+set -eux
+
+MYTMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir')
+trap 'rm -rf "${MYTMPDIR}"' EXIT
+
+# ensure we can incrementally set fact via loopi, injection or not
+ANSIBLE_INJECT_FACT_VARS=0 ansible-playbook -i inventory incremental.yml
+ANSIBLE_INJECT_FACT_VARS=1 ansible-playbook -i inventory incremental.yml
+
+# ensure we dont have spurious warnings do to clean_facts
+ansible-playbook -i inventory nowarn_clean_facts.yml | grep '[WARNING]: Removed restricted key from module data: ansible_ssh_common_args' && exit 1
+
+# test cached feature
+export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ANSIBLE_CACHE_PLUGIN_PREFIX=prefix_
+ansible-playbook -i inventory "$@" set_fact_cached_1.yml
+ansible-playbook -i inventory "$@" set_fact_cached_2.yml
+
+# check contents of the fact cache directory before flushing it
+if [[ "$(find "${MYTMPDIR}" -type f)" != $MYTMPDIR/prefix_* ]]; then
+ echo "Unexpected cache file"
+ exit 1
+fi
+
+ansible-playbook -i inventory --flush-cache "$@" set_fact_no_cache.yml
+
+# Test boolean conversions in set_fact
+ansible-playbook -v set_fact_bool_conv.yml
+ANSIBLE_JINJA2_NATIVE=1 ansible-playbook -v set_fact_bool_conv_jinja2_native.yml
diff --git a/test/integration/targets/set_fact/set_fact_bool_conv.yml b/test/integration/targets/set_fact/set_fact_bool_conv.yml
new file mode 100644
index 00000000..8df249be
--- /dev/null
+++ b/test/integration/targets/set_fact/set_fact_bool_conv.yml
@@ -0,0 +1,35 @@
+- hosts: localhost
+ gather_facts: false
+ vars:
+ string_var: "no"
+ tasks:
+ - set_fact:
+ this_is_string: "yes"
+ this_is_not_string: yes
+ this_is_also_string: "{{ string_var }}"
+ this_is_another_string: !!str "{% set thing = '' + string_var + '' %}{{ thing }}"
+ this_is_more_strings: '{{ string_var + "" }}'
+
+ - assert:
+ that:
+ - string_var == 'no'
+ - this_is_string == True
+ - this_is_not_string == True
+ - this_is_also_string == False
+ - this_is_another_string == False
+ - this_is_more_strings == False
+
+ - ansible.builtin.set_fact:
+ this_is_string_fqcn: "yes"
+ this_is_not_string_fqcn: yes
+ this_is_also_string_fqcn: "{{ string_var }}"
+ this_is_another_string_fqcn: !!str "{% set thing = '' + string_var + '' %}{{ thing }}"
+ this_is_more_strings_fqcn: '{{ string_var + "" }}'
+
+ - assert:
+ that:
+ - this_is_string_fqcn == True
+ - this_is_not_string_fqcn == True
+ - this_is_also_string_fqcn == False
+ - this_is_another_string_fqcn == False
+ - this_is_more_strings_fqcn == False
diff --git a/test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml b/test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml
new file mode 100644
index 00000000..2642599f
--- /dev/null
+++ b/test/integration/targets/set_fact/set_fact_bool_conv_jinja2_native.yml
@@ -0,0 +1,35 @@
+- hosts: localhost
+ gather_facts: false
+ vars:
+ string_var: "no"
+ tasks:
+ - set_fact:
+ this_is_string: "yes"
+ this_is_not_string: yes
+ this_is_also_string: "{{ string_var }}"
+ this_is_another_string: !!str "{% set thing = '' + string_var + '' %}{{ thing }}"
+ this_is_more_strings: '{{ string_var + "" }}'
+
+ - assert:
+ that:
+ - string_var == 'no'
+ - this_is_string == 'yes'
+ - this_is_not_string == True
+ - this_is_also_string == 'no'
+ - this_is_another_string == 'no'
+ - this_is_more_strings == 'no'
+
+ - ansible.builtin.set_fact:
+ this_is_string_fqcn: "yes"
+ this_is_not_string_fqcn: yes
+ this_is_also_string_fqcn: "{{ string_var }}"
+ this_is_another_string_fqcn: !!str "{% set thing = '' + string_var + '' %}{{ thing }}"
+ this_is_more_strings_fqcn: '{{ string_var + "" }}'
+
+ - assert:
+ that:
+ - this_is_string_fqcn == 'yes'
+ - this_is_not_string_fqcn == True
+ - this_is_also_string_fqcn == 'no'
+ - this_is_another_string_fqcn == 'no'
+ - this_is_more_strings_fqcn == 'no'
diff --git a/test/integration/targets/set_fact/set_fact_cached_1.yml b/test/integration/targets/set_fact/set_fact_cached_1.yml
new file mode 100644
index 00000000..01c9f1e0
--- /dev/null
+++ b/test/integration/targets/set_fact/set_fact_cached_1.yml
@@ -0,0 +1,324 @@
+---
+- name: the first play
+ hosts: localhost
+ tasks:
+ - name: show foobar fact before
+ debug:
+ var: ansible_foobar
+
+ - name: set a persistent fact foobar
+ set_fact:
+ ansible_foobar: 'foobar_from_set_fact_cacheable'
+ cacheable: true
+
+ - name: show foobar fact after
+ debug:
+ var: ansible_foobar
+
+ - name: assert ansible_foobar is correct value
+ assert:
+ that:
+ - ansible_foobar == 'foobar_from_set_fact_cacheable'
+
+ - name: set a non persistent fact that will not be cached
+ set_fact:
+ ansible_foobar_not_cached: 'this_should_not_be_cached'
+
+ - name: show ansible_foobar_not_cached fact after being set
+ debug:
+ var: ansible_foobar_not_cached
+
+ - name: assert ansible_foobar_not_cached is correct value
+ assert:
+ that:
+ - ansible_foobar_not_cached == 'this_should_not_be_cached'
+
+ - name: set another non persistent fact that will not be cached
+ set_fact: "cacheable=no fact_not_cached='this_should_not_be_cached!'"
+
+ - name: show fact_not_cached fact after being set
+ debug:
+ var: fact_not_cached
+
+ - name: assert fact_not_cached is correct value
+ assert:
+ that:
+ - fact_not_cached == 'this_should_not_be_cached!'
+
+ - name: show foobar fact before (FQCN)
+ debug:
+ var: ansible_foobar_fqcn
+
+ - name: set a persistent fact foobar (FQCN)
+ set_fact:
+ ansible_foobar_fqcn: 'foobar_fqcn_from_set_fact_cacheable'
+ cacheable: true
+
+ - name: show foobar fact after (FQCN)
+ debug:
+ var: ansible_foobar_fqcn
+
+ - name: assert ansible_foobar_fqcn is correct value (FQCN)
+ assert:
+ that:
+ - ansible_foobar_fqcn == 'foobar_fqcn_from_set_fact_cacheable'
+
+ - name: set a non persistent fact that will not be cached (FQCN)
+ set_fact:
+ ansible_foobar_not_cached_fqcn: 'this_should_not_be_cached'
+
+ - name: show ansible_foobar_not_cached_fqcn fact after being set (FQCN)
+ debug:
+ var: ansible_foobar_not_cached_fqcn
+
+ - name: assert ansible_foobar_not_cached_fqcn is correct value (FQCN)
+ assert:
+ that:
+ - ansible_foobar_not_cached_fqcn == 'this_should_not_be_cached'
+
+ - name: set another non persistent fact that will not be cached (FQCN)
+ set_fact: "cacheable=no fact_not_cached_fqcn='this_should_not_be_cached!'"
+
+ - name: show fact_not_cached_fqcn fact after being set (FQCN)
+ debug:
+ var: fact_not_cached_fqcn
+
+ - name: assert fact_not_cached_fqcn is correct value (FQCN)
+ assert:
+ that:
+ - fact_not_cached_fqcn == 'this_should_not_be_cached!'
+
+- name: the second play
+ hosts: localhost
+ tasks:
+ - name: show foobar fact after second play
+ debug:
+ var: ansible_foobar
+
+ - name: assert ansible_foobar is correct value
+ assert:
+ that:
+ - ansible_foobar == 'foobar_from_set_fact_cacheable'
+
+ - name: show foobar fact after second play (FQCN)
+ debug:
+ var: ansible_foobar_fqcn
+
+ - name: assert ansible_foobar is correct value (FQCN)
+ assert:
+ that:
+ - ansible_foobar_fqcn == 'foobar_fqcn_from_set_fact_cacheable'
+
+- name: show ansible_nodename and ansible_os_family
+ hosts: localhost
+ tasks:
+ - name: show nodename fact after second play
+ debug:
+ var: ansible_nodename
+ - name: show os_family fact after second play (FQCN)
+ debug:
+ var: ansible_os_family
+
+- name: show ansible_nodename and ansible_os_family overridden with var
+ hosts: localhost
+ vars:
+ ansible_nodename: 'nodename_from_play_vars'
+ ansible_os_family: 'os_family_from_play_vars'
+ tasks:
+ - name: show nodename fact after second play
+ debug:
+ var: ansible_nodename
+ - name: show os_family fact after second play (FQCN)
+ debug:
+ var: ansible_os_family
+
+- name: verify ansible_nodename from vars overrides the fact
+ hosts: localhost
+ vars:
+ ansible_nodename: 'nodename_from_play_vars'
+ ansible_os_family: 'os_family_from_play_vars'
+ tasks:
+ - name: show nodename fact
+ debug:
+ var: ansible_nodename
+
+ - name: assert ansible_nodename is correct value
+ assert:
+ that:
+ - ansible_nodename == 'nodename_from_play_vars'
+
+ - name: show os_family fact (FQCN)
+ debug:
+ var: ansible_os_family
+
+ - name: assert ansible_os_family is correct value (FQCN)
+ assert:
+ that:
+ - ansible_os_family == 'os_family_from_play_vars'
+
+- name: set_fact ansible_nodename and ansible_os_family
+ hosts: localhost
+ tasks:
+ - name: set a persistent fact nodename
+ set_fact:
+ ansible_nodename: 'nodename_from_set_fact_cacheable'
+
+ - name: show nodename fact
+ debug:
+ var: ansible_nodename
+
+ - name: assert ansible_nodename is correct value
+ assert:
+ that:
+ - ansible_nodename == 'nodename_from_set_fact_cacheable'
+
+ - name: set a persistent fact os_family (FQCN)
+ ansible.builtin.set_fact:
+ ansible_os_family: 'os_family_from_set_fact_cacheable'
+
+ - name: show os_family fact (FQCN)
+ debug:
+ var: ansible_os_family
+
+ - name: assert ansible_os_family is correct value (FQCN)
+ assert:
+ that:
+ - ansible_os_family == 'os_family_from_set_fact_cacheable'
+
+- name: verify that set_fact ansible_xxx non_cacheable overrides ansible_xxx in vars
+ hosts: localhost
+ vars:
+ ansible_nodename: 'nodename_from_play_vars'
+ ansible_os_family: 'os_family_from_play_vars'
+ tasks:
+ - name: show nodename fact
+ debug:
+ var: ansible_nodename
+
+ - name: assert ansible_nodename is correct value
+ assert:
+ that:
+ - ansible_nodename == 'nodename_from_set_fact_cacheable'
+
+ - name: show os_family fact (FQCN)
+ debug:
+ var: ansible_os_family
+
+ - name: assert ansible_os_family is correct value (FQCN)
+ assert:
+ that:
+ - ansible_os_family == 'os_family_from_set_fact_cacheable'
+
+- name: verify that set_fact_cacheable in previous play overrides ansible_xxx in vars
+ hosts: localhost
+ vars:
+ ansible_nodename: 'nodename_from_play_vars'
+ ansible_os_family: 'os_family_from_play_vars'
+ tasks:
+ - name: show nodename fact
+ debug:
+ var: ansible_nodename
+
+ - name: assert ansible_nodename is correct value
+ assert:
+ that:
+ - ansible_nodename == 'nodename_from_set_fact_cacheable'
+
+ - name: show os_family fact (FQCN)
+ debug:
+ var: ansible_os_family
+
+ - name: assert ansible_os_family is correct value (FQCN)
+ assert:
+ that:
+ - ansible_os_family == 'os_family_from_set_fact_cacheable'
+
+- name: set_fact ansible_nodename and ansible_os_family cacheable
+ hosts: localhost
+ tasks:
+ - name: set a persistent fact nodename
+ set_fact:
+ ansible_nodename: 'nodename_from_set_fact_cacheable'
+ cacheable: true
+
+ - name: show nodename fact
+ debug:
+ var: ansible_nodename
+
+ - name: assert ansible_nodename is correct value
+ assert:
+ that:
+ - ansible_nodename == 'nodename_from_set_fact_cacheable'
+
+ - name: set a persistent fact os_family (FQCN)
+ ansible.builtin.set_fact:
+ ansible_os_family: 'os_family_from_set_fact_cacheable'
+ cacheable: true
+
+ - name: show os_family fact (FQCN)
+ debug:
+ var: ansible_os_family
+
+ - name: assert ansible_os_family is correct value (FQCN)
+ assert:
+ that:
+ - ansible_os_family == 'os_family_from_set_fact_cacheable'
+
+
+- name: verify that set_fact_cacheable in previous play overrides ansible_xxx in vars
+ hosts: localhost
+ vars:
+ ansible_nodename: 'nodename_from_play_vars'
+ ansible_os_family: 'os_family_from_play_vars'
+ tasks:
+ - name: show nodename fact
+ debug:
+ var: ansible_nodename
+
+ - name: assert ansible_nodename is correct value
+ assert:
+ that:
+ - ansible_nodename == 'nodename_from_set_fact_cacheable'
+
+ - name: show os_family fact (FQCN)
+ debug:
+ var: ansible_os_family
+
+ - name: assert ansible_os_family is correct value (FQCN)
+ assert:
+ that:
+ - ansible_os_family == 'os_family_from_set_fact_cacheable'
+
+- name: the fourth play
+ hosts: localhost
+ vars:
+ ansible_foobar: 'foobar_from_play_vars'
+ ansible_foobar_fqcn: 'foobar_fqcn_from_play_vars'
+ tasks:
+ - name: show example fact
+ debug:
+ var: ansible_example
+
+ - name: set a persistent fact example
+ set_fact:
+ ansible_example: 'foobar_from_set_fact_cacheable'
+ cacheable: true
+
+ - name: assert ansible_example is correct value
+ assert:
+ that:
+ - ansible_example == 'foobar_from_set_fact_cacheable'
+
+ - name: show example fact (FQCN)
+ debug:
+ var: ansible_example_fqcn
+
+ - name: set a persistent fact example (FQCN)
+ set_fact:
+ ansible_example_fqcn: 'foobar_fqcn_from_set_fact_cacheable'
+ cacheable: true
+
+ - name: assert ansible_example_fqcn is correct value (FQCN)
+ assert:
+ that:
+ - ansible_example_fqcn == 'foobar_fqcn_from_set_fact_cacheable'
diff --git a/test/integration/targets/set_fact/set_fact_cached_2.yml b/test/integration/targets/set_fact/set_fact_cached_2.yml
new file mode 100644
index 00000000..7df92244
--- /dev/null
+++ b/test/integration/targets/set_fact/set_fact_cached_2.yml
@@ -0,0 +1,57 @@
+---
+- name: A second playbook run with fact caching enabled
+ hosts: localhost
+ tasks:
+ - name: show ansible_foobar fact
+ debug:
+ var: ansible_foobar
+
+ - name: assert ansible_foobar is correct value when read from cache
+ assert:
+ that:
+ - ansible_foobar == 'foobar_from_set_fact_cacheable'
+
+ - name: show ansible_foobar_not_cached fact
+ debug:
+ var: ansible_foobar_not_cached
+
+ - name: assert ansible_foobar_not_cached is not cached
+ assert:
+ that:
+ - ansible_foobar_not_cached is undefined
+
+ - name: show fact_not_cached fact
+ debug:
+ var: fact_not_cached
+
+ - name: assert fact_not_cached is not cached
+ assert:
+ that:
+ - fact_not_cached is undefined
+
+ - name: show ansible_foobar_fqcn fact (FQCN)
+ debug:
+ var: ansible_foobar_fqcn
+
+ - name: assert ansible_foobar_fqcn is correct value when read from cache (FQCN)
+ assert:
+ that:
+ - ansible_foobar_fqcn == 'foobar_fqcn_from_set_fact_cacheable'
+
+ - name: show ansible_foobar_fqcn_not_cached fact (FQCN)
+ debug:
+ var: ansible_foobar_fqcn_not_cached
+
+ - name: assert ansible_foobar_fqcn_not_cached is not cached (FQCN)
+ assert:
+ that:
+ - ansible_foobar_fqcn_not_cached is undefined
+
+ - name: show fact_not_cached_fqcn fact (FQCN)
+ debug:
+ var: fact_not_cached_fqcn
+
+ - name: assert fact_not_cached_fqcn is not cached (FQCN)
+ assert:
+ that:
+ - fact_not_cached_fqcn is undefined
diff --git a/test/integration/targets/set_fact/set_fact_no_cache.yml b/test/integration/targets/set_fact/set_fact_no_cache.yml
new file mode 100644
index 00000000..f5a99792
--- /dev/null
+++ b/test/integration/targets/set_fact/set_fact_no_cache.yml
@@ -0,0 +1,39 @@
+---
+- name: Running with fact caching enabled but with cache flushed
+ hosts: localhost
+ tasks:
+ - name: show ansible_foobar fact
+ debug:
+ var: ansible_foobar
+
+ - name: assert ansible_foobar is correct value
+ assert:
+ that:
+ - ansible_foobar is undefined
+
+ - name: show ansible_foobar_not_cached fact
+ debug:
+ var: ansible_foobar_not_cached
+
+ - name: assert ansible_foobar_not_cached is not cached
+ assert:
+ that:
+ - ansible_foobar_not_cached is undefined
+
+ - name: show ansible_foobar fact (FQCN)
+ debug:
+ var: ansible_foobar_fqcn
+
+ - name: assert ansible_foobar is correct value (FQCN)
+ assert:
+ that:
+ - ansible_foobar_fqcn is undefined
+
+ - name: show ansible_foobar_not_cached fact (FQCN)
+ debug:
+ var: ansible_foobar_fqcn_not_cached
+
+ - name: assert ansible_foobar_not_cached is not cached (FQCN)
+ assert:
+ that:
+ - ansible_foobar_fqcn_not_cached is undefined