ansible如何判断文件是否是存在
在Ansible中,您可使用stat
模块来判断文件是否是存在。下面是一个使用stat
模块的示例:
- name: Check if file exists
stat:
path: /path/to/file
register: result
- name: Print file existence status
debug:
msg: "File exists"
when: result.stat.exists
- name: Print file does not exist status
debug:
msg: "File does not exist"
when: not result.stat.exists
在上面的示例中,stat
模块将检查指定路径的文件是否是存在,并将结果存储在名为result
的变量中。然后,您可使用when
条件语句根据result.stat.exists
的值来履行区分的任务。如果文件存在,第一个debug
任务将被履行,并打印"File exists"消息。如果文件不存在,第二个debug
任务将被履行,并打印"File does not exist"消息。
TOP