Ansible检查特定目录是否存在,如果不存在则创建它

问题描述 投票:0回答:1

我试图寻找ansible中是否存在特定的Director,如果不存在,就创建一个...我在这里面临的问题是,下面的代码不应该创建一个vartmpdirectory,但它在运行这个时创建了。(下面的代码不是用来创建目录的,我想)

   tasks:
     - name: Checking /var/tmp/ansible directory exists
       stat: path=/var/tmp/ansible
       register: status

谁能帮我解决这个问题...

ansible ansible-inventory
1个回答
3
投票

你应该使用 文件模块:

- name: Create a directory if it does not exist
  file:
    path: /var/tmp/ansible
    state: directory
    mode: '0755'

没有必要使用 stat 来检查该目录是否不存在,除非你打算用该测试的结果做其他事情。文件模块确保文件在运行任务后处于所需状态。

© www.soinside.com 2019 - 2024. All rights reserved.