使用ansible创建多个目录

jug*_*gal 5 ansible ansible-playbook

我想创建具有2个子目录(/ home/test1/bin和/ home/test2/conf)的多个目录(test1,test2),类似于test2.我的剧本看起来像这样:

--
- hosts: localhost
  tasks:
    - name: Create directory
      file: path=/home/{{item}}/bin  state=directory
      file: path=/home/{{item}}/conf state=directory
      with_items:
          - test1
          - test2
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

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": ""}
Run Code Online (Sandbox Code Playgroud)

这是什么问题?我正在使用最新的git checkout.有没有更好的方法来解决这个问题?

ngh*_*nam 13

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

在您的情况下,您应该使用嵌套循环来创建多个目录和子目录.

例:

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