如何用单行创建文件?
文件操作的文档似乎没有提到一个模块将允许我用 one 行创建一个文件.最接近的是 lineinfile
但这个模块也插入标记(所以至少三行).
The documentation for file operations does not seem to mention a module which would allow me to create a file with one line. The closest is lineinfile
but this module also inserts markers (so a minimum of three lines).
该行将从变量生成,因此我不想使用传输到目标的本地文件.
The line will be generated from variables so I do not want to use a local file transferred to the target.
有这样的模块吗?或者我应该运行一个 shell 命令,比如
Is there such a module? Or should I run a shell command such as
command: echo {{ myvariable }} > the_file_to_create
生成它?
您可以使用 copy
模块轻松完成,只需使用 force=no
创建一个新的空文件当文件不存在时(如果文件存在,则保留其内容)
You can do it easily with copy
module using force=no
to create a new empty file only when the file does not yet exist (if the file exists, it's content is preserved)
- name: Copy the content
copy:
content: "{{ myvariable }}"
dest: /location/of/the/file
force: no
希望能帮到你.