如何使用Ansible截断现有的和打开的文件? [英] How to truncate an existing and opened file using Ansible?
本文介绍了如何使用Ansible截断现有的和打开的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
/var/log/secure
。
在纯Linux中,等效项为
truncate -s 0 /var/log/secure
dd if=/dev/zero of=/var/log/secure bs=1M count=0
echo > /var/log/secure
根据
- How to empty ("truncate") a file on Linux that already exists and is protected in someway?
- How to create a file with a given size in Linux?
- How to create a file with ANY given size in Linux?
和Viatail -F /var/log/secure
每个都会导致
==> /var/log/secure <==
tail: /var/log/secure: file truncated
tail: /var/log/secure: file truncated
tail: /var/log/secure: file truncated
而不是
==> /var/log/secure <==
tail: ‘/var/log/secure’ has been replaced; following end of new file
经过测试,似乎Ansible v2.9中的所有可用模块(file
、lineinfile
、copy
等)用新的空文件替换现有文件,但不截断或调整大小并导致已提到的
==> /var/log/secure <==
tail: ‘/var/log/secure’ has been replaced; following end of new file
我知道使用VIAlogrotate
旋转、压缩和删除日志文件可能是更好的解决方案。
推荐答案
可能问题#902没有以这种方式实现。
但是,从Ansible v3.0.0开始,引入了filesize
_module,这是一个围绕dd
的简单包装器,用于在给定文件大小的情况下创建、扩展或截断文件。
它可用于管理交换文件(需要连续块)或大型稀疏文件。
有关详细信息,请参阅/ansible-collections/community.general/blob/main/plugins/modules/files/
下的filesize.py
。
这篇关于如何使用Ansible截断现有的和打开的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文