- Ansible's
regex_replace()
filter:
mystr | regex_replace('\\.[^.]+$', '')
- Using
map
filter, trim
filter, and Python list slicing:
- Apply the
trim
filter to every element of your list using the map
filter
- Using python list slicing to get everything but the last
- Join the elements back
$ ansible localhost -m debug \
-a "msg={{ (mystr.split('.') | map('trim'))[:-1] | join('.') }}" \
-e "mystr=I.to.be.war"
localhost | SUCCESS => {
"msg": "I.to.be"
}
- Using a regex to match the first part of the string:
$ ansible localhost -m debug \
-a 'msg={{ mystr | regex_replace("^(.*)\\.[^.]*$", "\\1") }}' \
-e "mystr=I.to.be.war"
localhost | SUCCESS => {
"msg": "I.to.be"
}
- Using the
splitext
filter:
- debug:
var: "'I.To.Be.war' | splitext | first | trim"
ok: [localhost] =>
'''I.To.Be.war'' | splitext | first | trim': I.To.Be
- Using
regex_replace()
filter to create a directory:
- name: "Create folder"
ansible.builtin.file:
path: '{{ sample_variable_name_from_group_vars_all | regex_replace("^(.*)\\.[^.]*$", "\\1") }}'
state: directory