How to create an empty file with Ansible?

后端 未结 10 755
傲寒
傲寒 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条回答
  • 2021-01-31 13:24

    A combination of two answers, with a twist. The code will be detected as changed, when the file is created or the permission updated.

    - name: Touch again the same file, but dont change times this makes the task idempotent
      file:
        path: /etc/foo.conf
        state: touch
        mode: 0644
        modification_time: preserve
        access_time: preserve
      changed_when: >
        p.diff.before.state == "absent" or
        p.diff.before.mode|default("0644") != "0644"
    

    and a version that also corrects the owner and group and detects it as changed when it does correct these:

    - name: Touch again the same file, but dont change times this makes the task idempotent
      file:
        path: /etc/foo.conf
        state: touch
        state: touch
        mode: 0644
        owner: root
        group: root
        modification_time: preserve
        access_time: preserve
      register: p
      changed_when: >
        p.diff.before.state == "absent" or
        p.diff.before.mode|default("0644") != "0644" or
        p.diff.before.owner|default(0) != 0 or
        p.diff.before.group|default(0) != 0
    
    0 讨论(0)
  • 2021-01-31 13:31

    file module provide way to touch file without modifying its time.

    - name: Touch again the same file, but dont change times this makes the task idempotent
      file:
        path: /etc/foo.conf
        state: touch
        mode: u+rw,g-wx,o-rwx
        modification_time: preserve
        access_time: preserve
    
    

    Reference: https://docs.ansible.com/ansible/latest/modules/file_module.html

    0 讨论(0)
  • 2021-01-31 13:32

    In order to create a file in the remote machine with the ad-hoc command

    ansible client -m file -a"dest=/tmp/file state=touch"
    

    Please correct me if I am wrong

    0 讨论(0)
  • 2021-01-31 13:34

    file: path=/etc/nologin state=touch

    Full equivalent of touch (new in 1.4+) - use stat if you don't want to change file timestamp.

    0 讨论(0)
  • 2021-01-31 13:35

    Something like this (using the stat module first to gather data about it and then filtering using a conditional) should work:

    - stat: path=/etc/nologin
      register: p
    
    - name: create fake 'nologin' shell
      file: path=/etc/nologin state=touch owner=root group=sys mode=0555
      when: p.stat.exists is defined and not p.stat.exists
    

    You might alternatively be able to leverage the changed_when functionality.

    0 讨论(0)
  • 2021-01-31 13:41

    Another option, using the command module:

    - name: Create file
      command: touch /path/to/file
      args:
        creates: /path/to/file
    

    The 'creates' argument ensures that this action is not performed if the file exists.

    0 讨论(0)
提交回复
热议问题