ANSIBLE: Split function not removing the '\r' carriage return from string

Solution 1:

So the trim filter seems to be removing the final \r from the list of files on file3.txt. So instead of using trim before splitting, we can trim each item after splitting the filenames with \r.

Something like below should do the trick:

    - name: saving the file names
      set_fact:
        filename_list: "{{ filename_list|default([]) + [ item|trim ] }}"
      with_items: "{{ (filenames.content|b64decode).split('\r') }}"
    - name: show the filenames
      debug:
        var: filename_list

This should produce:

ok: [localhost] => {
    "filename_list": [
        "file1.txt",
        "file2.txt",
        "file3.txt"
    ]
}