How to check if a file exists in Ansible?

后端 未结 11 1827
旧巷少年郎
旧巷少年郎 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:20

    You can use Ansible stat module to register the file, and when module to apply the condition.

    - name: Register file
          stat:
            path: "/tmp/test_file"
          register: file_path
    
    - name: Create file if it doesn't exists
          file: 
            path: "/tmp/test_file"
            state: touch
          when: file_path.stat.exists == False
    

提交回复
热议问题