Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract last character of string with ansible and regex_replace filter

In a playbook, I try to extract the last character of variable "ansible_hostname".

I try to use regex_replace filter to do that, but nothing works.

I simplified my piece of script with this ad-hoc command :

ansible localhost -m debug -a "msg= {{ 'devserver01' | regex_replace('[0-9]{1}$', '\1') }}"

I want to extract the last character : '1'.

I'm using Ansible 2.0.

like image 558
Antoine Avatar asked Jun 10 '16 12:06

Antoine


1 Answers

Python can save the day, and is acceptable in this use.

Just add a [-1] to the end of the string or variable, which gets the last character in a string.

ansible localhost -m debug -a "msg={{ 'devserver01'[-1] }}"
like image 194
Theo Avatar answered Sep 19 '22 16:09

Theo