trying to convert a dummy String with list but skipping the first iteration

41 views
Skip to first unread message

javed khan Siddque

unread,
May 18, 2023, 2:11:46 PM5/18/23
to Ansible Project
Hi Team,

i am trying to convert a dummy String with list but skipping the first iteration
i am getting below output 

"client_nfs_export_server_list": 
[
            "127.0.0.1",
            "test123.kp.org",
            "myserver.nndc.kp.org",
            "{{ item | trim }}"
  ]




skipping the first iteration


Dick Visser

unread,
May 18, 2023, 2:33:48 PM5/18/23
to ansible...@googlegroups.com
Hii

i am trying to convert a dummy String with list but skipping the first iteration

"Dummy string" - unclear what that is
Convert string with list - unclear what that means
Convert string with list but skipping first iteration - unclear. Do you want to skip the first iteration? or does that happen but you do not want that?

There are so many items unclear that I can't even start to help.
Please try to rephrase it to make it more clear.
Ideally you post the data structure that you have, and the structure you would like to have.

 
i am getting below output 

"client_nfs_export_server_list": 
[
            "127.0.0.1",
            "test123.kp.org",
            "myserver.nndc.kp.org",
            "{{ item | trim }}"
  ]




skipping the first iteration


--
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/175189a1-eed2-4240-8435-a09447b9d9cdn%40googlegroups.com.

javed khan Siddque

unread,
May 18, 2023, 2:44:08 PM5/18/23
to Ansible Project
Hi Dick,

screenshots missed from my side.
here we go.


- name: "Converting String Input From - > client_nfs_export_server_string In A List After trim/split by comma -> client_nfs_export_server_list"
  ansible.builtin.set_fact:
    server_list: "{{ item | trim }}"
    client_nfs_export_server_list: "{{ client_nfs_export_server_list | default([]) + ['{{ server_list }}'] }}"
  loop: "{{ client_nfs_export_server_string_ui.split(',') }}"


- name: "Print client_nfs_export_server_list Once"
  ansible.builtin.debug:
    msg: "{{ client_nfs_export_server_list }}"




ok: [cnpdcstore117.ctolab.kp.org] => (item=172.11.12.13) => {
528
"ansible_facts": {
529
"client_nfs_export_server_list": [
530
"{{ server_list }}"
531
],
532
"server_list": "172.11.12.13"
533
},
534
"ansible_loop_var": "item",
535
"changed": false,
536
"item": "172.11.12.13"
537
}
538
ok: [cnpdcstore117.ctolab.kp.org] => (item= 127.0.0.1 ) => {
539
"ansible_facts": {
540
"client_nfs_export_server_list": [
541
"172.11.12.13",
542
"{{ server_list }}"
543
],
544
"server_list": "127.0.0.1"
545
},
546
"ansible_loop_var": "item",
547
"changed": false,
548
"item": " 127.0.0.1 "
549
}
551
"ansible_facts": {
552
"client_nfs_export_server_list": [
553
"172.11.12.13",
554
"{{ server_list }}",
555
"{{ server_list }}"
556
],
557
"server_list": "test123.kp.org"
558
},
559
"ansible_loop_var": "item",
560
"changed": false,
561
"item": " test123.kp.org"
562
}
563




TASK [isilon_allocation_for_KPHC_environment : Print client_nfs_export_server_list Once] ***
00:01:11
565
task path: /tmp/bwrap_1228189_9zrqk15v/awx_1228189_9shz6zke/project/roles/xxxxxxx/tasks/main.yml:25
567
"msg": [
568
"172.11.12.13",
569
"{{ server_list }}",
570
"{{ server_list }}"
571
]
572
}
573
META: ending play
574

javed khan Siddque

unread,
May 18, 2023, 2:48:07 PM5/18/23
to Ansible Project
The Input Which i passed 


client_nfs_export_server_string_ui: '172.11.12.13, 127.0.0.1 , test123.kp.org'

there is some space before and after strings , which i require to trim and create a fresh list.


expected output

ok: [localhost] => {
    "msg": [
        "172.11.12.13",

        "127.0.0.1",
        "test123.kp.org",
        "myserver.nndc.kp.org"
    ]
}

Todd Lewis

unread,
May 18, 2023, 4:26:27 PM5/18/23
to ansible...@googlegroups.com, uto...@gmail.com
Let's start with the low-hanging fruit. Mustaches don't nest, so change this:


    client_nfs_export_server_list: "{{ client_nfs_export_server_list | default([]) + ['{{ server_list }}'] }}"
to this:
    client_nfs_export_server_list: "{{ client_nfs_export_server_list | default([]) + [ server_list ] }}"
and see if that gets you to a better place.
--
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.

Rowe, Walter P. (Fed)

unread,
May 19, 2023, 7:43:49 AM5/19/23
to ansible...@googlegroups.com

% ansible -e 'input="172.11.12.13, 127.0.0.1 , test123.kp.org"' -m debug -a msg="{{ input | split(',') | map('trim') | list }}" localhost -i localhost,


localhost | SUCCESS => {

    "msg": [

        "172.11.12.13",

        "127.0.0.1",

        "test123.kp.org"

    ]

}


Note the use of "map('trim')" this applying trim to each item in the list.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

javed khan Siddque

unread,
May 19, 2023, 9:26:27 AM5/19/23
to Ansible Project
Thanks Walter,


This one worked
client_nfs_export_server_list: "{{ client_nfs_export_server_string_ui.split(',') | map('trim') | list }}"

Reply all
Reply to author
Forward
Message has been deleted
0 new messages