ansible底层文件传输机制中默认模式遇到权限拒绝后启用管道模式可以得到解决

news/2025/10/20 22:43:15/文章来源:https://www.cnblogs.com/muxinq/p/19153847

为啥我在ansible.cf中加入这一配置后就可以ping通了

[ssh_connection]
pipelining = True
[student@master ansible]$ ansible all -m ping 
[WARNING]: sftp transfer mechanism failed on [node2]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [node1]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: scp transfer mechanism failed on [node2]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: scp transfer mechanism failed on [node1]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: piped transfer mechanism failed on [node2]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: piped transfer mechanism failed on [node1]. Use ANSIBLE_DEBUG=1 to see detailed information
node2 | FAILED! => {"msg": "failed to transfer file to /home/student/.ansible/tmp/ansible-local-4122_k3rqmzq/tmp1t0mhe3_ /home/student/.ansible/tmp/ansible-tmp-1760963719.3710346-4127-253941042140370/AnsiballZ_ping.py:\n\ndd: failed to open '/home/student/.ansible/tmp/ansible-tmp-1760963719.3710346-4127-253941042140370/AnsiballZ_ping.py': Permission denied\n"
}
node1 | FAILED! => {"msg": "failed to transfer file to /home/student/.ansible/tmp/ansible-local-4122_k3rqmzq/tmpkfkcz216 /home/student/.ansible/tmp/ansible-tmp-1760963719.397347-4126-192652111529128/AnsiballZ_ping.py:\n\ndd: failed to open '/home/student/.ansible/tmp/ansible-tmp-1760963719.397347-4126-192652111529128/AnsiballZ_ping.py': Permission denied\n"
}cat ansible.cfg
[defaults]
inventory=/home/student/ansible/inventory 
remote_user=student
roles_path=/home/student/ansible/roles
host_key_checking=False 
collections_path=/home/student/ansible/collections 
[privilege_escalation]
become=True 
become_ask_pass=False 
become_method=sudo
become_user=root

这涉及到 Ansible 底层文件传输机制的工作原理。下面将详细解释一下为什么添加 pipelining = True 解决了这个问题。

问题根源分析

从错误信息可以看出,问题出现在文件传输阶段:

failed to transfer file to /home/student/.ansible/tmp/...
dd: failed to open '/home/student/.ansible/tmp/.../AnsiballZ_ping.py': Permission denied

两种传输机制的区别

1. 默认模式 (pipelining = False)

  • Ansible 先将模块代码(如 ping 模块的 AnsiballZ_ping.py)通过 SFTP/SCP 传输到目标节点的临时目录
  • 然后在目标节点上执行该模块
  • 问题:需要写入临时文件的权限,如果目标用户的 home 目录权限严格,就会遇到权限拒绝

2. 管道模式 (pipelining = True)

  • Ansible 不先将文件写入磁盘,而是通过 SSH 管道直接将模块代码传递给 Python 解释器执行
  • 优势:避免了文件写入操作,完全在内存中处理
  • 解决了权限问题,因为不需要在目标节点创建临时文件

具体工作流程对比

默认模式:

# 1. 传输文件到临时目录
scp/sftp AnsiballZ_ping.py node1:/home/student/.ansible/tmp/
# 2. 执行模块
ssh node1 python /home/student/.ansible/tmp/AnsiballZ_ping.py

管道模式:

# 直接通过管道执行,无需临时文件
ssh node1 python -c "$(cat AnsiballZ_ping.py)"

权限问题的根本原因

目标节点上 /home/student/.ansible/tmp/ 目录可能存在权限问题:

  • 目录属主或权限设置不正确
  • SELinux 安全上下文限制
  • 磁盘空间或 inode 问题
  • 先前执行留下的损坏文件

建议的解决方案

除了启用管道模式,还可以考虑:

  1. 检查目标节点 tmp 目录权限:
ansible all -a "ls -ld /home/student/.ansible/tmp/"
  1. 清理临时目录:
ansible all -a "rm -rf /home/student/.ansible/tmp/*"
  1. 在 ansible.cfg 中设置合适的临时目录:
[defaults]
remote_tmp = /tmp/.ansible

管道模式的注意事项

启用 pipelining = True 虽然解决了这个问题,但需要注意:

  • 要求:目标节点的 /etc/sudoers 需要禁用 requiretty
  • 兼容性:某些需要文件操作的模块可能不兼容
  • 性能:通常性能更好,因为减少了文件传输开销
    在此案例中,启用管道模式是正确的解决方案,因为它避免了权限问题并提高了执行效率。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/941687.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20232304 2025-2026-1 《网络与系统攻防技术》实验三实验报告

20232304 2025-2026-1 《网络与系统攻防技术》实验三实验报告 1.实验内容 1.1 实验内容 (1)使用msf编码器以及多种格式Payload生成,veil的evasion功能,利用C+Shellcode编程等技巧 (2)通过组合应用各种技术实现恶意代…

10月20日记

1.今天学习java 2.明天学习工程技艺 3.递归的终止条件怎么确定?会不会栈溢出?

笔记本 光驱 的内部结构及用法: 应急强大的系统启动 (恢复) 光盘 (DVD+R/RW)

笔记本 光驱 的内部结构及用法: 应急强大的系统启动 (恢复) 光盘 (DVD+R/RW)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-fami…

20251020周一日记

20251020周一日记为什么上周五没写日记?因为在集中办公耶! 周五: 1.早上开项目启动大会,一群老总来,有点像誓师大会。开始干活,第一天还没啥人吃零食。 2.下午上课,本来说是要汇报?结果老师微笑着走了忘了这事…

WPF loading data asynchronously and contextmenu save as json in mvvm

Install-Package Prism.Wpf; Install-Package Prism.DryIOC; Install-Package System.Text.Json;<prism:PrismApplication x:Class="WpfApp36.App"xmlns="http://schemas.microsoft.com/winfx/2006/…

Android 源码解析系列1- Android init 进程启动流程

Android 源码解析系列1- Android init 进程启动流程Android 源码解析系列1- Android init 进程启动流程 init进程是用户空间的第一个进程.如下 g1930fua_g210_ai:/ $ ps -ef | grep init #uid pid ppid C…

英语_阅读_Start school_待读

When children start school for the first time, parents often feel a sense of excitement coupled with a touch of sadness at the end of an era. This is the start of a new adventure for children playing a…

2025.10.20总结

今天继续学软考相关内容,目前过完操作系统剩下的相关知识 上午上课的时候进行项目演示有很多的收获,与它们的项目相比我目前的开发而言还存在很多的不足 操作系统作为系统资源的管理者,当然也需要对内存进行管理,要…

10.20总结

class Mammal{} class Dog extends Mammal {} class Cat extends Mammal{} public class TestCast { public static void main(String args[]) { Mammal m; Dog d=new Dog(); Cat c=new Cat(); m=d; //d=m; d=(Dog)m; …

学习相关

博客园主题设置 参考 一个是这个博主的,设置的比较精美:https://www.cnblogs.com/huxingxin/p/16886323.html 还有就是这个博主:https://www.cnblogs.com/zhaohongbing/p/16332606.html 其实自己也想照着这个博主的…

题解:Luogu P2075 区间 LIS

题意 给定长度为 \(n\) 的排列 \(p\),\(q\) 次询问 \(l,r\),求 \(p[l,r]\) 的 LIS 长度。\(1\leq n,q\leq 10^5\)。 题解 挺牛的题。 考虑如何刻画 LIS。感觉上 DP 没有什么前途,考虑另一种经典的 \(\mathcal{O}(n\…

英语_阅读_2050 Space tourism_待读

It is 2050. Space tourism has become a reality. 现在是2050年。太空旅游已经成为现实。 So would you want to be a space tourist? 那么你想成为一名太空游客吗? Here, some middle school students share their…

题解:Luogu P10644 [NordicOI 2022] 能源网格 Power Grid

题意 给定 \(n,m\)。对于 \(n\times m\) 的网格 \(a\),定义 \[c_{i,j}=\left\lvert \sum_{k=1}^{n}a_{k,j}-\sum_{k=1}^{m}a_{i,k} \right\rvert \]现在给定 \(c\),构造一组合法的 \(a\)。数据保证有解。\(1\leq n,m…

题解:Luogu P10004 [集训队互测 2023] Permutation Counting 2

题意 给定 \(n\),对于所有 \(0\leq x,y<n\) 求有多少长度为 \(n\) 的排列 \(p\) 满足 \(\sum\limits_{i=1}^{n-1}[p_i<p_{i+1}]=x\) 且 \(\sum\limits_{i=1}^{n-1}[p^{-1}_i<p^{-1}_{i+1}]=y\),答案对给定的…

题解:Luogu P4143 采集矿石

题意 给定长度为 \(n\) 的字符串 \(s\) 和权值序列 \(v\)。求所有子串 \(s[l,r]\) 使得 \(s[l,r]\) 在所有子串去重后的字典序降序排名,恰好等于 \(v[l,r]\) 的区间和。\(1\leq n\leq 10^5\)。 题解 注意到固定左端点…

从18w到1600w播放量,我的一点思考。

你好呀,我是歪歪。 前几天我想要巩固一下共识算法这个知识点。 (先声明,这篇文章不深入讨论共识算法本身) 于是我在 B 站大学上搜索了“共识算法”这个词:我还特意按照播放量排序了一下,准备先找个播放量高点的视…

扣一个细节问题

请看下这个uint bits = 0; for (int y = 0; y < 5; y++) {for (int x = 0; x < 4; x++)bits |= b[y][x] << (y * 4 + x);}上面的代码把一个[5][4]的byte数组用bits表示,该数组里的元素非0即1. 在经典的Th…

10.20java作业

10.20java作业1.2.3.4.5. 结果Parent: myValue = 10 Child: myValue = 20 Child: myValue = 20 Child: myValue = 20 Child: myValue = 21 第一行输出:new Parent() 创建父类对象,调用父类的 printValue 方法,输出父…

题解:Luogu P14175 【MX-X23-T5】向死存魏

题意 给定长度为 \(n\) 的序列 \(a\) 和值域 \(V\)。有 \(m\) 次操作:给定 \(l,r,x\),将 \(a[l,r]\) 中 \(=x\) 的数改为 \(0\)。 给定 \(x\),在序列末尾添加 \(x\)。 给定 \(l\),查询最小的 \(r\) 使得 \(a[l,r]\…