How to remove the line breaker character '\n' from the result of lookup() module in Ansible?

91,719

Solution 1

Use the Jinja trim filter:

"{{ lookup('file', 'foo.xml' ) | trim }}"

Solution 2

You can do that with the replace filter?

contents: "{{ lookup('file', '/etc/foo.txt') | replace('\n', '')}}"

Solution 3

You may use the regex_replace filter since the trim doesn't clear other line break characters as you mentioned in the question.

"{{  some_stdout_to_clear | regex_replace('[\\r\\n\\t]+','') }}"
Share:
91,719

Related videos on Youtube

Nishant Singh
Author by

Nishant Singh

DevOps/SRE!! Reincarnation 1.0

Updated on July 09, 2022

Comments

  • Nishant Singh
    Nishant Singh almost 2 years

    I am using [file lookup] which reads the whole file and stores the content in a variable. My play looks something like this:

      - name: Store foo.xml contents in a variable
        set_fact:
         foo_content: "{{ lookup('file', 'foo.xml' ) | replace('\n', '')}}"
    

    So the above code reads the foo.xml file and stores it in the variable, but the problem is when the foo.xml has line breaks in it, it also includes the line break in the variable.

    My foo.xml is this file:

    <?xml version="1.0" encoding="utf-8"?>
    <initialize_param>
        <secrets>
            <my_secret id="99">3VMjII6Hw+pd1zHV5THSI712y421USUS8124487128745812sajfhsakjfasbfvcasvnjasjkvbhasdfasgfsfaj5G8A9+n8CkLxk7Dqu0G8Jclg0eb1A5xeFzR3rrJHrb2GBBa7PJNVx8tFJP3AtF6ek/F/WvlBIs2leX2fq+/bGryKlySuFmbcwBsThmPJC5Z5AwPJgGZx</my_secret>
        </secrets>
    </initialize_param>
    

    The output removes line break \n but also incudes the tabs \r & \t

    I need to got rid of the \n , need to get rid of extra formatting too (\r & \t), Moreover after the replace filter I get the error while firing a DB Update query as

    stderr: /bin/sh: 1: cannot open ?xml: No such file
    
    • Mikko Ohtamaa
      Mikko Ohtamaa over 8 years
      My guess is that \n is just artifact of debug output and it is correctly handled there.
    • Nishant Singh
      Nishant Singh over 8 years
      nope, it actually stores it as \n and then when i push the variable in a database it throws a error :
    • Mikko Ohtamaa
      Mikko Ohtamaa over 8 years
      Can you please clarify what kind of error and how you are pushing to database?
    • Nishant Singh
      Nishant Singh over 8 years
      stderr: /bin/sh: 2: Syntax error: newline unexpected
    • Mikko Ohtamaa
      Mikko Ohtamaa over 8 years
      Please edit the question with full information, including your script, databse info and so on. The given error message does not give any more information.
    • Mikko Ohtamaa
      Mikko Ohtamaa over 8 years
      It is easier to help if one doesn't need to guess what is being done
    • Mikko Ohtamaa
      Mikko Ohtamaa over 8 years
      Yes, now it does make a lot of more sense :)
    • Mikko Ohtamaa
      Mikko Ohtamaa over 8 years
      Looks like the error happens when you pass the file content as is as a filename to some script. Script content is missing from the question so it s not possible to tell what the script is doing with the content and why it fails.
  • Nishant Singh
    Nishant Singh over 8 years
    Yes it does it.. but what if the txt file gets replaced with .xml.. m gonna probably reframe my question
  • Nishant Singh
    Nishant Singh over 8 years
    How can we use multiple replace in lookup ?? i am trying to remove more charecters like \r soo expecting something like this contents: "{{ lookup('file', '/etc/foo.txt') | replace('\n', '') | replace('\r', '') }}"
  • 007
    007 over 6 years
    Wow, it worked. I swear I was working on this for hours. There was a trailing \n in a JSON string I was trying to post to a server using the uri_module. replace('\n', '') wasn't getting rid of it, I thought, what the hell, I'll try this, and it WORKED. argh. I think there was multiple levels of issues with this but thank you sir!
  • Ryan Fisher
    Ryan Fisher over 5 years
    Using the Jinja trim filter as in @andreas-maier 's answer is a more appropriate and readable solution.
  • udondan
    udondan over 5 years
    @RyanFisher No it's not. trim removes leading and trailing characters, while the op wanted to remove all such characters, also in the middle. Also note, the question has been heavily modified since the answer was posted, therefore might not make too much sense. Anyway, thanks for the downvote. :)
  • JCotton
    JCotton about 5 years
    In my case trim filter would have worked (only had a trailing newline). However, my variable was Vault encrypted and trim didn't decrypt first. replace handled it correctly (decrypt, then remove newline). Thanks!
  • Yorai Levi
    Yorai Levi over 2 years
    This is the only solution that worked