ansible怎么判断文件是否存在

   2025-02-15 8640
核心提示:在Ansible中,您可以使用stat模块来判断文件是否存在。下面是一个使用stat模块的示例:- name: Check if file existsstat:path:

在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"消息。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言