vsphere_inventory.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:vsphere_ansible_inventory 作者: mikesimos 项目源码 文件源码
def cached_inventory(self, filters, cache_path=None, cache_ttl=3600, refresh=False, ):
        """
        Wrapper method implementing caching functionality over list_inventory.
        :param dict filters: A dictionary of pyVmomi virtual machine attribute key-value filters.
        :param str cache_path: A path for caching inventory list data. Quite a necessity for large environments.
        :param int cache_ttl: Integer Inventory list data cache Time To Live in seconds. (cache Expiration period)
        :param boolean refresh: Setting this True, triggers a cache refresh. Fresh data is fetched.
        :return: An Ansible pluggable dynamic inventory, as a Python json serializable dictionary.
        """
        if refresh:
            return self.list_and_save(filters, cache_path)
        else:
            if os.path.isfile(cache_path) and time() - os.stat(cache_path).st_mtime < cache_ttl:
                try:
                    with open(cache_path) as f:
                        data = load(f)
                        return data
                except (ValueError, IOError):
                    return self.list_and_save(filters, cache_path)
            else:
                if not os.path.exists(os.path.dirname(cache_path)):
                    try:
                        if cache_path:
                            os.makedirs(os.path.dirname(cache_path))
                        else:
                            raise OSError("[Error] cache_path not defined: {}".format(cache_path))
                    # handle race condition
                    except OSError as exc:
                        if exc.errno == errno.EACCES:
                            print("{}".format(str(exc)))
                            exit(1)
                        elif exc.errno != errno.EEXIST:
                            raise
                return self.list_and_save(filters, cache_path)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号