使用ansible创建多个目录

使用ansible创建多个目录

本文介绍了使用ansible创建多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为test2类似地创建带有2个子目录(/home/test1/bin和/home/test2/conf)的多个目录(test1,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

但是我遇到以下错误:

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 checkout.有没有更好的方法来解决这个问题?

解决方案

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

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

示例:

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

I want to create multiple directories(test1,test2) with 2 sub directories (/home/test1/bin and /home/test2/conf) similarly for test2. My playbook looks like this :

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

However i get the following error:

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": ""}

Whats the issue here? I'm using the latest git checkout. Is there a better way to approach this?

解决方案

I think that errors raised because you used file module 2 times in 1 task. You should only use 1 module per task.

In your case, you should use nested loop to create multiple directories and subdirectories.

Example:

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

这篇关于使用ansible创建多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:40