循环调试打印的无用寄存器不起作用 [英] ansible register with loop debug print not working
问题描述
我有一个简单的剧本,可以显示我的服务状态.我想查看机器的输出以查看状态是否为活动.所以我使用了调试打印,就像这样:
i have a simple playbook that is supposed to display my services status. and i want to view the output from the machine to see if the status is active or not. so i used a debug print, like so:
- name: name_of_services
shell: systemctl status {{item}}
with_items:
- service1
- service2
register: out
- debug: var=item.stdout_lines
with_items: out.results
当我执行此操作时,我会得到很多我不想要的信息,以及我最后想要的item.stdout_lines信息. 如何更好地查看我的命令的输出?
when i execute this i get a lot of info i don't want plus the item.stdout_lines info that i do want in the end of it. how is it possible to view the output of my command better?
推荐答案
对于在循环中调用的模块(包括调试)(即with_items),将显示每次迭代时item的值.我不知道有什么方法可以关闭此功能.如果您希望减少输出,可以尝试使用msg参数切换到调试模块需要一个Jinja模板字符串.您可以执行类似的操作,明显地调整正则表达式以匹配systemctl输出.
For modules, including debug, called in a loop (ie with_items), the value of item at each iteration will be shown. I don't know of a way to turn this off. If you want you reduce your output you can try switching to using the msg parameter to the debug module which takes a jinja templated string. You could do something like this obviously adjusting the regex to match systemctl output.
- name: show values
debug: msg="{{ item.stdout_lines | replace_regex('^(.*).service.*Active: (.*).$', \\\1 \\\2) }}"
with_items: out.results
如果您不想使用replace_regex函数,可以考虑编写自己的过滤器插件以您喜欢的方式格式化数据.
If you don't want to use the replace_regex function you can consider writing your own filter plugin to format the data the way you like it.
通常,可播放的剧本不是显示通过寄存器变量,事实等收集的状态信息的好地方.剧本的输出更适合于任务状态.
In general ansible playbooks aren't a great place to display status information gathered through register vars, facts, etc. The playbook output is more geared toward task status.
这篇关于循环调试打印的无用寄存器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!