我想将特定文件从localhost复制到特定远程
服务器。
示例:
file1 ---- server1
file2 ---- server2
file3 ---- server3
尝试了以下解决方案,但是它遍历所有服务器并将文件复制到所有服务器。
deploy.yaml
---
- hosts: dev
gather_facts: True
vars:
version: 1.0.0
tasks:
- name: "copying the service files to remote hosts"
copy:
src: ./abc/{{ item.service }}/{{ item.build }}-{{ version }}.zip
dest: ~/{{ item.build }}-{{ version }}.zip
force: yes
delegate_to: "{{ item.service }}"
with_items:
- { service: "abc", build: "abc-service" }
- { service: "bcd" , build: "bcd-service" }
- { service: "mvn" , build: "mvn-service" }
- { service: "ansible" , build: "ans-service" }
库存
[dev]
abc ansible_ssh_host=12.xx.xx.6 ansible_user=dev
bcd ansible_ssh_host=12.xx.xx.7 ansible_user=dev
mvn ansible_ssh_host=12.xx.xx.3 ansible_user=dev
ansible ansible_ssh_host=12.xx.xx.8 ansible_user=dev
答案 0 :(得分:0)
我猜你可以用很多方法做到这一点。一种方法是在库存中添加变量service
和build
,库存如下:
[dev]
abc ansible_ssh_host=12.xx.xx.6 ansible_user=dev service=abcbuild=abc
bcd ansible_ssh_host=12.xx.xx.7 ansible_user=dev service=bcd build=bcd
mvn ansible_ssh_host=12.xx.xx.3 ansible_user=dev service=mvn build=mvn
ansible ansible_ssh_host=12.xx.xx.8 ansible_user=dev service=ansible build=ans
或者您可以在剧本中使用一个列表变量来包含此信息,具体取决于您在何处/如何存储它。
,您的任务将不再需要with_items
子句:
- name: "copying the service files to remote hosts"
copy:
src: ./abc/{{ service }}/{{ build }}-{{ version }}.zip
dest: ~/{{ build }}-{{ version }}.zip
force: yes
delegate_to: "{{ service }}"
希望有帮助