Friday, January 24, 2020

Ansible Template Tip: Split a String

Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery. We use Ansible heavily for our AWS deployment stack.

When working with Ansible template, sometimes you will need to split a string and use part of its value. For example, you have a application template which setup the application base url, like follow:

<?xml version='1.1' encoding='UTF-8'?>
    <appUrl>https://{{ app.split("-")[0] | lower }}-{{ env | lower }}</appUrl>

We have a variable "app" containing the string "" and want to extract the geolocation part of this string, we could just use "{{ app.split("-")[0] | lower }}"

The constructed url will be "".

Ansible use Jinja2 for formatting, so you can apply the split filter (

No comments: