有没有办法使用单个任务在同一目录中创建目录和文件?当前,在我的任务中,我正在使用文件模块state = directory创建目录。如何在同一任务中触摸该目录内的文件?
答案 0 :(得分:1)
您可以使用loop:
- name: Create a directory with a file inside
file:
path: "{{ item.path }}"
state: "{{ item.state }}"
loop:
- { path: /tmp/foo, state: directory }
- { path: /tmp/foo/bar.txt, state: touch }
答案 1 :(得分:0)
@Dom H正确给出了它,我只是想让它在剧本中提供更方便的方式。
剧本看起来像:
---
- name: Creating directory and files
hosts: localhost
become: yes
become_user: root
tasks:
- name: Create a directory with a file inside
file:
path: "{{ item.path }}"
state: "{{ item.state }}"
loop:
- { path: /tmp/foo, state: directory }
- { path: /tmp/foo/bar.txt, state: touch }
我们可以在运行之前进行实际测试:
ansible-playbook -i localhost mkdir.yml --check