温馨提示×

ansible怎么判断文件是否存在

小亿
132
2023-10-24 18:43:05
栏目: 编程语言

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

0