Directory 如何使用Ansible创建目录
如何在基于Debian的系统上使用Ansible playbook在Directory 如何使用Ansible创建目录,directory,filesystems,ansible,Directory,Filesystems,Ansible,如何在基于Debian的系统上使用Ansible playbook在/srv处创建目录www?您需要文件模块。要创建目录,需要指定选项state=directory: - name: Creates directory file: path: /src/www state: directory 您可以在上看到其他选项。您甚至可以扩展文件模块,甚至可以通过它设置所有者、组和权限。(参考号:) 甚至,您可以递归地创建目录: - name: Creates directory
/srv
处创建目录www
?您需要文件模块。要创建目录,需要指定选项state=directory
:
- name: Creates directory
file:
path: /src/www
state: directory
您可以在上看到其他选项。您甚至可以扩展文件模块,甚至可以通过它设置所有者、组和权限。(参考号:) 甚至,您可以递归地创建目录:
- name: Creates directory
file:
path: /src/www
state: directory
owner: www-data
group: www-data
mode: 0775
recurse: yes
这样,如果两个目录都不存在,它将创建它们。您可以创建一个目录。使用
# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755
你也可以咨询
有关目录和文件系统的详细信息。您可以使用以下方法创建:
最新版本2您可以使用该语句
- name: webfolder - Creates web folder
file: path=/srv/www state=directory owner=www-data group=www-data mode=0775`
这里有更简单的方法
-名称:创建目录
命令:mkdir-p dir/a dir/b
只需要设置条件来执行特定分发的任务
- name: Creates directory
file: path=/src/www state=directory
when: ansible_distribution == 'Debian'
这是您实际上也可以设置权限、所有者和组的方式。最后三个参数不是强制性的
---
- hosts: all
connection: local
tasks:
- name: Creates directory
file: path=/src/www state=directory
上面的playbook将在/src路径中创建www目录
在跑过剧本之前。请确保您的ansible主机连接已设置
“localhost ansible_连接=本地”
应出现在/etc/ansible/hosts中
有关更多信息,请告诉我。您可以直接运行命令并使用ansible直接创建
ansible -v targethostname -m shell -a "mkdir /srv/www" -u targetuser
或
目录只能使用文件模块创建,因为目录只是一个文件
# create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: foo
group: foo
创建目录
ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"
我们有可用的模块来创建ansible中的目录和文件 范例
- name: Creates directory
file:
path: /src/www
state: directory
如果要在windows中创建目录:
- 名称:创建目录结构
win_文件:
路径:C:\Temp\folder\subfolder>
状态:目录
对于这里的所有答案,在很多情况下需要创建多个目录,因此最好使用循环,而不是为每个目录创建单独的任务
- name: Creates directory
file:
path: "{{ item }}"
state: directory
with_items:
- /srv/www
- /dir/foo
- /dir/bar
使用文件模块创建目录,并使用命令“ansible doc file”获取有关文件模块的详细信息 下面是一个选项“状态”,说明: 如果
目录
,则将创建所有不存在的直接子目录,因为1.7将使用提供的权限创建它们。如果
文件
,如果该文件不存在,则不会创建该文件。如果需要该行为,请参阅[复制]或[模板]模块。如果
链接
,将创建或更改符号链接。使用硬链接
进行硬链接。如果
不存在
,目录将被递归删除,文件或符号链接将被取消链接
请注意,文件
不会在路径不存在时失败,因为状态没有更改
如果触摸(在1.4中新增),如果路径不正确,将创建一个空文件
存在,而现有文件或目录将接收更新的文件
访问和修改时间(类似于touch
从
命令行)
您可以使用“文件”模块在这种情况下,有太多的参数,您可以为新创建的目录传递,如所有者、组、位置、模式等等
有关文件模块的详细说明,请参阅本文档
请记住,此模块不仅仅用于创建目录 在Ansible中创建目录的最简单方法
- 名称:如果您的_目录不存在,请创建它。
文件:
路径:/etc/your\u目录
或
您希望为该目录授予sudo权限
- name: Creates directory
file:
path: "{{ item }}"
state: directory
with_items:
- /srv/www
- /dir/foo
- /dir/bar
- 名称:如果您的_目录不存在,请创建它。
文件:
路径:/etc/your\u目录
模式:“777”
我看到了很多剧本示例,我想提及临时命令示例
$ansible-i inventory-m file-a“path=/tmp/direcory state=目录(我们可以用touch来创建文件,而不是目录)在这种情况下,您需要使用文件模块。下面的playbook可供参考
---
- hosts: <Your target host group>
name: play1
tasks:
- name: Create Directory
files:
path=/srv/www/
owner=<Intended User>
mode=<Intended permission, e.g.: 0750>
state=directory
---
-主持人:
姓名:play1
任务:
-名称:创建目录
文件夹:
路径=/srv/www/
所有者=
模式=
state=目录
要检查目录是否存在,然后运行某些任务(例如创建目录),请使用以下命令
- name: Check if output directory exists
stat:
path: /path/to/output
register: output_folder
- name: Create output directory if not exists
file:
path: /path/to/output
state: directory
owner: user
group: user
mode: 0775
when: output_folder.stat.exists == false
你好,下午好,团队
我与你分享以下内容
- 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
您可以使用它在创建目录之前验证目录是否存在
enter code here
- name: creating directory in ansible
file:
path: /src/www
state: directory
owner: foo
您可以参考
-名称:创建目录
ansible.builtin.file:
路径:/etc/some\u目录
状态:目录
模式:“0755”
参数recursive
使其非常类似于使用mkdir-p
(对于那些在谷歌上搜索ansible mkdir-p的人)。我发现它也会更改子文件权限…几乎像mkdir-p/foo/bar&&chmod-R 0775/foo/bar..有没有其他人在Ansible 2.0.0.2中观察到这一点recurse
参数不喜欢mkdir-p
。它递归地设置指定的文件属性(仅适用于state=directory)。如果state=directory
,则将创建所有不存在的直接子目录,因为1.7将使用提供的权限创建这些子目录。这将使用=
字符的旧语法,最好使用:
语法在新目录上更新每个atributeline@ThePracticalOne-是…使用“递归”“行为方式与chmod-R
完全相同。也就是说,如果path
已经作为dir存在,并且其中有文件,那么recurse
选项(有时很不幸)也会对文件应用相同的权限。不管是好是坏。如果state=directory
,则如果t
---
- hosts: <Your target host group>
name: play1
tasks:
- name: Create Directory
files:
path=/srv/www/
owner=<Intended User>
mode=<Intended permission, e.g.: 0750>
state=directory
- name: Check if output directory exists
stat:
path: /path/to/output
register: output_folder
- name: Create output directory if not exists
file:
path: /path/to/output
state: directory
owner: user
group: user
mode: 0775
when: output_folder.stat.exists == false
- 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
enter code here
- name: creating directory in ansible
file:
path: /src/www
state: directory
owner: foo