How to check if a file exists in Ansible?

后端 未结 11 1874
旧巷少年郎
旧巷少年郎 2020-12-23 02:35

I have to check whether a file exists in /etc/. If the file exists then I have to skip the task. Here is the code I am using:

- name: checking th         


        
11条回答
  •  长情又很酷
    2020-12-23 03:17

    In general you would do this with the stat module. But the command module has the creates option which makes this very simple:

    - name: touch file
      command: touch /etc/file.txt
      args:
        creates: /etc/file.txt
    

    I guess your touch command is just an example? Best practice would be to not check anything at all and let ansible do its job - with the correct module. So if you want to ensure the file exists you would use the file module:

    - name: make sure file exists
      file:
        path: /etc/file.txt
        state: touch
    

提交回复
热议问题