使用ansible创建多个目录

create multiple directories using ansible

我想为 test2 创建多个目录(test1、test2)和 2 个子目录(/home/test1/bin 和 /home/test2/conf)。我的剧本如下所示:

--
- hosts: localhost
  tasks:
    - name: Create directory
      file: path=/home/{{item}}/bin  state=directory
      file: path=/home/{{item}}/conf state=directory
      with_items:
          - test1
          - test2

但是我收到以下错误:

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/root/ansible/lib/ansible/executor/process/worker.py", line 122, in run
    executor_result = TaskExecutor(host, task, job_vars, new_play_context, self._new_stdin, self._loader, shared_loader_obj).run()
  File "/root/ansible/lib/ansible/executor/task_executor.py", line 89, in run
    items = self._get_loop_items()
  File "/root/ansible/lib/ansible/executor/task_executor.py", line 179, in _get_loop_items
    items = self._shared_loader_obj.lookup_loader.get(self._task.loop, loader=self._loader, templar=templar).run(terms=loop_terms, variables=vars_copy)
  File "/root/ansible/lib/ansible/plugins/lookup/items.py", line 26, in run
    return self._flatten(terms)
TypeError: _flatten() takes exactly 2 arguments (1 given)

fatal: [localhost]: FAILED! => {"failed": true, "stdout": ""}

这里有什么问题?我正在使用最新的 git 结帐。有没有更好的方法来解决这个问题?

我认为出现错误是因为您在 1 个任务中使用了 file 模块 2 次。每个任务只能使用 1 个模块。

在您的情况下,您应该使用 nested loop 创建多个目录和子目录。

示例:

---
- hosts: localhost
  tasks:
    - name: test
      file: path=/tmp/{{item.0}}/{{item.1}} state=directory
      with_nested:
        - ['test1', 'test2']
        - ['bin', 'conf']