Ansible 获取报价错误

Ansible Fetch Quote Error

我正在尝试从远程主机检索文件。但是,在查看了 Whosebug 上的一些示例之后,最下面的两种方法会导致以下错误:

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

两种方法都给出相同的错误。好像哪里不对?

正确的方法是遍历文件 glob,例如:

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

注意:know what you may face 每当尝试解析 ls 输出时

尽可能避免shell。模块——这是 Ansible 的方式。
如果您需要从远程主机获取文件列表并获取它们:

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