2015-09-23 27 views
5

Tôi muốn tạo nhiều thư mục (test1, test2) với 2 thư mục con (/ home/test1/bin và/home/test2/conf) tương tự cho test2. playbook của tôi trông như thế này:tạo nhiều thư mục bằng cách sử dụng ansible

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

Tuy nhiên tôi nhận được lỗi sau:

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 vấn đề ở đây? Tôi đang sử dụng thanh toán git mới nhất. Có cách nào tốt hơn để tiếp cận điều này?

+0

bạn có thể hiển thị các dòng mã của bạn mà là cho vấn đề này? Rõ ràng bạn đang thiếu một đối số cho một số chức năng. – dasjkdj

+0

@victor Tôi chưa viết bất kỳ mã nào cho công việc trên, tôi chỉ sử dụng các mô-đun ansible để hoàn thành công việc. – jugal

Trả lời

14

Tôi nghĩ rằng lỗi được tăng lên vì bạn đã sử dụng file mô-đun 2 lần trong 1 tác vụ. Bạn chỉ nên sử dụng 1 mô-đun cho mỗi tác vụ.

Trong trường hợp của bạn, bạn nên sử dụng nested loop để tạo nhiều thư mục và thư mục con.

Ví dụ:

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

sử dụng một mô-đun tập tin duy nhất cũng đưa ra cùng một lỗi. Nhưng mã vòng lặp lồng nhau hoạt động như một charm.Thanks !! chỉ là một câu hỏi nhanh - chúng ta có thể tạo cấu trúc thư mục giống nhau trên một máy tính từ xa bằng cách sử dụng ansible không? – jugal

+0

lạ, bạn có thể sử dụng mô-đun tệp hai lần (có lẽ đây là phiên bản cụ thể?) Nhưng 'with_items' chỉ áp dụng cho 'ansible 1.9.3' –

+0

@miked Tôi đang sử dụng phiên bản nhà phát triển mới nhất từ ​​nguồn git (v2. 0). Và đối với máy windows chúng ta có thể sử dụng module win_file. Ban đầu nó đã cho tôi một lỗi phản ứng xấu. Vì vậy, tôi đã phải khởi động lại nút cửa sổ của tôi và sau đó nó hoạt động tốt. – jugal

Các vấn đề liên quan