Armandus Filtch
Armandus Filtch

Reputation: 77

Erase all lines but one using Ansible

So I'm working with an Ansible playbook and my idea is to remove all lines from a debug variable but keep one. For example,

- name: Debug
  debug:
    var: json_response

Let's consider that json_response has 100+ lines. I would like to find my specific word and keep the line. Like this:

Keeping the line where you find the words: "crazy"

output.json:

I'm doing this test

So you guys can understand

What I'm saying

I'm a crazy test

And the result output.json would be:

I'm a crazy test

Right now, I've looked at the "lineinfile" module for ansible and it says: "This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression"

But it doesn't seems to fullfill my needs, any ideas?

Thanks

Upvotes: 0

Views: 856

Answers (1)

Domingo Tamayo
Domingo Tamayo

Reputation: 261

If json_response is a proper json data, you can use the json_query filter to search for the keyword: https://docs.ansible.com/ansible/2.7/user_guide/playbooks_filters.html#json-query-filter

Else, if it's a plain multi-line string, you can use regex_search filter instead: https://docs.ansible.com/ansible/2.7/user_guide/playbooks_filters.html#regular-expression-filters

Upvotes: 2

Related Questions