Ansible脚本从多个服务器复制文件

时间:2019-02-07 04:54:07

标签: ansible

只是想了解您可以使用哪种方法将目录从5-10台远程主机复制到目标(例如跳转服务器)?

我一直在努力的一个是:

---

 - hosts: all
   gather_facts: true

   vars:
     src_path: "/source_path/"
     dest_path: "/destination_path/{{ inventory_hostname }}/"

   tasks:
    - name: Copy a location from REMOTE server to the LOCAL jump box
      tags: sync-pull
      synchronize:
        # The synchronize module forces–delay-updates to avoid leaving a destination in a broken in-between state if the underlying rsync process encounters an error.
        src: "{{ src_path }}"
        dest: "{{ dest_path }}"
        mode: pull       # In pull mode the remote host in context is the source.
        delete: yes      # Delete files in dest that don't exist (after transfer, not before) in the src path. This option requires recursive=yes.
        recursive: yes
        compress: yes    # Default yes.  Compress file data during the transfer
        times: yes       # Preserve modification times
        rsync_timeout: 15 # Specify a --timeout for the rsync command in seconds.
        rsync_opts:
         - "--exclude=.git"
         - "--exclude=*.log"
         - "--exclude=*.log.gz"

仍有许多事情要弄清楚。其中一些是:

1)如何确保根据主机选择路径。即,主机1具有3个source_path。我不确定该怎么做!

2)如果source_path没有必需的权限,则脚本失败并显示错误:

send_files无法打开\“ source_path / iamlog.log \”:权限被拒绝(13)\ 无法弄清楚在这里可以做什么!

1 个答案:

答案 0 :(得分:0)

首先要检查的是您要复制的文件的文件权限,由于运行ansible-playbook的用户无权读取该文件,因此文件似乎没有被复制。

检查

ls -lhtra iamlog.log

最坏的情况暂时将所有人的全部权限授予,以查看是否可行:

chmod 777 iamlog.log

如果这样可以工作,则可能需要将执行ansible-playbook的用户添加到服务器上的本地组中,以授予访问权限或以该文件的用户所有者身份运行。 HTH。

相关问题