问题描述
我有一个简单的剧本,应该显示我的服务状态.我想查看机器的输出以查看状态是否处于活动状态.所以我使用了调试打印,如下所示:
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?
推荐答案
对于包括 debug 在内的模块,在循环中调用(即 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 函数,您可以考虑编写自己的 filter plugin 以您喜欢的方式格式化数据.
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.
一般而言,ansible playbook 不是显示通过寄存器变量、事实等收集的状态信息的好地方. playbook 输出更适合任务状态.
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.
这篇关于带有循环调试打印的 ansible 寄存器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!