在ansible中,如何在同一台远程机器上同步2个文件夹?
In ansible, how to synchronize 2 folders on the same remote machine?
我有以下简单任务:
正在将文件夹 A 中的所有内容复制到文件夹 B。由于我在一个组中有很多主机,因此我使用以下 yaml 任务定义:
- name: Sync /etc/spark/conf to $SPARK_HOME/conf
synchronize: src=/etc/spark/conf dest={{spark_home}}/conf
delegate_to: "{{item}}"
with_items: "{{play_hosts}}"
tags: spark
然而,运行 ansible-playbook 给了我以下错误:
TASK [cloudera : Sync /etc/spark/conf to $SPARK_HOME/conf] *********************
failed: [52.53.220.119 -> 52.53.200.0] (item=52.53.200.0) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.220.119:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.0", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
failed: [52.53.200.193 -> 52.53.200.0] (item=52.53.200.0) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.193:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.0", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
ok: [52.53.200.0 -> 52.53.200.0] => (item=52.53.200.0)
ok: [52.53.220.119 -> 52.53.220.119] => (item=52.53.220.119)
failed: [52.53.200.193 -> 52.53.220.119] (item=52.53.220.119) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.193:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.220.119", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
failed: [52.53.200.0 -> 52.53.220.119] (item=52.53.220.119) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.0:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.220.119", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
ok: [52.53.200.193 -> 52.53.200.193] => (item=52.53.200.193)
failed: [52.53.220.119 -> 52.53.200.193] (item=52.53.200.193) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.220.119:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.193", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]\n", "rc": 12}
failed: [52.53.200.0 -> 52.53.200.193] (item=52.53.200.193) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.0:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.193", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
显然,ansible 似乎正在尝试在我的所有 3 台主机之间创建置换对并在每对之间进行同步(因此执行了 9 次 rsync),如何避免这种情况并命令 ansible 仅在本地执行 rsync?
更新:我已经将任务定义更改为使用 delegate.host:
- name: Sync /etc/spark/conf to $SPARK_HOME/conf
synchronize: src=/etc/spark/conf dest={{spark_home}}/conf
delegate_to: delegate.host
tags: spark
但是ansible引擎显然没有正确解释,调试日志显示它没有被主机IP地址替代:
ESTABLISH SSH CONNECTION FOR USER: None
SSH: EXEC ssh -C -q -o ControlMaster=auto -o
ControlPersist=60s -o KbdInteractiveAuthentication=no -o
PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey
-o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/peng/.ansible/cp/ansible-ssh-%h-%p-%r
delegate.host '/bin/sh -c '"'"'( umask 77 && mkdir -p "echo $HOME/.ansible/tmp/ansible-tmp-1470667606.38-157157938048153
" &&
echo ansible-tmp-1470667606.38-157157938048153="echo
$HOME/.ansible/tmp/ansible-tmp-1470667606.38-157157938048153
" ) &&
sleep 0'"'"''
这看起来像是一个已弃用的功能,我正在使用 ansible 2.1.0.0
已解决:
- name: Sync /etc/spark/conf to $SPARK_HOME/conf
synchronize: src=/etc/spark/conf dest={{spark_home}} copy_links=true
delegate_to: "{{ inventory_hostname }}"
tags: spark
可能删除了 delegate.host 以支持新变量
我有以下简单任务:
正在将文件夹 A 中的所有内容复制到文件夹 B。由于我在一个组中有很多主机,因此我使用以下 yaml 任务定义:
- name: Sync /etc/spark/conf to $SPARK_HOME/conf
synchronize: src=/etc/spark/conf dest={{spark_home}}/conf
delegate_to: "{{item}}"
with_items: "{{play_hosts}}"
tags: spark
然而,运行 ansible-playbook 给了我以下错误:
TASK [cloudera : Sync /etc/spark/conf to $SPARK_HOME/conf] *********************
failed: [52.53.220.119 -> 52.53.200.0] (item=52.53.200.0) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.220.119:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.0", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
failed: [52.53.200.193 -> 52.53.200.0] (item=52.53.200.0) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.193:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.0", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
ok: [52.53.200.0 -> 52.53.200.0] => (item=52.53.200.0)
ok: [52.53.220.119 -> 52.53.220.119] => (item=52.53.220.119)
failed: [52.53.200.193 -> 52.53.220.119] (item=52.53.220.119) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.193:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.220.119", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
failed: [52.53.200.0 -> 52.53.220.119] (item=52.53.220.119) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.0:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.220.119", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
ok: [52.53.200.193 -> 52.53.200.193] => (item=52.53.200.193)
failed: [52.53.220.119 -> 52.53.200.193] (item=52.53.200.193) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.220.119:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.193", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.0]\n", "rc": 12}
failed: [52.53.200.0 -> 52.53.200.193] (item=52.53.200.193) => {"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/peng/.ssh/saphana.pem -S none -o StrictHostKeyChecking=no' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/etc/spark/conf\" \"52.53.200.0:/opt/spark/spark-1.6.2-bin-hadoop2.4/conf\"", "failed": true, "item": "52.53.200.193", "msg": "Warning: Identity file /home/peng/.ssh/saphana.pem not accessible: No such file or directory.\nPermission denied (publickey).\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", "rc": 255}
显然,ansible 似乎正在尝试在我的所有 3 台主机之间创建置换对并在每对之间进行同步(因此执行了 9 次 rsync),如何避免这种情况并命令 ansible 仅在本地执行 rsync?
更新:我已经将任务定义更改为使用 delegate.host:
- name: Sync /etc/spark/conf to $SPARK_HOME/conf
synchronize: src=/etc/spark/conf dest={{spark_home}}/conf
delegate_to: delegate.host
tags: spark
但是ansible引擎显然没有正确解释,调试日志显示它没有被主机IP地址替代:
ESTABLISH SSH CONNECTION FOR USER: None
SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/peng/.ansible/cp/ansible-ssh-%h-%p-%r delegate.host '/bin/sh -c '"'"'( umask 77 && mkdir -p "
echo $HOME/.ansible/tmp/ansible-tmp-1470667606.38-157157938048153
" && echo ansible-tmp-1470667606.38-157157938048153="echo $HOME/.ansible/tmp/ansible-tmp-1470667606.38-157157938048153
" ) && sleep 0'"'"''
这看起来像是一个已弃用的功能,我正在使用 ansible 2.1.0.0
已解决:
- name: Sync /etc/spark/conf to $SPARK_HOME/conf
synchronize: src=/etc/spark/conf dest={{spark_home}} copy_links=true
delegate_to: "{{ inventory_hostname }}"
tags: spark
可能删除了 delegate.host 以支持新变量