你如何创建一个目录www在/srv上基于debian的系统使用Ansible剧本?
当前回答
---
- hosts: all
connection: local
tasks:
- name: Creates directory
file: path=/src/www state=directory
上面的playbook将在/src路径下创建www目录。
在运行上面的剧本之前。请确保您的ansible主机连接应该设置,
“本地主机ansible_connection=本地”
应该出现在/etc/ansible/hosts中
如需更多信息,请与我联系。
其他回答
只需要为特定的分配设置执行任务的条件
- name: Creates directory
file: path=/src/www state=directory
when: ansible_distribution == 'Debian'
您需要文件模块。要创建目录,您需要指定选项状态:directory:
- name: Creates directory
file:
path: /src/www
state: directory
你可以在https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html上看到其他选项
使用文件模块创建一个目录,并使用命令“ansible-doc file”获取文件模块的详细信息
这里有一个选项“state”解释:
If directory, all immediate subdirectories will be created if they do not exist, since 1.7 they will be created with the supplied permissions. If file, the file will NOT be created if it does not exist, see the [copy] or [template] module if you want that behavior. If link, the symbolic link will be created or changed. Use hard for hardlinks. If absent, directories will be recursively deleted, and files or symlinks will be unlinked. Note that file will not fail if the path does not exist as the state did not change. If touch (new in 1.4), an empty file will be created if the path does not exist, while an existing file or directory will receive updated file access and modification times (similar to the way touch works from the command line).
可以直接执行该命令,使用ansible直接创建
ansible -v targethostname -m shell -a "mkdir /srv/www" -u targetuser
OR
ansible -v targethostname -m file -a "path=/srv/www state=directory" -u targetuser
要检查目录是否存在,然后运行一些任务(例如创建目录),请使用以下命令
- 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
推荐文章
- 如何删除目录内的文件和文件夹?
- c#测试用户是否有对文件夹的写权限
- 移动一个文件到服务器上的另一个文件夹
- 用Python遍历目录
- 如何删除整个文件夹和内容?
- 如何在Python中获取当前执行文件的路径?
- 如何忽略ansible SSH的真实性检查?
- 如何在Android工作室添加“libs”文件夹?
- 在Python中提取文件路径(目录)的一部分
- Crontab -在目录中运行
- 如何可靠地打开与当前运行脚本在同一目录下的文件
- 如何在一个可行的剧本中只运行一个任务?
- 如何使用Bash创建一个文件夹,如果它还不存在?
- 如何从查找“类型d”中排除此/ current / dot文件夹
- 如何定位父文件夹?