2016-09-29 398 views
0

我想从远程主机检索文件。然而,看着计算器上的一些例子之后,底部有两个方法导致后续的错误:Ansible提取报价错误

- shell: ls -f ubuntu_s* 
    register: file_name 
- fetch: src=/home/ubuntu/{{file_name.stdout_lines}} dest=/home/user 


- shell: ls -f ubuntu_s* 
     register: file_name 
    - fetch: src={{item}} dest=/home/user 
     with_items: "{{file_name.stdout_lines}}" 

错误:

ERROR! this task 'fetch' has extra params, which is only allowed in the following modules: command, shell, script, include, include_vars, add_host, group_by, set_fact, raw, meta 

The error appears to have been in '/home/user/BuildPkg.yml': line 49, column 7, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

     register: file_name 
    - fetch: src=/home/ubuntu/{{file_name.stdout_lines}} dest=/home/user 
    ^here 
We could be wrong, but this one looks like it might be an issue with 
missing quotes. Always quote template expression brackets when they 
start a value. For instance: 

    with_items: 
     - {{ foo }} 

Should be written as: 

    with_items: 
     - "{{ foo }}" 


The error appears to have been in '/home/user/BuildPkg.yml': line 49, column 7, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

     register: file_name 
    - fetch: src=/home/ubuntu/{{file_name.stdout_lines}} dest=/home/user 
    ^here 
We could be wrong, but this one looks like it might be an issue with 
missing quotes. Always quote template expression brackets when they 
start a value. For instance: 

    with_items: 
     - {{ foo }} 

Should be written as: 

    with_items: 
     - "{{ foo }}" 

这两种方法都给予同样的错误。什么似乎是错的?

回答

2

尽量避免shell在可能的情况。模块 - 这是Ansible的方式。
如果你需要从远程主机获取文件的列表,并获取他们:

- find: 
    pattern: ubuntu_s* 
    path: /home/ubuntu/ 
    register: myfiles 
- fetch: 
    src: "{{ item.path }}" 
    dest: /home/user 
    flat: yes 
    with_items: "{{ myfiles.files }}" 
+0

这工作得很好!只是一个说明,如果你想传输更大的文件添加成为:没有这个 – latencybit

+0

@ latencybit大文件还有'同步'模块 –

1

正确的做法是遍历文件水珠,如:

- fetch: src={{ item }} dest=/home/user 
    with_fileglob: 
    - ubuntu_s* 

注:know what you may face每当试图解析LS输出

+0

我还需要壳,注册命令? – latencybit

+0

nope,只是上面的一个 –

+2

请记住''with_fileglob'在**本地Ansible主机**上展开!这不适用于从远程主机收集文件列表。 –