So I have a scenario where I am executing a shell command on a machine using ansible to get some information on standard output . I am using register
to log its result in a variable my_info
and print my_info
using debug
, I am seeing its result with \n
appended to it (Ansible has appended \n .Same command on linux do not appends \n" ).When I use the my_info
in templates for a config it prints a new line in config hence messing up my config .
Here is how the code and output goes .
Ansible code :
- name: calculate range address start
raw: grep 'CONFIG_PARAMS' /path/to/the/file | head -n 1
register: my_info
Output :
ok: [My_HOST] => {
"msg": "CONFIG_PARAMS\n"
}
How can we strip the space from this output or possibly make a change in template so that new line don't gets printed .
To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.
Ansible Output Format To change the Ansible's output format you can pass the ANSIBLE_STDOUT_CALLBACK=yaml environment variable on the command line or define the stdout_callback = yaml in Ansible configuration file.
The shell module executes commands in nodes or Shell scripts. Another dedicated Ansible module is Script that transfers the Shell script from the control machine to the remote server and executes it. In the command module, the given command executes on all selected nodes.
You can use the built-in filters of Jinja2 to achieve this. Specifically, use trim
, e.g.:
- debug: msg='{{ my_info.stdout | trim}}'
In the documentation of raw_module, it asks to use it only in specific cases:
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.
So, I tested simple echo calls with raw
and shell
modules:
- 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 }}'
and it outputs with a new line:
TASK: [debug ] ****************************************************************
ok: [127.0.0.1] => {
"msg": "hello\n"
}
and by using the shell
module:
- 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 }}'
and this results in:
TASK: [debug ] ****************************************************************
ok: [127.0.0.1] => {
"msg": "hello"
}
And, you can see the difference in the output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With