split for data manitpulation

Hi,

I have this playbook to print the columns 5 and 6 using the debug.

Do split accepts only one square bracket parameter?

the python aquivalent (something similar to this should be happening in the backgound) would be:

myline = ‘This line is a test, two, three, four, five, 6, seven’
print(myline.split(‘,’)[4], myline.split(‘,’)[5], myline.split(‘,’)[6])
five 6 seven

as you do not have an index 11 your concrete example makes no big sense in that regards.

I neither found the correct ansible syntax to translate that mentioned python logic into a TASK example like yours though.
maybe someone else here can help out with that

You have 6 delimiters, so only 7 fields. Your maximum index therefore is 6.

Setting that issue aside, you can use map(), like so:

    "{{ [2, 4, 5] | map('extract', myline.split(',')) }}"

Cheers,

Thanks Todd.
I got the desired output and it answers the other thread also .
https://groups.google.com/g/ansible-project/c/ZqZuw3do-5g/m/hMlDvLceAQAJ (Now I understand it is a string… )

Hi Todd,

Is there a way to combine the 2 positional values .

“{{ [2, 4, 5] | map(‘extract’, myline.split(‘,’)) }}”

as
“{{ [2, 4 5] | map(‘extract’, myline.split(‘,’)) }}”
to print like below

  • two
  • four five

I tried to combine … but missing a bit
“{{ [2, combine [4 5] | map(‘extract’, myline.split(‘,’)) }}” or printing “two” as a item and “{{ [4 5] | map(‘extract’, myline.split(‘,’)) |combine }}”

or a different filter instead of combine can help?

Split the string and trim the items

  arr: "{{ myline|split(',')|map('trim') }}"

gives

  arr:
  - This line is a test
  - two
  - three
  - four
  - five
  - '6'
  - seven

A list of lists the structure that describes what you want

  s1_34: "{{ [[1], [3, 4]]|
           map('map', 'extract', arr)|
           map('join', ' ') }}"

gives

  s1_34:
  - two
  - four five

You can substitute *arr* in *s1_34* if you want a 'one-liner'.

Hi Vladimir ,

I got it by using the join - s1_34: "{{ [[1], [3, 4]]| map(‘map’, ‘extract’, arr)| join(‘,’)}