How to create an empty file with Ansible?

后端 未结 10 802
傲寒
傲寒 2021-01-31 12:53

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 f

10条回答
  •  旧时难觅i
    2021-01-31 13:48

    The documentation of the file module says

    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.

    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.

提交回复
热议问题