如何使用 Ansible playbook 在基于 Debian 的系统上的 /srv
创建目录 www
?
你想要文件模块。要创建目录,您需要指定选项 state=directory
:
- name: Creates directory
file:
path: /src/www
state: directory
您可以在 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html 中查看其他选项
您甚至可以扩展文件模块,甚至设置所有者、组和通过它的许可。 (参考:Ansible file documentation)
- 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
这样,如果它们不存在,它将创建两个目录。
recursive
参数使得这很像使用 mkdir -p
(对于那些在谷歌上搜索 ansible mkdir -p 的人)。
recurse
参数不喜欢 mkdir -p
。它递归地设置指定的文件属性(仅适用于 state=directory)。如果为 state=directory
,则将创建所有不存在的直接子目录,因为 1.7 将使用提供的权限创建它们。
chmod -R
完全相同。也就是说,如果 path
已经作为目录存在,并且其中有文件,则 recurse
选项(有时很遗憾)也会将这些相同的权限应用于文件。这是 by design,无论好坏。
owner
或 group
指定 recurse
选项,则将应用 chmod -R
(等效)。如果有很多文件,这将成为一个问题。对我来说,我在一个文件夹中有大约 200 个应用程序版本,每个版本都有大约 35k 个文件,加起来有大约 7M 个文件。 chmod 需要很长时间,以至于共享 SSH 连接超时。
对于此处的所有答案,有很多情况需要创建多个目录,因此最好使用循环而不是为每个目录创建单独的任务。
- name: Creates directory
file:
path: "{{ item }}"
state: directory
with_items:
- /srv/www
- /dir/foo
- /dir/bar
您可以使用以下方法创建:
最新版本 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
目录只能使用文件模块创建,因为目录只不过是一个文件。
# create a directory if it doesn't exist
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: foo
group: foo
- file:
path: /etc/some_directory
state: directory
mode: 0755
owner: someone
group: somegroup
这就是您实际上还可以设置权限、所有者和组的方式。最后三个参数不是必须的。
您可以创建一个目录。使用
# create a directory if it doesn't exist
- file: path=/src/www state=directory mode=0755
您还可以参考 http://docs.ansible.com/ansible/file_module.html 了解有关目录和文件系统的更多详细信息。
名称:创建目录 ansible.builtin.file:路径:/etc/some_directory 状态:目录模式:'0755'
只需要设置条件来执行特定分配的任务
- name: Creates directory
file: path=/src/www state=directory
when: ansible_distribution == 'Debian'
你可以使用语句
- name: webfolder - Creates web folder
file: path=/srv/www state=directory owner=www-data group=www-data mode=0775`
如果要在 windows 中创建目录:
name: 创建目录结构 win_file: path: C:\Temp\folder\subfolder> state: directory
enter code here
- name: creating directory in ansible
file:
path: /src/www
state: directory
owner: foo
我们有可用于在 ansible 中创建目录和文件的模块
例子
- name: Creates directory
file:
path: /src/www
state: directory
在这种情况下,您可以使用“文件”模块,您可以为新创建的目录传递很多参数,例如所有者、组、位置、模式等.....
有关文件模块的详细说明,请参阅此文档...
https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
记住这个模块不只是用于创建目录!!!
可以直接运行命令,直接使用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
---
- hosts: all
connection: local
tasks:
- name: Creates directory
file: path=/src/www state=directory
上面的剧本将在 /src 路径中创建 www 目录。
在运行上面的剧本之前。请确保应设置您的 ansible 主机连接,
“本地主机 ansible_connection = 本地”
应该存在于 /etc/ansible/hosts
欲了解更多信息,请告诉我。
创建目录
ansible host_name -m file -a "dest=/home/ansible/vndir state=directory"
使用文件模块创建目录并使用命令“ansible-doc file”获取有关文件模块的详细信息
这是一个选项“状态”,它解释:
如果是目录,则将创建所有直接子目录(如果它们不存在),因为 1.7 将使用提供的权限创建它们。如果是文件,则如果文件不存在,则不会创建该文件,如果您想要该行为,请参阅 [copy] 或 [template] 模块。如果链接,则将创建或更改符号链接。硬链接使用 hard。如果不存在,目录将被递归删除,文件或符号链接将被取消链接。请注意,如果路径不存在,文件将不会失败,因为状态没有改变。如果是 touch(1.4 中的新功能),如果路径不存在,将创建一个空文件,而现有文件或目录将接收更新的文件访问和修改时间(类似于 touch 从命令行工作的方式)。
在 Ansible 中创建目录的最简单方法。
名称:如果您的目录不存在,则创建它。文件:路径:/etc/your_directory
或者
您想为该目录授予 sudo 权限。
名称:如果您的目录不存在,则创建它。文件:路径:/etc/your_directory 模式:'777'
要检查目录是否存在然后运行某些任务(例如创建目录),请使用以下命令
- 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
您可以使用它在创建目录之前验证目录是否存在
/srv/www
路径,而您的示例是关于 /tmp/Sabana
,您应该尽可能尝试将您的示例与问题相匹配,并避免重复 22 个答案中的任何一个。
您可以通过以下方式之一执行此操作:
示例 1:如果父目录已经存在:
- name: Create a new directory www at given path
ansible.builtin.file:
path: /srv/www/
state: directory
mode: '0755'
示例 2:如果父目录不存在:
- name: Create a new directory www at given path recursively
ansible.builtin.file:
path: /srv/www/
state: directory
mode: '0755'
recurse: yes
在示例 2 中,如果两个目录都不存在,它将递归地创建它们。
您可以查看 Official Documentation 以了解有关 file_module
的更多信息
我看到很多 Playbooks 示例,我想提一下 Adhoc 命令示例。
$ansible -i inventory -m file -a "path=/tmp/direcory state=directory (而不是目录我们可以提到touch来创建文件)
在这种情况下,您需要使用文件模块。下面的剧本供您参考。
---
- 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: create dir command: mkdir -p dir dir/a dir/b
-p
它是幂等的
不定期副业成功案例分享
state=directory
,则将创建所有不存在的直接子目录,因为 1.7 将使用提供的权限创建它们。all immediate subdirectories
令人困惑,你能定义它并举个例子吗?