你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?


当前回答

大家下午好。

我愿在此与大家分享以下几点。

   - name: Validar Directorio
     stat:
       path: /tmp/Sabana
     register: sabana_directorio
   
   - debug:
       msg: "Existe"
     when: sabana_directorio.stat.isdir == sabana_directorio.stat.isdir

   - name: Crear el directorio si no existe.
     file:
       path: /tmp/Sabana
       state: directory
     when: sabana_directorio.stat.exists == false

使用它可以在创建目录之前验证该目录是否存在

其他回答

您可以创建目录。使用

# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755

你也可以咨询 http://docs.ansible.com/ansible/file_module.html 有关目录和文件系统的进一步详细信息。

这里有一个更简单的方法。

—name:创建dir mkdir -p dir dir/a dir/b

您需要文件模块。要创建目录,您需要指定选项状态:directory:

- name: Creates directory
  file:
    path: /src/www
    state: directory

你可以在https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html上看到其他选项

- file:
    path: /etc/some_directory
    state: directory
    mode: 0755
    owner: someone
    group: somegroup

这也是设置权限,所有者和组的方法。最后三个参数不是必须的。

---
- hosts: all
  connection: local
  tasks:
    - name: Creates directory
      file: path=/src/www state=directory

上面的playbook将在/src路径下创建www目录。

在运行上面的剧本之前。请确保您的ansible主机连接应该设置,

“本地主机ansible_connection=本地”

应该出现在/etc/ansible/hosts中

如需更多信息,请与我联系。