1# (c) 2017, Brian Coca 2# (c) 2017 Ansible Project 3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 4 5# Make coding more python3-ish 6from __future__ import (absolute_import, division, print_function) 7__metaclass__ = type 8 9DOCUMENTATION = ''' 10 cache: pickle 11 short_description: Pickle formatted files. 12 description: 13 - This cache uses Python's pickle serialization format, in per host files, saved to the filesystem. 14 version_added: "2.3" 15 author: Brian Coca (@bcoca) 16 options: 17 _uri: 18 required: True 19 description: 20 - Path in which the cache plugin will save the files 21 env: 22 - name: ANSIBLE_CACHE_PLUGIN_CONNECTION 23 ini: 24 - key: fact_caching_connection 25 section: defaults 26 _prefix: 27 description: User defined prefix to use when creating the files 28 env: 29 - name: ANSIBLE_CACHE_PLUGIN_PREFIX 30 ini: 31 - key: fact_caching_prefix 32 section: defaults 33 _timeout: 34 default: 86400 35 description: Expiration timeout for the cache plugin data 36 env: 37 - name: ANSIBLE_CACHE_PLUGIN_TIMEOUT 38 ini: 39 - key: fact_caching_timeout 40 section: defaults 41''' 42 43try: 44 import cPickle as pickle 45except ImportError: 46 import pickle 47 48from ansible.module_utils.six import PY3 49from ansible.plugins.cache import BaseFileCacheModule 50 51 52class CacheModule(BaseFileCacheModule): 53 """ 54 A caching module backed by pickle files. 55 """ 56 57 def _load(self, filepath): 58 # Pickle is a binary format 59 with open(filepath, 'rb') as f: 60 if PY3: 61 return pickle.load(f, encoding='bytes') 62 else: 63 return pickle.load(f) 64 65 def _dump(self, value, filepath): 66 with open(filepath, 'wb') as f: 67 # Use pickle protocol 2 which is compatible with Python 2.3+. 68 pickle.dump(value, f, protocol=2) 69