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.
This commit is contained in:
Timo Makinen 2023-02-05 17:15:31 +00:00
parent 2036276402
commit f1dce581c3
3 changed files with 65 additions and 1 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.*.swp
__pycache__

View file

@ -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

61
plugins/cache/jsonfile_fix.py vendored Normal file
View file

@ -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