how to get specified order of strings in list ?

10 views
Skip to first unread message

Eric Chang

unread,
Feb 9, 2020, 10:34:21 PM2/9/20
to Ansible Project
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 ?



Karl Auer

unread,
Feb 9, 2020, 11:10:55 PM2/9/20
to ansible-project
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


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cd62f864-de01-4d5c-8829-47607bd5b626%40googlegroups.com.


--

Karl Auer { manager, systems support }
P: 1300 759 975
E: ka...@2pisoftware.com
2pisoftware.com

GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous:
958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

Vladimir Botka

unread,
Feb 9, 2020, 11:44:16 PM2/9/20
to Eric Chang, ansible...@googlegroups.com
On Sun, 9 Feb 2020 19:34:21 -0800 (PST)
Eric Chang <chan...@gmail.com> wrote:

> user:
> name: chchang
> comment: "Chang Chic Hung"
>
> and I want to add more attrs like
>
> last_name: Chang
> mid_name: Chic

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
Reply all
Reply to author
Forward
0 new messages