【问题标题】:ansible regex_search exclude specific string from stdout_linesansible regex_search 从 stdout_lines 中排除特定字符串
【发布时间】:2019-07-05 08:26:40
【问题描述】:

我用 ansible 运行了一个 powershell 脚本并返回 stdout,如下所示。我想排除“名称”和“---”如何通过使用 regex_search 来实现?或任何其他可以达到相同目的的方法。任何想法将不胜感激:-)

"output.stdout_lines": [
    "",
    "name                                                                           ",
    "----                                                                           ",
    "Microsoft Visual C++ 2017 x86 Additional Runtime - 14.12.25810                 ",
    "Microsoft Visual C++ 2017 x64 Additional Runtime - 14.12.25810                 ",
    "Microsoft Visual C++ 2017 x86 Minimum Runtime - 14.12.25810                    ",
    "Microsoft Visual C++ 2017 x64 Minimum Runtime - 14.12.25810                    ",
    "",
    ""
]

【问题讨论】:

  • 为什么不从 powershell 命令中删除标头?试试Get-WmiObject -Class Win32_Product | Select Name -ExpandProperty Name
  • 如果还想在 Ansible 中做,when: '"name" not in output.stdout' and when: '"----" not in output.stdout' and when: '"" not in output.stdout'
  • 谢谢它的工作。这是另一种解决方法。

标签: regex search ansible


【解决方案1】:

您可以使用 regex_replace。 “{{output.stdout_lines | regex_replace('name', '')}}”和 "{{output.stdout_lines | regex_replace('---', '')}}"

【讨论】:

  • 感谢它有效,但它似乎也会删除包含 --- 或名称的行。可以删除只开始?像^----?
  • 请尝试如下: msg: "{{output.stdout_lines | regex_replace('---')}}"
猜你喜欢
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
相关资源
最近更新 更多