linux Ansible -将文件从本地同步到远程主机

efzxgjgh  于 2023-03-17  发布在  Linux
关注(0)|答案(2)|浏览(122)

我有以下清单:

mta:
  hosts:
    10.1.0.3:
    10.1.0.2:
  vars:
    ansible_user: root
    ansible_ssh_private_key_file: ~/.ssh/play

以及接下来的任务

- name: Synchronization using rsync protocol on delegate host (push)
  ansible.posix.synchronize:
    src: "/home/play/tmp/"
    dest: "ssh://{{ mta_servers }}/tmp"
  loop: "{{ groups['mta'] }}"
  loop_control:
    loop_var: mta_servers
  run_once: true
  register: sync_files

当我运行它的时候

TASK [local : Synchronization using rsync protocol on delegate host ( push )] **********************************************************************************************************************************
failed: [10.1.0.4] (item=10.1.0.3) => {"ansible_loop_var": "mta_servers", "changed": false, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -S none -i /home/play/.ssh/pwned -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --out-format='<<CHANGED>>%i %n%L' /home/play/tmp/ root@10.1.0.4:ssh://10.1.0.3/tmp", "msg": "Warning: Permanently added '10.1.0.4' (ED25519) to the list of known hosts.\r\nrsync: [Receiver] mkdir \"/root/ssh://10.1.0.3/tmp\" failed: No such file or directory (2)\nrsync error: error in file IO (code 11) at main.c(783) [Receiver=3.2.3]\n", "mta_servers": "10.1.0.3", "rc": 11}
failed: [10.1.0.4] (item=10.1.0.2) => {"ansible_loop_var": "mta_servers", "changed": false, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -S none -i /home/play/.ssh/pwned -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --out-format='<<CHANGED>>%i %n%L' /home/play/tmp/ root@10.1.0.4:ssh://10.1.0.2/tmp", "msg": "Warning: Permanently added '10.1.0.4' (ED25519) to the list of known hosts.\r\nrsync: [Receiver] mkdir \"/root/ssh://10.1.0.2/tmp\" failed: No such file or directory (2)\nrsync error: error in file IO (code 11) at main.c(783) [Receiver=3.2.3]\n", "mta_servers": "10.1.0.2", "rc": 11}

需要了解的事实
我以用户play的身份在www.example.com上运行剧本10.1.0.4,然后以root用户身份使用pubkey连接到本地。
我之所以这样做,是因为最终我想在本地主机上生成letsencrypt证书,然后将它们同步到上述服务器。
让我感到困惑的是,无论我选择什么目录作为目标(dest: "ssh://{{ mta_servers }}/tmp"),都仍然会失败。
详细输出-vvv
一个三个三个一个
RHEL9基因

wpcxdonn

wpcxdonn1#

您正在使这个过于复杂,您的dest字符串是不正确的无论如何(无论是在您的问题或在您的评论)。
您所需要做的就是在一个游戏中锁定您的组,并使用自然循环来同步到每个目标主机。

- name: sync my files
  hosts: mta

  tasks:
    - name: push files to target with rsync
      ansible.posix.synchronize:
        src: "/home/play/tmp/"
        dest: "/tmp/"
      register: sync_files
frebpwbc

frebpwbc2#

例如,将localhost * /tmp/foo/ * 同步到两个远程服务器 test_12test_13 上的 * /tmp/bar/ *。

shell> tree /tmp/foo
/tmp/foo
├── file1
└── file2

0 directories, 2 files

行动手册

shell> cat pb.yml
- hosts: test_12,test_13

  tasks:

    - ansible.posix.synchronize:
        src: /tmp/foo/
        dest: /tmp/bar/
      register: sync_files

    - debug:
        var: sync_files.stdout_lines

给予

shell> ansible-playbook pb.yml

PLAY [test_12,test_13] ***********************************************************************

TASK [ansible.posix.synchronize] *************************************************************
changed: [test_12]
changed: [test_13]

TASK [debug] *********************************************************************************
ok: [test_12] => 
  sync_files.stdout_lines:
  - .d..t...... ./
  - <f+++++++++ file1
  - <f+++++++++ file2
ok: [test_13] => 
  sync_files.stdout_lines:
  - .d..t...... ./
  - <f+++++++++ file1
  - <f+++++++++ file2

PLAY RECAP ***********************************************************************************
test_12: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_13: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

查看远程主机上的文件
一个三个三个一个
注:

  • 您的标题为 “从本地同步文件到远程主机”,但任务名称为 “在委派主机上使用rsync协议同步(推送)"。使用rsync协议推送需要在目标上运行rsync守护程序。请参阅 * 注解 *。
  • 推送是默认 * 模式 *
  • 你不需要在目的地URL中指定 ssh 协议。参见注册变量的 cmd 属性,例如:

cmd:/usr/bin/rsync --延迟更新-F --压缩--空运行--存档--rsh ='/usr/bin/ssh -S无-o严格主机密钥检查=no -o用户已知主机文件=/dev/null' --rsync路径='sudo-u根rsync' --输出格式='〈〉%i %n%L' /tmp/foo/ admin@test_12:/tmp/bar/

  • 你不必委托给localhost,控制器是默认的源。
  • 不要迭代主机,让它们并行运行既简单又快速。

相关问题