以下是使用Ansible创建目录并检查是否存在的示例代码:
---
- name: Create directory and check existence
hosts: all
gather_facts: false
tasks:
- name: Create directory
file:
path: /path/to/directory
state: directory
register: dir_created
- name: Check directory existence
assert:
that:
- dir_created.stat.exists
fail_msg: "Directory does not exist"
在上述示例中,我们使用Ansible的file
模块创建一个目录,并将结果存储在dir_created
变量中。然后,我们使用assert
模块检查目录是否存在。如果目录不存在,任务将失败并显示错误消息“Directory does not exist”。
请将/path/to/directory
替换为您要创建的目录的实际路径。