如何从 Ansible 中的 lookup() 模块的结果中删除换行符 ' '? [英] How to remove the line breaker character ' ' from the result of lookup() module in Ansible?

查看:33
本文介绍了如何从 Ansible 中的 lookup() 模块的结果中删除换行符 ' '?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 [文件查找],它读取整个文件并将内容存储在一个变量中.我的剧本是这样的:

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('
', '')}}"

所以上面的代码读取foo.xml文件并将其存储在变量中,但问题是当foo.xml中有换行符时,它还包括变量中的换行符.

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.

我的 foo.xml 是这个文件:

<?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 but also incudes the tabs &

我需要去掉 ,也需要去掉额外的格式( & ),而且在替换过滤器之后我在触发数据库时遇到错误将查询更新为

I need to got rid of the , need to get rid of extra formatting too ( & ), 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

推荐答案

使用 Jinja 修剪 过滤器:

Use the Jinja trim filter:

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

这篇关于如何从 Ansible 中的 lookup() 模块的结果中删除换行符 ' '?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆