Ansible获取引用错误

时间:2016-09-29 22:04:51

标签: ansible fetch ansible-playbook

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

- 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 个答案:

答案 0 :(得分: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 }}"

答案 1 :(得分:1)

正确的方法是循环文件全局,例如:

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

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