【问题标题】:Copy specific file to a specific server from a list of servers从服务器列表中将特定文件复制到特定服务器
【发布时间】:2019-08-15 16:07:09
【问题描述】:

我想将特定文件从 localhost 复制到特定远程 服务器。
示例:

file1 ---- server1

file2 ---- server2

file3 ---- server3

已尝试以下解决方案,但它会遍历所有服务器并将文件复制到所有服务器。

部署.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

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    我猜你可以通过多种方式做到这一点。一种方法是在库存中添加变量 servicebuild,库存如下所示:

    [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 }}"
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2023-03-04
      相关资源
      最近更新 更多