【问题标题】:Can't copy files using loop无法使用循环复制文件
【发布时间】:2019-12-15 03:11:29
【问题描述】:

我正在尝试使用 ansible 复制许多文件。 这是我的剧本:

- name: Copy the scenario test 
        copy: 
            src: files/{{ scenario_name }} 
            dest: /home/{{ user }}/scenario_creation 
            mode: '0644' 
            run_once: true 
        loop: "{{ scenario_name }}" 
        tags: 
            - user 
            - scenario 

这是我的角色/scenario_test/defaults/main.yml

scenario_name: ['topup-scenario.json', 'test.json'] 

当我执行我的剧本时,它说:

"msg": "Could not find or access 'files/[u'topup-scenario.json', u'test.json']'\nSearched in:\n\t/home/path/ansible/plays/files/[u'topup-scenario.json', u'test.json']\n\t/home/path/ansible/plays/files/[u'topup-scenario.json', u'test.json'] on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"
}

有什么帮助吗?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    变化:

    src: files/
    

     src: ./files/
    

    【讨论】:

      【解决方案2】:

      您需要将代码更改为:

      - name: Copy the scenario test 
          copy: 
              src: files/{{ item }} 
              dest: /home/{{ user }}/scenario_creation 
              mode: '0644' 
              run_once: true 
          loop: "{{ scenario_name }}" 
          tags: 
              - user 
              - scenario 
      

      循环将列表迭代到术语“item”,除非您使用 loop_var 选项重新定义它。因此,当您在 src 行中调用场景名称时,您实际上是在调用整个列表,而不是它的迭代。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        相关资源
        最近更新 更多