如何在 Ansible playbook 中简洁地提取或合并哈希值
为了简化许多简单情况下的打印 shell 输出,我想要一个快速的 Ansible Playbook 片段来仅显示 stdout_lines
和 stderr_lines
- 但仍然保持它们不同。
- name: demo output streams
shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
register: tout
- debug:
var: tout
给出这样的:
TASK [debug] **************************************************************
ok: [remote] => {
"tout": {
"changed": true,
"cmd": "echo hi stdout; echo ho stderr 1>&2; false; true",
"delta": "0:00:00.011020",
"end": "2022-02-24 16:19:25.812732",
"failed": false,
"msg": "",
"rc": 0,
"start": "2022-02-24 16:19:25.801712",
"stderr": "ho stderr",
"stderr_lines": [
"ho stderr"
],
"stdout": "hi stdout",
"stdout_lines": [
"hi stdout"
]
}
}
我想要的是像这样更简洁的东西,或者可能是拆分的,但仍然由 Playbook 中的单个调试任务引用。 (集成到 shell 任务本身会更理想,但我想这是不可能的。)
TASK [debug] **************************************************************
ok: [remote] => {
"tout": {
"stderr_lines": [
"ho stderr"
],
"stdout_lines": [
"hi stdout"
]
}
}
false
和 true
的绒毛只是表明我只是想看看命令输出,无需 Ansible 担心成功或失败。
当然,我可以使用多个调试,但是当存在许多此类快速信息任务时,该输出会变得很麻烦。
To simplify printing shell output in many simple situations, I'd like a quick Ansible Playbook snippet to show just the stdout_lines
and stderr_lines
— but still keeping them distinct.
- name: demo output streams
shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
register: tout
- debug:
var: tout
gives this:
TASK [debug] **************************************************************
ok: [remote] => {
"tout": {
"changed": true,
"cmd": "echo hi stdout; echo ho stderr 1>&2; false; true",
"delta": "0:00:00.011020",
"end": "2022-02-24 16:19:25.812732",
"failed": false,
"msg": "",
"rc": 0,
"start": "2022-02-24 16:19:25.801712",
"stderr": "ho stderr",
"stderr_lines": [
"ho stderr"
],
"stdout": "hi stdout",
"stdout_lines": [
"hi stdout"
]
}
}
What I want is something more concise like this, or perhaps split, but still referenced by a single debug task in the Playbook. (Integrated into the shell task itself would be even more ideal, but I guess that's off the table.)
TASK [debug] **************************************************************
ok: [remote] => {
"tout": {
"stderr_lines": [
"ho stderr"
],
"stdout_lines": [
"hi stdout"
]
}
}
The false
and true
fluff just indicates that I simply want to see the command outputs, without Ansible-worrying about success or failure.
Of course I could use multiple debug
's, but that output becomes cumbersome when there are many of these quick-info tasks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以做这样的事情:
或者
如果你想要一些易于阅读的东西,创建一个自定义插件:(这里只有std)
剧本来使用它的
结果:
you could do something like this:
or using
if you want something easy to read, create a custom plugin: (here onlystd)
playbook to use it
results:
使用
selectattr
过滤由过滤器dict2items
在你的字典上。然后使用items2dict
。鉴于:
这将产生:
Use
selectattr
to filter the keys given by the result of the filterdict2items
on your dictionary. Then swap it back to a dictionary withitems2dict
.Given:
This would yield: