要将命令(在本例中为echo hi)的标准输出写入文件,您可以执行以下操作:
echo hi > outfile我想要一个命令,而不是重定向或管道,这样我就不需要调用shell。这最终用于调用python的subprocess.POpen的Ansible。
我正在寻找:
stdout-to-file outfile echo hitee使将标准输出复制到文件变得非常容易,但它接受标准输入,而不是单独的命令。
有没有一个通用的、可移植的命令可以做到这一点?当然,写一个很容易,但这不是问题所在。最后,在Ansible中,我想做的是:
command: to-file /opt/binary_data base64 -d {{ base64_secret }}而不是:
shell: base64 -d {{ base64_secret }} > /opt/binary_data编辑:查找RHEL 7、Fedora 21上可用的命令
发布于 2015-02-28 12:26:14
您实际上正在寻找的是一个Ansible模块,它有两个参数,
的输出
在这种情况下,您可以使用shell模块而不是允许此类重定向的command模块。
例如:
- shell: /usr/bin/your_command >> output.log这是最简单的。我相信你已经意识到了这一点。我只是想让任何刚接触shell/命令模块的人都能读到这个线程。
如果你不喜欢这样做,你仍然可以写一个包装器模块,它接受“文件名”作为参数,
- custommodule: output.log /usr/bin/your_command您可能需要做的就是派生代码库,查看现有模块,并相应地定制您的模块。
发布于 2016-05-13 13:06:53
不知道这是不是你想要的,但是在Ansible - Save registered variable to file中我找到了我需要的东西:
- name: "Gather lsof"
command: lsof
register: lsof_command
- name: "Save lsof log"
local_command:
copy content="{{ lsof_command.stdout }}" dest="/root/lsof.log"或者,在我的特定情况下(可能对您也有用),playbook在系统A上运行,但我需要来自B的日志并将其保存到localhost (因为A系统正在访问B,而我想要记录B的状态):
- name: "Gather lsof on B"
delegate_to: B
command: lsof
register: lsof_command
run_once: true
- name: "Save lsof log"
local_action:
copy content="{{ lsof_command.stdout }}" dest="/root/lsof.log"
run_once: true在我的案例中需要IMO run_once: true,因为我希望每次运行playbook时只收集一次日志(如果playbook在10个系统上运行,则不会说10次)。
改进的空间将是保存stderr,或者当"Save lsof log“任务不为空时可能会失败。
https://stackoverflow.com/questions/28777306
复制相似问题