summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2020-06-17 14:52:58 -0400
committerGitHub <noreply@github.com>2020-06-17 11:52:58 -0700
commite0322a04203809bf6451f376dc62ed8d15054618 (patch)
tree51dcf9e62fe4a1a4b4a86f6cf286d8e1bd4a4045
parent2cba0dc75c994afa2bc96ada3595f0bdc79b16d8 (diff)
downloadansible-e0322a04203809bf6451f376dc62ed8d15054618.tar.gz
implemented 'prefix' for file based cache (#69872) (#70008)
* implemented 'prefix' for file based cache Co-authored-by: s-hertel <shertel@redhat.com> (cherry picked from commit ebb22655e42f944cfa898488a7cfd3003297ecfe)
-rw-r--r--changelogs/fragments/add_prefix_to_cache.yml2
-rw-r--r--lib/ansible/plugins/cache/__init__.py18
-rw-r--r--test/integration/targets/collections/cache.statichost.yml7
-rwxr-xr-xtest/integration/targets/set_fact/runme.sh9
4 files changed, 30 insertions, 6 deletions
diff --git a/changelogs/fragments/add_prefix_to_cache.yml b/changelogs/fragments/add_prefix_to_cache.yml
new file mode 100644
index 0000000000..ed98052dbf
--- /dev/null
+++ b/changelogs/fragments/add_prefix_to_cache.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - added 'unimplemented' prefix to file based caching
diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py
index fbc4122aca..46e4802217 100644
--- a/lib/ansible/plugins/cache/__init__.py
+++ b/lib/ansible/plugins/cache/__init__.py
@@ -132,6 +132,14 @@ class BaseFileCacheModule(BaseCacheModule):
raise AnsibleError("error in '%s' cache, configured path (%s) does not have necessary permissions (rwx), disabling plugin" % (
self.plugin_name, self._cache_dir))
+ def _get_cache_file_name(self, key):
+ prefix = self.get_option('_prefix')
+ if prefix:
+ cachefile = "%s/%s%s" % (self._cache_dir, prefix, key)
+ else:
+ cachefile = "%s/%s" % (self._cache_dir, key)
+ return cachefile
+
def get(self, key):
""" This checks the in memory cache first as the fact was not expired at 'gather time'
and it would be problematic if the key did expire after some long running tasks and
@@ -142,7 +150,7 @@ class BaseFileCacheModule(BaseCacheModule):
if self.has_expired(key) or key == "":
raise KeyError
- cachefile = "%s/%s" % (self._cache_dir, key)
+ cachefile = self._get_cache_file_name(key)
try:
value = self._load(cachefile)
self._cache[key] = value
@@ -164,7 +172,7 @@ class BaseFileCacheModule(BaseCacheModule):
self._cache[key] = value
- cachefile = "%s/%s" % (self._cache_dir, key)
+ cachefile = self._get_cache_file_name(key)
try:
self._dump(value, cachefile)
except (OSError, IOError) as e:
@@ -175,7 +183,7 @@ class BaseFileCacheModule(BaseCacheModule):
if self._timeout == 0:
return False
- cachefile = "%s/%s" % (self._cache_dir, key)
+ cachefile = self._get_cache_file_name(key)
try:
st = os.stat(cachefile)
except (OSError, IOError) as e:
@@ -200,7 +208,7 @@ class BaseFileCacheModule(BaseCacheModule):
return keys
def contains(self, key):
- cachefile = "%s/%s" % (self._cache_dir, key)
+ cachefile = self._get_cache_file_name(key)
if key in self._cache:
return True
@@ -222,7 +230,7 @@ class BaseFileCacheModule(BaseCacheModule):
except KeyError:
pass
try:
- os.remove("%s/%s" % (self._cache_dir, key))
+ os.remove(self._get_cache_file_name(key))
except (OSError, IOError):
pass # TODO: only pass on non existing?
diff --git a/test/integration/targets/collections/cache.statichost.yml b/test/integration/targets/collections/cache.statichost.yml
new file mode 100644
index 0000000000..b2adcfa63c
--- /dev/null
+++ b/test/integration/targets/collections/cache.statichost.yml
@@ -0,0 +1,7 @@
+# use inventory and cache plugins defined in a content-adjacent collection
+plugin: testns.content_adj.statichost
+hostname: cache_host_a
+cache_plugin: testns.content_adj.custom_jsonfile
+cache: yes
+cache_connection: inventory_cache
+cache_prefix: 'prefix_'
diff --git a/test/integration/targets/set_fact/runme.sh b/test/integration/targets/set_fact/runme.sh
index 426d2f009e..364798a1f5 100755
--- a/test/integration/targets/set_fact/runme.sh
+++ b/test/integration/targets/set_fact/runme.sh
@@ -13,9 +13,16 @@ ANSIBLE_INJECT_FACT_VARS=1 ansible-playbook -i inventory incremental.yml
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}"
+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