2020年2月13日 星期四

[Ansible 文章收集] How to check if a file/directory exists in Ansible

Source From Here
Preface
To get the details of a file or directory in a Linux system, we can use the Ansible stat module. It works similar to the Linux ‘stat’ command. Of course, the module provides much more detail than whether a file exists or not. We can know when the file was last modified, what all permissions the invoking user has about the file, whether it is a directory etc.

If you just need to check the status of the file to create a new file, then you should use the file module. It will create a file or directory only if the file does not exist. I will show you an example below. For more details on how to create directories in Ansible, you can refer this post.

The stat module needs the path of the file, directory or symlink to be checked at the minimum. The stat module returns all the details regarding the object specified in the path parameter. We need to store this in a register. Then using the return values and the ‘when’ condition statements, we can check various properties of the files.

Checking if the file exists or not exists
Let’s see a primary example of how to check if an object, file/directory, exists using the stat module.

In the following example, I am checking the status of ‘prompt.yaml’ file. I am storing the details of the stat module execution in a register named ‘file_details’. The module returns a lot of return values, and one of them is ‘exists’. It will return a boolean value, true or false, depending on the status of the file.
  1. - hosts: all  
  2.   tasks:  
  3.   - name: Ansible check file exists example.  
  4.     stat:  
  5.       path: /Users/mdtutorials2/Documents/Ansible/prompt.yaml  
  6.     register: file_details  
  7.   
  8.   - debug:  
  9.       msg: "The file or directory exists"  
  10.     when: file_details.stat.exists  
  11.     # check file not exist as below  
  12.     # when: not file_details.stat.exists   
The above example will give the output whether it is a file or directory or symlink. So how to check if a particular object exists and it is not a directory or symlink. We can use the ‘isreg‘ return value for checking if its a return value. It will be true if the path is a regular file.
  1. - debug:  
  2.     msg: "Ansible file exists example"  
  3.   when: file_details.stat.exists and file_details.stat.isreg  
Another way is to combine the ‘isdir‘ and ‘islnk‘ return values. The below task shows how it can be done.
  1. - debug:  
  2.      msg: "It is a file"  
  3.    when: file_details.stat.exists and file_details.stat.isdir == false and file_details.stat.islnk == false  
Checking if a directory exists
For checking if a particular object is a directory and also it exists you can combine the ‘exists’ and ‘isdir’ return values. The ‘isdir’ value will be true if the object is a directory and else false. For example, in the below case I have given the path for ‘test1’ object. If it is not a directory, the task will be skipped.
  1. - hosts: all  
  2.   tasks:  
  3.   - name: Ansible check directory exists example.  
  4.     stat:  
  5.       path: /Users/dnpmacpro/Documents/Ansible/test1  
  6.     register: files_to_delete  
  7.   
  8.   - debug:  
  9.       msg: "It is a directory"  
  10.     when: files_to_delete.stat.exists and files_to_delete.stat.isdir  
Supplement
FAQ - How can I run a ansible task only if a file or directory does NOT exist?

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...