Ansible upper, lower, capitalize & title Filters: Text Case Guide
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
How to transform text case in Ansible with upper, lower, capitalize, and title filters. Convert strings for configs, templates, and variable manipulation.
Introduction
In the ever-evolving landscape of IT automation, Ansible continues to be a go-to tool for simplifying and streamlining repetitive tasks. While traditionally known for its prowess in system configuration and infrastructure management, Ansible extends its capabilities to even the minutiae of text processing. In this article, we'll explore a practical use case: automating the transformation of text to uppercase using an Ansible playbook.
The Uppercase Ansible Playbook
Below is an Ansible playbook named upper.yml designed to capitalize a given text and print the result on the screen.
Breaking down the playbook components: • name: Describes the purpose of the playbook. • hosts: Specifies the target hosts; in this case, it's set to all for execution on all hosts defined in the inventory. • gather_facts: Set to false to skip gathering facts about hosts, ensuring a quicker execution. • vars: Defines the my_text variable with the initial value set to "hello world." • tasks: Contains a single task to print the uppercase message on the screen.
The Inventory File
Ansible requires an inventory file to specify the hosts where the playbook will run. The inventory file in this example designates the localhost with the connection set to local, indicating that the playbook will be executed on the local machine.
Executing the Playbook
Running the playbook is a straightforward process. The ansible-playbook command, coupled with the -i flag to specify the inventory file, is used:
Upon execution, Ansible processes the defined tasks and outputs the result:
The output confirms the successful execution of the playbook on the localhost host, resulting in the message "HELLO WORLD" being printed on the screen.
Customizing and Extending
This example illustrates the simplicity of leveraging Ansible for text manipulation tasks. To adapt the playbook for different text or sources, modify the my_text variable. Additionally, you can expand the playbook's functionality to capitalize text from external files or dynamic inventories.
Conclusion
As Playbooknstrated, Ansible is not only a powerhouse for system configurations but also a versatile tool for text processing automation. Whether you're a system administrator, a DevOps professional, or anyone involved in IT operations, Ansible's capabilities extend to tasks both large and small, showcasing its value in the realm of automation.
Case Filters
Practical Use Cases
Normalize hostnames
Case-insensitive comparison
Generate filenames
Configuration templates
Environment variables
String Manipulation Filters
| Filter | Input | Output | |--------|-------|--------| | upper | hello | HELLO | | lower | HELLO | hello | | title | hello world | Hello World | | capitalize | hello WORLD | Hello world | | trim | " hello " | "hello" | | replace('a','b') | cat | cbt | | regex_replace | Pattern replacement | | truncate(5) | hello world | he... | | wordwrap(40) | Long text → wrapped | | center(20) | Centered text |
Combine with Other Filters
FAQ
Can I chain case filters?
Yes: {{ 'HeLLo' | lower | capitalize }} → Hello
How do I convert snake_case to CamelCase?
Is there a swapcase filter?
Not built-in. Use a custom filter or: {{ text | regex_replace('[a-z]', '\\U\\0') }} won't work in Jinja2. Write a filter plugin instead.
Case Filters
Practical: Environment Config
Hostname Normalization
Conditional on Case-Insensitive Match
Generate Config Keys
Slug Generation
regex_replace for Advanced Transforms
String Manipulation Filters
| Filter | Input | Output | |--------|-------|--------| | upper | hello | HELLO | | lower | HELLO | hello | | capitalize | hello world | Hello world | | title | hello world | Hello World | | trim | " hello " | "hello" | | replace('a','b') | cat | cbt | | regex_replace | Pattern-based | replacement | | truncate(10) | long string | long strin... | | center(20) | hello | " hello " | | wordwrap(40) | long line | wrapped lines |
FAQ
How to convert first letter only?
capitalize does this: "hello" | capitalize → "Hello".
Can I chain multiple filters?
Yes: "{{ name | lower | replace(' ', '_') | truncate(20) }}".
How to handle None/undefined?
Use default: "{{ name | default('unknown') | upper }}".
Related Articles • Ansible Inventory Guide
Category: troubleshooting