summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSloane Hertel <19572925+s-hertel@users.noreply.github.com>2021-02-07 02:05:20 -0500
committerGitHub <noreply@github.com>2021-02-07 01:05:20 -0600
commit6df8a9ec532b8ec85ba776e3fc1675b74f8a9349 (patch)
treebdcc1ffa312380fc3aabd3d3c9248f22f3366bc4
parent148240099aa9bb20f1d7735ba26775658c14d877 (diff)
downloadansible-6df8a9ec532b8ec85ba776e3fc1675b74f8a9349.tar.gz
Fix warning for nonexistent inventory cache (#72840) (#73443)
* Fix inventory cache warning by checking if the key exists before loading it (cherry picked from commit 840bdc1e10f1f0d3c8c0ce4109e9724b466202c0)
-rw-r--r--changelogs/fragments/inventory-cache-file-missing-warning.yaml2
-rw-r--r--lib/ansible/plugins/cache/__init__.py13
-rw-r--r--test/integration/targets/inventory_cache/aliases1
-rw-r--r--test/integration/targets/inventory_cache/cache/.keep0
-rw-r--r--test/integration/targets/inventory_cache/cache_host.yml4
-rw-r--r--test/integration/targets/inventory_cache/plugins/inventory/cache_host.py56
-rwxr-xr-xtest/integration/targets/inventory_cache/runme.sh23
7 files changed, 93 insertions, 6 deletions
diff --git a/changelogs/fragments/inventory-cache-file-missing-warning.yaml b/changelogs/fragments/inventory-cache-file-missing-warning.yaml
new file mode 100644
index 0000000000..3ef58c3e07
--- /dev/null
+++ b/changelogs/fragments/inventory-cache-file-missing-warning.yaml
@@ -0,0 +1,2 @@
+minor_changes:
+ - inventory cache - do not show a warning when the cache file does not (yet) exist.
diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py
index 68b960e106..20df37d8f1 100644
--- a/lib/ansible/plugins/cache/__init__.py
+++ b/lib/ansible/plugins/cache/__init__.py
@@ -319,12 +319,13 @@ class CachePluginAdjudicator(MutableMapping):
def _do_load_key(self, key):
load = False
- if key not in self._cache and key not in self._retrieved and self._plugin_name != 'memory':
- if isinstance(self._plugin, BaseFileCacheModule):
- load = True
- elif not isinstance(self._plugin, BaseFileCacheModule) and self._plugin.contains(key):
- # Database-backed caches don't raise KeyError for expired keys, so only load if the key is valid by checking contains()
- load = True
+ if all([
+ key not in self._cache,
+ key not in self._retrieved,
+ self._plugin_name != 'memory',
+ self._plugin.contains(key),
+ ]):
+ load = True
return load
def __getitem__(self, key):
diff --git a/test/integration/targets/inventory_cache/aliases b/test/integration/targets/inventory_cache/aliases
new file mode 100644
index 0000000000..70a7b7a9f3
--- /dev/null
+++ b/test/integration/targets/inventory_cache/aliases
@@ -0,0 +1 @@
+shippable/posix/group5
diff --git a/test/integration/targets/inventory_cache/cache/.keep b/test/integration/targets/inventory_cache/cache/.keep
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/test/integration/targets/inventory_cache/cache/.keep
diff --git a/test/integration/targets/inventory_cache/cache_host.yml b/test/integration/targets/inventory_cache/cache_host.yml
new file mode 100644
index 0000000000..3630641b46
--- /dev/null
+++ b/test/integration/targets/inventory_cache/cache_host.yml
@@ -0,0 +1,4 @@
+plugin: cache_host
+cache: true
+cache_plugin: jsonfile
+cache_connection: ./cache
diff --git a/test/integration/targets/inventory_cache/plugins/inventory/cache_host.py b/test/integration/targets/inventory_cache/plugins/inventory/cache_host.py
new file mode 100644
index 0000000000..628aba158e
--- /dev/null
+++ b/test/integration/targets/inventory_cache/plugins/inventory/cache_host.py
@@ -0,0 +1,56 @@
+# Copyright (c) 2021 Ansible Project
+# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+DOCUMENTATION = '''
+ inventory: cache_host
+ short_description: add a host to inventory and cache it
+ description: add a host to inventory and cache it
+ extends_documentation_fragment:
+ - inventory_cache
+ options:
+ plugin:
+ required: true
+ description: name of the plugin (cache_host)
+'''
+
+from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable
+import random
+
+
+class InventoryModule(BaseInventoryPlugin, Cacheable):
+
+ NAME = 'cache_host'
+
+ def verify_file(self, path):
+ if not path.endswith(('cache_host.yml', 'cache_host.yaml',)):
+ return False
+ return super(InventoryModule, self).verify_file(path)
+
+ def parse(self, inventory, loader, path, cache=None):
+ super(InventoryModule, self).parse(inventory, loader, path)
+ self._read_config_data(path)
+
+ cache_key = self.get_cache_key(path)
+ # user has enabled cache and the cache is not being flushed
+ read_cache = self.get_option('cache') and cache
+ # user has enabled cache and the cache is being flushed
+ update_cache = self.get_option('cache') and not cache
+
+ host = None
+ if read_cache:
+ try:
+ host = self._cache[cache_key]
+ except KeyError:
+ # cache expired
+ update_cache = True
+
+ if host is None:
+ host = 'testhost{0}'.format(random.randint(0, 50))
+
+ self.inventory.add_host(host, 'all')
+
+ if update_cache:
+ self._cache[cache_key] = host
diff --git a/test/integration/targets/inventory_cache/runme.sh b/test/integration/targets/inventory_cache/runme.sh
new file mode 100755
index 0000000000..098439eb22
--- /dev/null
+++ b/test/integration/targets/inventory_cache/runme.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+
+set -eux
+
+export ANSIBLE_INVENTORY_PLUGINS=./plugins/inventory
+
+cleanup() {
+ for f in ./cache/ansible_inventory*; do
+ if [ -f "$f" ]; then rm -rf "$f"; fi
+ done
+}
+
+trap 'cleanup' EXIT
+
+# Test no warning when writing to the cache for the first time
+test "$(ansible-inventory -i cache_host.yml --graph 2>&1 | tee out.txt | grep -c '\[WARNING\]')" = 0
+writehost="$(grep "testhost[0-9]\{1,2\}" out.txt)"
+
+# Test reading from the cache
+test "$(ansible-inventory -i cache_host.yml --graph 2>&1 | tee out.txt | grep -c '\[WARNING\]')" = 0
+readhost="$(grep 'testhost[0-9]\{1,2\}' out.txt)"
+
+test "$readhost" = "$writehost"