如何用单行创建文件? [英] How to create a file with a single line?

查看:24
本文介绍了如何用单行创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件操作的文档似乎没有提到一个模块将允许我用 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

希望能帮到你.

这篇关于如何用单行创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆