Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip newline from shell command's standard output run via ansible

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 .

like image 837
Ankit Kulkarni Avatar asked Nov 23 '15 13:11

Ankit Kulkarni


People also ask

How do you capture command output in Ansible?

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.

How do I format Ansible output?

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.

What is the difference between shell and command in Ansible?

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.


2 Answers

You can use the built-in filters of Jinja2 to achieve this. Specifically, use trim, e.g.:

- debug: msg='{{ my_info.stdout | trim}}'
like image 106
Todd Owen Avatar answered Nov 09 '22 06:11

Todd Owen


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 the shell or command 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.

like image 28
AKS Avatar answered Nov 09 '22 05:11

AKS