如何使用 Ansible 创建一个空文件? [英] How to create an empty file with Ansible?
问题描述
使用 Ansible 创建空文件的最简单方法是什么?我知道我可以将一个空文件保存到 files
目录中,然后将其复制到远程主机,但我觉得这有点不令人满意.
What is the easiest way to create an empty file using Ansible? I know I can save an empty file into the files
directory and then copy it to the remote host, but I find that somewhat unsatisfactory.
另一种方法是触摸远程主机上的文件:
Another way is to touch a file on the remote host:
- name: create fake 'nologin' shell
file: path=/etc/nologin state=touch owner=root group=sys mode=0555
但是后来每次都碰到文件,在日志中显示为一条黄线,也不尽人意...
But then the file gets touched every time, showing up as a yellow line in the log, which is also unsatisfactory...
对于这个简单的问题,有没有更好的解决方案?
Is there any better solution to this simple problem?
推荐答案
文件模块的文档说
如果 state=file
,如果文件不存在,则不会创建该文件,如果需要该行为,请参阅副本或模板模块.
If
state=file
, the file will NOT be created if it does not exist, see the copy or template module if you want that behavior.
所以我们使用复制模块,使用force=no
仅在文件尚不存在时创建新的空文件(如果文件存在,则保留其内容).
So we use the copy module, using force=no
to create a new empty file only when the file does not yet exist (if the file exists, its content is preserved).
- name: ensure file exists
copy:
content: ""
dest: /etc/nologin
force: no
group: sys
owner: root
mode: 0555
这是一个声明式且优雅的解决方案.
This is a declarative and elegant solution.
这篇关于如何使用 Ansible 创建一个空文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!