diff --git a/.gitignore b/.gitignore index a01ee28..d513b9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .*.swp +__pycache__ diff --git a/ansible.cfg b/ansible.cfg index c64cbd7..6cbe1c8 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -2,8 +2,10 @@ inventory = hosts.yml roles_path = ./roles:./software +cache_plugins = ./plugins/cache + gathering = smart -fact_caching = jsonfile +fact_caching = jsonfile_fix fact_caching_connection = facts fact_caching_timeout = 86400 diff --git a/plugins/cache/jsonfile_fix.py b/plugins/cache/jsonfile_fix.py new file mode 100644 index 0000000..2f5e175 --- /dev/null +++ b/plugins/cache/jsonfile_fix.py @@ -0,0 +1,61 @@ +__metaclass__ = type + +DOCUMENTATION = ''' + options: + _uri: + required: True + description: + - Path in which the cache plugin will save the JSON files + env: + - name: ANSIBLE_CACHE_PLUGIN_CONNECTION + ini: + - key: fact_caching_connection + section: defaults + type: path + _prefix: + description: User defined prefix to use when creating the JSON files + env: + - name: ANSIBLE_CACHE_PLUGIN_PREFIX + ini: + - key: fact_caching_prefix + section: defaults + _timeout: + default: 86400 + description: Expiration timeout for the cache plugin data + env: + - name: ANSIBLE_CACHE_PLUGIN_TIMEOUT + ini: + - key: fact_caching_timeout + section: defaults + type: integer +''' + +import os +import tempfile + +from ansible.plugins.cache.jsonfile import CacheModule as JsonCacheModule + + +class CacheModule(JsonCacheModule): + + def set(self, key, value): + + self._cache[key] = value + + cachefile = self._get_cache_file_name(key) + tmpfile_handle, tmpfile_path = tempfile.mkstemp(dir=os.path.dirname(cachefile)) + try: + try: + self._dump(value, tmpfile_path) + except (OSError, IOError) as e: + display.warning("error in '%s' cache plugin while trying to write to '%s' : %s" % (self.plugin_name, tmpfile_path, to_bytes(e))) + try: + os.chmod(tmpfile_path, 0o644) + os.rename(tmpfile_path, cachefile) + except (OSError, IOError) as e: + display.warning("error in '%s' cache plugin while trying to move '%s' to '%s' : %s" % (self.plugin_name, tmpfile_path, cachefile, to_bytes(e))) + finally: + try: + os.unlink(tmpfile_path) + except OSError: + pass