2017-03-17 43 views
1

我试图找到一个例子,我可以从serverA将文件拉到一组服务器。
mygroup由10台服务器组成。需要将该文件复制到这10台服务器上。 这是我有,但它不完全工作。如果没有处理程序部分,我可以一对一地复制。如何使用同步模块将文件复制到许多服务器

- hosts: serverA 
    tasks: 
- name: Transfer file from serverA to mygroup 
    synchronize: 
    src: /tmp/file.txt 
    dest: /tmp/file.txt 
    mode: pull 

    handlers: 
- name: to many servers 
    delegate_to: $item 
    with_items: ${groups.mygroup} 

回答

1

您应该仔细阅读关于ansible作品(什么是主机模式,什么是策略,什么是处理程序......)的文档。

这里的回答你的问题:

--- 
# Push mode (connect to xenial1 and rsync-push to other hosts) 
- hosts: xenial-group:!xenial1 
    gather_facts: no 
    tasks: 
    - synchronize: 
     src: /tmp/hello.txt 
     dest: /tmp/hello.txt 
     delegate_to: xenial1 

# Pull mode (connect to other hosts and rsync-pull from xenial1) 
- hosts: xenial1 
    gather_facts: no 
    tasks: 
    - synchronize: 
     src: /tmp/hello.txt 
     dest: /tmp/hello.txt 
     mode: pull 
     delegate_to: "{{ item }}" 
     with_inventory_hostnames: xenial-group:!xenial1 

库存:

[xenial-group] 
xenial1 ansible_ssh_host=192.168.168.186 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes' 
xenial2 ansible_ssh_host=192.168.168.187 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes' 
xenial3 ansible_ssh_host=192.168.168.188 ansible_user=ubuntu ansible_ssh_extra_args='-o ForwardAgent=yes' 

记住synchronizersync的包装,因此,对于这种设置工作,必须有SSH-目标主机之间的连接(通常你在控制主机和目标主机之间有ssh连接)。我为此使用代理转发。

+0

谢谢。在我发布之前,我确实阅读了大量文档。我发现缺乏实例的可靠文档。有些人从阅读课本中学习,有些人喜欢用书中的例子学习。 –

相关问题