how to get specified order of strings in list ?

I have a list like this

user:

name: chchang
comment: “Chang Chic Hung”

and I want to add more attrs like

last_name: Chang
mid_name: Chic

but how to get the specifed order of the values ?
I try to use filter “first” , but it just return “C” , the first letter , not the first word.

any suggestions ?

Do you want to extract the strings from the comment, or do you just want to get the three attributes (last_name, mid_name etc) in some particular order?

You can get the parts of the name out of the comment by using the split() filter. This assumes no names contain spaces or whatever split delimiter you use - not a good assumption. Ask Charles de Gaulle.

There is no automatable way to reliably work out what order the parts of a name should be in. Some cultures are firstname, lastname, some are lastname firstname, some are one in some circumstances and the other way in other circumstances, some routinely have three parts, or only one part. Even in the English-speaking world there are zillions of exceptions like J. Edgar Hoover or Fred Smith Junior.

Regards, K

The tasks

   - set_fact:
        user: "{{ user|
                  combine({'last_name': comment_split.0})|
                  combine({'mid_name': comment_split.1}) }}"
      vars:
        comment_split: "{{ user.comment.split() }}"
    - debug:
        var: user

give

    "user": {
        "comment": "Chang Chic Hung",
        "last_name": "Chang",
        "mid_name": "Chic",
        "name": "chchang"
    }

HTH,

  -vlado