【问题标题】:Erase all lines but one using Ansible使用 Ansible 擦除除一行之外的所有行
【发布时间】:2018-11-22 11:11:41
【问题描述】:

所以我正在使用 Ansible 剧本,我的想法是从调试变量中删除所有行,但保留一个。例如,

- name: Debug
  debug:
    var: json_response

假设 json_response 有 100 多行。我想找到我的具体词并保持线路。像这样:

保留找到单词的那一行:“疯狂”

输出.json:

我正在做这个测试

所以你们可以理解

我在说什么

我是一个疯狂的测试

结果 output.json 将是:

我是一个疯狂的测试

现在,我查看了 ansible 的“lineinfile”模块,它说: "此模块确保特定行在文件中,或使用反向引用的正则表达式替换现有行"

但它似乎不能满足我的需求,有什么想法吗?

谢谢

【问题讨论】:

  • 您的示例看起来不像有效的 JSON。

标签: ansible


【解决方案1】:

如果json_response是正确的json数据,可以使用json_query过滤器搜索关键字:https://docs.ansible.com/ansible/2.7/user_guide/playbooks_filters.html#json-query-filter

否则,如果它是一个普通的多行字符串,您可以使用regex_search 过滤器代替:https://docs.ansible.com/ansible/2.7/user_guide/playbooks_filters.html#regular-expression-filters

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多