Ansible - Upper case first character using the title filter

title is a Jinja2 filter, used to update a string so that the first character is upper case and all subsequent characters are lower case. Or, the capatalize filter could be used as well. In this example, "hello world" will be updated to "Hello world".

- name: "output 'hello world', upper case first character"
  debug: 
    msg: "{{ 'hello world' | title }}"

 

Which should return the following.

TASK [output 'hello world', upper case first character]
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 title filter can be used to update the variable so that the first character is upper case and all subsequent characters are lower case. 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, upper case first character"
  debug: 
    msg: "{{ foo | title }}"

 

Which should return the following.

TASK [output the contents of the 'foo' variable, upper case first character]
ok: [server1.example.com] => {
    "msg": [
        "Hello world"
    ]
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |