How to remove the trailing new lines from the jinja templating of stdout_lines

Hi all,

I am trying to parse the stdout_lines to find the matching strings in a line. But the problem is stdout_lines adds new line at the end and the match string also contains new line which looks ugly. Any idea how I can fix it?
I tried trim, difference etc after some google around but it does not work inside the jinja template as below. I am stuck here… Can anyone please help?

stdout_lines

ok: [10.10.25.1] => {

“stdout_lines”: [

“X”,

“B”,

“C”,

“D”,

“E”,

“F”,

“G”,

“H”,

“I”,

“J”,

“A”

]

}

-name : Test match string
 - command: |
        printf "X\nB\nC\n\D\nE\nF\n\G\nH\nI\nJ\nA\n"
        register: cmd
  • name: Find the matching string

set_fact:

msg: |

{% for line in cmd.stdout_lines %}

{% if line is defined and ‘A’ in line %}

{{ line }}

{% endif %}

{% endfor %}

when:

  • cmd.stdout_lines is defined

  • debug:

var: msg

The output looks as below as it adds the new line till the match “A” found(e.g X\n,B\n blah blah) and it looks ugly. But I dont want these new lines in my output. Can anybody please throw some idea how can i fix it?

Actual Output: