租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

ansible如何判断文件是不是存在,ansible chef

发布时间:2023-10-24 18:43:05

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