解决 Ansible 报错 failed to resolve remote temporary directory
分析 Ansible 在远端主机执行任务时报 failed to resolve remote temporary directory 的权限与空间故障及解决步骤。
问题现象
Ansible 执行 Playbook 或 Ad-hoc 命令时,目标节点全部返回失败:
node-1 | FAILED! => {
"msg": "Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user. See https://docs.ansible.com/ansible/latest/user_guide/become.html#risks-of-becoming-an-unprivileged-user for general explanations of this issue."
}
或输出:
fatal: [node-1]: UNREACHABLE! => {"changed": false, "msg": "Failed to create temporary directory. In some cases, you may have been able to authenticate and did not have permissions on the remote directory. Additional issues may result from a system configuration that prevents creating temporary files or directories (such as no space left on device or a read-only filesystem)."}
触发根因
- 目标远端服务器的磁盘根分区或
/tmp分区已占满(100% 磁盘占用或 Inode 耗尽)。 /tmp挂载点被挂载为noexec或只读(ro)。- 使用
become_user切换为非 root 普通用户时,该用户无权访问 Ansible 创建在/tmp/ansible-tmp-...的中间模块脚本。
解决步骤与操作命令
步骤一:排查远端磁盘空间与 Inode
登录目标节点检查容量:
df -h /tmp
df -i /tmp
若占满,清理旧日志或临时垃圾文件:
sudo rm -rf /tmp/ansible-tmp-*
步骤二:安装 acl 权限支持包
Ansible 切换用户操作需要依赖 POSIX ACL 权限系统。在远端受控节点安装 acl 工具:
# Ubuntu / Debian
sudo apt-get install -y acl
# CentOS / RHEL / AlmaLinux
sudo yum install -y acl
步骤三:修改 ansible.cfg 自定义远程临时目录
如果 /tmp 挂载了 noexec,可在 ansible.cfg 中将临时目录指向其他允许执行的目录:
[defaults]
remote_tmp = ~/.ansible/tmp
local_tmp = ~/.ansible/tmp
allow_world_readable_tmpfiles = True
验证确认
使用 ping 模块测试所有节点连通性:
ansible all -m ping -b
返回 SUCCESS => {"changed": false, "ping": "pong"} 表明临时目录解析与执行环境恢复正常。


