【问题标题】:ansible register with loop debug print not working带有循环调试打印的ansible寄存器不起作用
【发布时间】:2016-07-10 12:25:27
【问题描述】:

我有一个简单的剧本,应该显示我的服务状态。我想查看机器的输出以查看状态是否处于活动状态。所以我使用了调试打印,如下所示:

- 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 信息。 如何更好地查看我的命令的输出?

【问题讨论】:

标签: ansible ansible-playbook ansible-2.x


【解决方案1】:

对于在循环中调用的模块(包括调试)(即 with_items),将显示每次迭代时 item 的值。我不知道有什么方法可以关闭它。如果你想减少你的输出,你可以尝试切换到使用 msg 参数到 debug module ,它接受一个 jinja 模板化字符串。你可以做这样的事情显然调整正则表达式以匹配 systemctl 输出。

- name: show values
  debug: msg="{{ item.stdout_lines | replace_regex('^(.*).service.*Active: (.*).$', \\\1 \\\2) }}"
  with_items: out.results

如果您不想使用 replace_regex 函数,您可以考虑编写自己的 filter plugin 以按照您喜欢的方式格式化数据。

一般来说,ansible playbook 不是显示通过寄存器变量、事实等收集的状态信息的好地方。playbook 输出更适合任务状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2020-11-26
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多