首页 文章

如何使用Ansible创建目录

提问于
浏览
263

如何使用Ansible playbook在基于Debian的系统上在 /srv 创建目录 www

12 回答

  • -7

    你想要文件模块 . 要创建目录,您需要指定选项 state=directory

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

    你可以在http://docs.ansible.com/file_module.html看到其他选项

  • 3

    您甚至可以扩展文件模块,甚至可以通过它设置所有者,组和权限 .

    - name: Creates directory
      file:
        path: /src/www
        state: directory
        owner: www-data
        group: www-data
        mode: 0775
    

    甚至,您可以递归地创建目录:

    - name: Creates directory
      file:
        path: /src/www
        state: directory
        owner: www-data
        group: www-data
        mode: 0775
        recurse: yes
    

    这样,如果它们不存在,它将创建两个目录 .

  • 1

    你可以创建使用:

    最新版本2 <

    - name: Create Folder
      file: 
        path: /srv/www/
        owner: user 
        group: user 
        mode: 0755 
        state: directory
    

    旧版本

    - name: Create Folder
      file: 
       path=/srv/www/
       owner=user 
       group=user 
       mode=0755 
       state=directory
    

    参考 - http://docs.ansible.com/ansible/file_module.html

  • 2

    目录只能使用文件模块创建,因为目录只是一个文件 .

    # create a directory if it doesn't exist
    - file:
        path: /etc/some_directory
        state: directory
        mode: 0755
        owner: foo
        group: foo
    

    -Jayesh

  • 10

    您可以创建目录 . 运用

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

    您还可以参考http://docs.ansible.com/ansible/file_module.html了解更多详细信息regaridng目录和文件系统 .

  • 161

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

    这就是你实际上也可以设置权限,所有者和组的方式 . 最后三个参数不是强制性的 .

  • 2

    您可以使用该声明

    - name: webfolder - Creates web folder file: path=/srv/www state=directory owner=www-data group=www-data mode=0775

  • 460

    只需要设置条件来执行特定分配的任务

    - name: Creates directory
      file: path=/src/www state=directory
      when: ansible_distribution == 'Debian'
    
  • 6

    According to the Ansible documentation,可以通过定义以下内容来完成:

    #如果目录不存在,则创建一个目录

    • 文件:
      path:/ etc / some_directory
      state:目录
      模式:0755
  • 12
    ---
    - hosts: all
      connection: local
      tasks:
        - name: Creates directory
          file: path=/src/www state=directory
    

    上面的playbook将在/ src路径中创建www目录 .

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

    “localhost ansible_connection = local”

    应存在于/ etc / ansible / hosts中

    欲了解更多信息,请告诉我 .

  • 0

    您可以直接运行该命令并使用ansible直接创建

    ansible -v targethostname -m shell -a "mkdir /srv/www" -u targetuser
    

    要么

    ansible -v targethostname -m file -a "path=/srv/www state=directory" -u targetuser
    
  • 4

    这是更简单的方法 .

    - name: create dir command: mkdir -p dir dir/a dir/b

相关问题