Ansible - Capitalize first character using the capitalize filter

by
Jeremy Canfield |
Updated: June 01 2025
| Ansible articles
There are a few different filters that can be used to update a value to uppercase or lowercase.
- lower is a Jinja2 filter to update a string to all lowercase characters
- upper is a Jinja2 filter to update a string to all uppercase characters
- title is a Jinja2 filter to update the first character in a string to uppercase
- capatalize is a Jinja2 filter to update the first character in a string to uppercase (this article)
In this example, "hello world" will be updated to "Hello world".
- name: "output 'hello world', upper case first character"
debug:
msg: "{{ 'hello world' | capitalize }}"
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 capitalize 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 | capitalize }}"
Which should return the following.
TASK [output the contents of the 'foo' variable, upper case first character]
ok: [server1.example.com] => {
"msg": [
"Hello world"
]
}
Did you find this article helpful?
If so, consider buying me a coffee over at