带有 {{ item }} 的 Ansible 复制模块

0 copy ansible

我有一个像这样的变量的 yml 文件。

- newHosts     
   - hostIP: 192.168.1.22
     filename: file1 
   - hostIP: 192.168.1.23   
     filename: file2   
Run Code Online (Sandbox Code Playgroud)

我正在使用 add_host: {{ item.hostIP }} with_items {{ newHosts }}。我想用 {{ item.filename }} 之类的东西将各自的文件复制到各自的主机,但它将所有文件复制到每个主机。我如何只将相应的文件复制到节点。我怎样才能做到这一点?

Pet*_*026 6

您可以使用在循环的每次迭代中应用的条件,例如:

- hosts: all
  tasks:
  - name: copy file to appropriate server
    copy: src={{item.filename}} dest=/var/foo/{{item.filename}}
    with_items: newHosts
    when: item.hostIP == ansible_ssh_host
Run Code Online (Sandbox Code Playgroud)