ansible/plugins/cache/jsonfile_fix.py
Timo Makinen f1dce581c3 Fix permissions for jsonfile cache plugin
By default jsonfile uses mkstemp to create temporary file and it gets
0700 permissions. Explicitly run chmod to fix permissions.
2023-02-05 17:15:31 +00:00

61 lines
1.9 KiB
Python

__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