服务器已安装rsync但Ansible无法定位,如何解决?
Ansible synchronize模块提示找不到rsync的问题排查与解决
问题场景
我在Ansible中执行以下synchronize推送任务:
- name: Transfer package from server to target server synchronize: src: "{{ item }}" dest: "{{ item }}" mode: push with_items: - "/opt/screener/{{PACKAGE_NAME}}"
但任务报错:
Failed to find required executable "rsync" in paths:
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
但本地控制节点中rsync已正常安装:
[server ~]$ rsync --version rsync version 3.1.2 protocol version 31 Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes, prealloc rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. [server ~]$ whereis rsync rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz
请问是否需要手动映射rsync路径?该如何操作?或者还有其他解决办法吗?
解决办法
1. 先确认目标节点是否安装rsync
synchronize的mode: push是从控制节点推送文件到目标节点,报错提示的是目标节点找不到rsync,而非你本地检查的控制节点。先登录目标节点执行rsync --version和whereis rsync,如果没装,直接在目标节点安装:
- RHEL/CentOS系:
yum install rsync -y - Debian/Ubuntu系:
apt install rsync -y
2. 手动指定rsync路径(目标节点路径不在默认PATH时)
如果目标节点已安装rsync,但路径不在Ansible检测的PATH里,可通过synchronize的rsync_path参数指定完整路径:
- name: Transfer package from server to target server synchronize: src: "{{ item }}" dest: "{{ item }}" mode: push rsync_path: "/usr/bin/rsync" # 替换为目标节点的rsync实际路径 with_items: - "/opt/screener/{{PACKAGE_NAME}}"
3. 调整Ansible执行时的PATH环境变量
如果Ansible以非root用户执行任务,该用户的PATH可能不包含rsync路径,可通过environment参数临时补充:
- name: Transfer package from server to target server synchronize: src: "{{ item }}" dest: "{{ item }}" mode: push with_items: - "/opt/screener/{{PACKAGE_NAME}}" environment: PATH: "/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin" # 确保包含rsync所在路径
4. 检查目标节点rsync的可执行权限
确保目标节点上的rsync二进制文件有可执行权限:
chmod +x /usr/bin/rsync
内容的提问来源于stack exchange,提问作者rishi_stark
相关产品推荐
相关产品推荐

