How to check if a file exists in Ansible?

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

    This can be achieved with the stat module to skip the task when file exists.

    - hosts: servers
      tasks:
      - name: Ansible check file exists.
        stat:
          path: /etc/issue
        register: p
      - debug:
          msg: "File exists..."
        when: p.stat.exists
      - debug:
          msg: "File not found"
        when: p.stat.exists == False
    

提交回复
热议问题