Bootstrap FreeKB - Ansible - Remove whitespace using trim
Ansible - Remove whitespace using trim

Updated:   |  Ansible articles

trim is a Jinja filter used to remove whitespace from the left and right sides of a string. In this example, "    hello world   " will be trimmed.

- name: "output '     hello world    ', trim whitespace"
  debug: 
    msg: "{{ '     hello world     ' | trim }}"

 

Which should return the following.

TASK [output '     hello world    ', trim whitespace]
ok: [server1.example.com] => {
    "msg": [
        "hello world"
    ]
}

 

Let's say you are using the vars plugin to create a variable, like this.

vars:
  foo: "     hello world     "

 

The debug module can be used to output the variable, like this.

- name: "output the contents of the 'foo' variable"
  debug: 
    msg: "{{ foo }}"

 

Which should return the following.

TASK [output the contents of the 'foo' variable]
ok: [server1.example.com] => {
    "msg": [
        "     hello world     "
    ]
}

 

The trim filter can be used to to remove whitespace from the left and right sides of a string. Notice that foo is not wrapped in quotes, so that foo is interpreted as a variable, not a string.

- name: "output the contents of the 'foo' variable, trim whitespace"
  debug: 
    msg: "{{ foo | trim }}"

 

Which should return the following.

TASK [output the contents of the 'foo' variable, trim whitespace]
ok: [server1.example.com] => {
    "msg": [
        "hello world"
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f78656 in the box below so that we can be sure you are a human.