浅聊ansible的幂等 file模块源码解析

2024-01-26 16:20

本文主要是介绍浅聊ansible的幂等 file模块源码解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

描述#

  幂等性是在实际应用中经常需要考虑的概念,尤其是运维中。相较于将幂等性理解为各种异常情况的综合处理,将其理解为执行时需要考虑到在前次执行产生的影响的情况下能够正常执行则会更加容易接近业务需求。
  ansible包含众多的模块,大部分内置模块都能够保证操作的幂等性,即相关操作的多次执行能够达到相同结果这一特性,不会出现多次执行带来副作用的影响。但是也有不满足幂等原则的,比如shell模块、raw模块、command模块。

幂等操作和非幂等操作的对比

场景说明:
比如实现删除一个临时性的文件/root/testfile的操作,如果希望其在相同的条件下,多次执行能够保持相同的结果和不会带来其它副作用,至少需要保证此操作在/root/testfile文件存在和不存在的情况下都能正常动作。

# 当采用raw模块执行shell命令删除文件,第一次删除是成功,当执行第二次删除也是成功的,但是在生产环境这结果是不理想的,比如重启一个服务,你会随便重启服务吗?

[root@Server-1~]# touch /root/testfile
[root@Server-1~]# ansible localhost -m shell -a "rm -rf testfile"
localhost | CHANGED | rc=0 >>
[root@Server-1~]# ansible localhost -m shell -a "rm -rf testfile"
localhost | CHANGED | rc=0 >>

# 当采用file 模块执行删除文件,第一次执行删除文件成功changed: true,多次执行删除文件都是同一样的结果,不会带来副作用的影响changed: Fasle

[root@Server-1~]# touch /root/testfile
[root@Server-1~]# ansible localhost -m file -a "path=/root/testfile state=absent"
localhost | CHANGED => {"changed": true, "path": "/root/testfile", "state": "absent"
}
[root@Server-1~]# ansible localhost -m file -a "path=/root/testfile state=absent"
localhost | SUCCESS => {"changed": false, "path": "/root/testfile", "state": "absent"
}
那file模块是如何实现幂等的呢?如下是file模块执行absent文件时的代码(有中文注释)

vim /usr/lib/python2.7/site-packages/ansible/modules/files/file.py
.....
def get_state(path):''' Find out current state '''b_path = to_bytes(path, errors='surrogate_or_strict')try:if os.path.lexists(b_path): # 如果文件存在返回file,文件不存在返回absentif os.path.islink(b_path):return 'link'elif os.path.isdir(b_path):return 'directory'elif os.stat(b_path).st_nlink > 1:return 'hard'# could be many other things, but defaulting to filereturn 'file' return 'absent'except OSError as e:if e.errno == errno.ENOENT:  # It may already have been removedreturn 'absent'else:raisedef ensure_absent(path):b_path = to_bytes(path, errors='surrogate_or_strict')prev_state = get_state(b_path) # 获取文件的状态result = {}if prev_state != 'absent': # 当prev_state='directory' or 'file' 为真diff = initial_diff(path, 'absent', prev_state)if not module.check_mode:if prev_state == 'directory': # 如果prev_state='directory', 则删除目录try:shutil.rmtree(b_path, ignore_errors=False)except Exception as e:raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e)})else:try:os.unlink(b_path) # 如果prev_state='file', 则删除文件except OSError as e:if e.errno != errno.ENOENT:  # It may already have been removedraise AnsibleModuleError(results={'msg': "unlinking failed: %s " % to_native(e),'path': path})result.update({'path': path, 'changed': True, 'diff': diff, 'state': 'absent'}) # 删除文件成功,动作有改变,changed=Trueelse:result.update({'path': path, 'changed': False, 'state': 'absent'}) # 如果prev_state='absent', 动作没有改变,changed=False, 实现多次操作执行不会有任何改变。return resultdef main():global modulemodule = AnsibleModule(argument_spec=dict(state=dict(type='str', choices=['absent', 'directory', 'file', 'hard', 'link', 'touch']),path=dict(type='path', required=True, aliases=['dest', 'name']),_original_basename=dict(type='str'),  # Internal use only, for recursive opsrecurse=dict(type='bool', default=False),force=dict(type='bool', default=False),  # Note: Should not be in file_common_args in futurefollow=dict(type='bool', default=True),  # Note: Different default than file_common_args_diff_peek=dict(type='bool'),  # Internal use only, for internal checks in the action pluginssrc=dict(type='path'),  # Note: Should not be in file_common_args in futuremodification_time=dict(type='str'),modification_time_format=dict(type='str', default='%Y%m%d%H%M.%S'),access_time=dict(type='str'),access_time_format=dict(type='str', default='%Y%m%d%H%M.%S'),),add_file_common_args=True,supports_check_mode=True,)# When we rewrite basic.py, we will do something similar to this on instantiating an AnsibleModulesys.excepthook = _ansible_excepthookadditional_parameter_handling(module.params)params = module.paramsstate = params['state']recurse = params['recurse']force = params['force']follow = params['follow']path = params['path']src = params['src']timestamps = {}timestamps['modification_time'] = keep_backward_compatibility_on_timestamps(params['modification_time'], state)timestamps['modification_time_format'] = params['modification_time_format']timestamps['access_time'] = keep_backward_compatibility_on_timestamps(params['access_time'], state)timestamps['access_time_format'] = params['access_time_format']# short-circuit for diff_peekif params['_diff_peek'] is not None:appears_binary = execute_diff_peek(to_bytes(path, errors='surrogate_or_strict'))module.exit_json(path=path, changed=False, appears_binary=appears_binary)if state == 'file':result = ensure_file_attributes(path, follow, timestamps)elif state == 'directory':result = ensure_directory(path, follow, recurse, timestamps)elif state == 'link':result = ensure_symlink(path, src, follow, force, timestamps)elif state == 'hard':result = ensure_hardlink(path, src, follow, force, timestamps)elif state == 'touch':result = execute_touch(path, follow, timestamps)elif state == 'absent': result = ensure_absent(path) # 执行删除文件时,调用方法 def ensure_absentmodule.exit_json(**result)if __name__ == '__main__':main()

这篇关于浅聊ansible的幂等 file模块源码解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/647364

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组