1

Would require a single ansible regex for below three outputs:

input1: aa::bb::cc output1: aa::cc
input2: bb::aa::cc output2: aa::cc
input3: aa::cc::bb output3: aa::cc

I have written the below reg exp. But the extra double colons are still there.

example: {{ aa::bb::cc | regexx_replace('bb') }} --> gives output as: aa::::cc

2
  • Why do you absolutely need to do this with a regex ? It's going to be hard to write, unreadable probably, and you will need quite a lot of time to understand what you have done when you come accross it for maintenance in a few weeks/month. There is a much easier solution IMO. Experiment with this one liner for example: ansible localhost -m debug -a msg="{{ 'aa::bb::cc'.split('::') | difference(['bb']) | join('::') }}". Also note that this can work with any number of elements to remove. Commented Oct 12, 2020 at 11:15
  • Your example will not work. a) you can not push a string into a filter like that, because it will try to resolve aa::bb::cc as a variable. b) it is regex_replace (you have regexx_replace). Commented Oct 12, 2020 at 11:43

1 Answer 1

0

This is your regexp: (::b+|b+::)

It will match :: followed by one or more bs or one or more bs followed by :: so it will take the double colons with it, no matter if they are in front of the bs or after. When it sees ::bb:: as in aa::bb::cc only one of them will match and the result will be aa::cc (so the second pair of colons persists).

Sign up to request clarification or add additional context in comments.

4 Comments

Ok. So now lets modify our input string: bb::bbb::bbbb :) I'm just pushing it a little too far just to point out that securing the regex a bit more will make it almost unreadable. But If you don't need to, your solution is really KISS.
@toydarian Thanks for your response. Your logic for the question did work actually. But according to Zeitounator, this logic would cause problems if multiple values of input in present. Example, ansible localhost -m debug -a msg="{{ 'aa::bb::cc::bbb::bbbb' | regex_replace('::bb|bb::') }}" --> gives output as "msg": "aa::ccbbb"
@Mani your question leads to the assumption, that you wanted to replace exactly two bs. So I answered your question. See my latest edit for a regexp that will match one or more bs (they will still need to be next to ::) and it will not work if the bs are next to any other character. Like this aabb::aa::cc will turn into aaaa::cc.
@toydarian, Great. This helps as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.