如何通过ansible从shell命令的标准输出运行中去除换行符
How to strip newline from shell command's standard output run via ansible
所以我有一个场景,我正在使用 ansible 的机器上执行 shell 命令以获取有关标准输出的一些信息。我正在使用 register
将其结果记录在变量 my_info
中并使用 debug
打印 my_info
,我看到它的结果附加了 \n
(Ansible已附加 \n 。linux 上的相同命令不附加 \n" )。当我在配置模板中使用 my_info
时,它会在配置中打印一个新行,从而弄乱我的配置。
代码和输出如下。
Ansible 代码:
- name: calculate range address start
raw: grep 'CONFIG_PARAMS' /path/to/the/file | head -n 1
register: my_info
输出:
ok: [My_HOST] => {
"msg": "CONFIG_PARAMS\n"
}
我们如何从该输出中删除 space 或可能更改模板以便不打印新行。
您可以使用
从标准输出中删除换行符
grep 'CONFIG_PARAMS' /path/to/the/file | head -n 1 | tr -d '\n'
如果你在命令行上运行这个你会看到类似
的东西
me@machine:~ > echo cat | tr -d '\n'
catme@machine:~ >
因为linux 是添加换行符。
在raw_module的文档中,要求只在特定情况下使用:
Executes a low-down and dirty SSH command, not going through the module subsystem. This is useful and should only be done in two cases. The first case is installing python-simplejson
on older (Python 2.4 and before) hosts that need it as a dependency to run modules, since nearly all core modules require it. Another is speaking to any devices such as routers that do not have any Python installed. In any other case, using the shell
or command
module is much more appropriate. Arguments given to raw are run directly through the configured remote shell.
因此,我使用 raw
和 shell
模块测试了简单的回声调用:
- name: System setup
hosts: 127.0.0.1
connection: local
tasks:
- name: simple print hello
raw: echo 'hello'
register: my_info
- debug:
msg: '{{ my_info.stdout }}'
它输出一个新行:
TASK: [debug ] ****************************************************************
ok: [127.0.0.1] => {
"msg": "hello\n"
}
并使用 shell
模块:
- name: System setup
hosts: 127.0.0.1
connection: local
tasks:
- name: simple print hello
action: shell echo 'hello'
register: my_info
- debug:
msg: '{{ my_info.stdout }}'
这导致:
TASK: [debug ] ****************************************************************
ok: [127.0.0.1] => {
"msg": "hello"
}
而且,您可以在输出中看到差异。
您可以使用 Jinja2 的内置过滤器来实现这一点。具体使用trim
,例如:
- debug: msg='{{ my_info.stdout | trim}}'
所以我有一个场景,我正在使用 ansible 的机器上执行 shell 命令以获取有关标准输出的一些信息。我正在使用 register
将其结果记录在变量 my_info
中并使用 debug
打印 my_info
,我看到它的结果附加了 \n
(Ansible已附加 \n 。linux 上的相同命令不附加 \n" )。当我在配置模板中使用 my_info
时,它会在配置中打印一个新行,从而弄乱我的配置。
代码和输出如下。
Ansible 代码:
- name: calculate range address start
raw: grep 'CONFIG_PARAMS' /path/to/the/file | head -n 1
register: my_info
输出:
ok: [My_HOST] => {
"msg": "CONFIG_PARAMS\n"
}
我们如何从该输出中删除 space 或可能更改模板以便不打印新行。
您可以使用
从标准输出中删除换行符grep 'CONFIG_PARAMS' /path/to/the/file | head -n 1 | tr -d '\n'
如果你在命令行上运行这个你会看到类似
的东西me@machine:~ > echo cat | tr -d '\n'
catme@machine:~ >
因为linux 是添加换行符。
在raw_module的文档中,要求只在特定情况下使用:
Executes a low-down and dirty SSH command, not going through the module subsystem. This is useful and should only be done in two cases. The first case is installing
python-simplejson
on older (Python 2.4 and before) hosts that need it as a dependency to run modules, since nearly all core modules require it. Another is speaking to any devices such as routers that do not have any Python installed. In any other case, using theshell
orcommand
module is much more appropriate. Arguments given to raw are run directly through the configured remote shell.
因此,我使用 raw
和 shell
模块测试了简单的回声调用:
- name: System setup
hosts: 127.0.0.1
connection: local
tasks:
- name: simple print hello
raw: echo 'hello'
register: my_info
- debug:
msg: '{{ my_info.stdout }}'
它输出一个新行:
TASK: [debug ] ****************************************************************
ok: [127.0.0.1] => {
"msg": "hello\n"
}
并使用 shell
模块:
- name: System setup
hosts: 127.0.0.1
connection: local
tasks:
- name: simple print hello
action: shell echo 'hello'
register: my_info
- debug:
msg: '{{ my_info.stdout }}'
这导致:
TASK: [debug ] ****************************************************************
ok: [127.0.0.1] => {
"msg": "hello"
}
而且,您可以在输出中看到差异。
您可以使用 Jinja2 的内置过滤器来实现这一点。具体使用trim
,例如:
- debug: msg='{{ my_info.stdout | trim}}'