using every other character in a variable

10 views
Skip to first unread message

Kathy L

unread,
Sep 12, 2023, 9:35:14 AM9/12/23
to Ansible Project
I am trying to grab every other character in a very long variable, and placing it in another variable.  This is what I have:

set_fact:
   new_string: "{{ [ 0,2,4,6,8,10,12 ] | map('extract', verylongvariable) }}"

I've also tried this:
set_fact:
    new_string: "{{ [ 0,2,4,6,8,10,12 ] | map('extract', verylongvariable) }} | map('join', ',' | join('')) }}

What I end up getting in the new_string is this:

   'N', 'y', 'a', 'd'....

What I want is:

    Nyad....

I clearly am not understanding the join syntax.  How do I use join to get the new variable?

Matt Martz

unread,
Sep 12, 2023, 9:40:05 AM9/12/23
to ansible...@googlegroups.com
All you need is to use is slicing with a step:

If my variable is `verylongvariable: s1i2v3e4l5`

If I want `sivel`, I'd use: {{verylongvariable[::2]}}

If I want `12345`, I'd use: {{verylongvariable[1::2]}}

--
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/ec75223e-8d4a-4aa8-bbfa-2d78389d35fan%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Kathy L

unread,
Sep 12, 2023, 10:06:28 AM9/12/23
to Ansible Project
Great thank you!  Is there documentation somewhere on using '::' ?  I've looked everywhere...

Brian Coca

unread,
Sep 12, 2023, 10:16:54 AM9/12/23
to ansible...@googlegroups.com
You just need a join to make your current expression's output (list)
into a string.
But @sivel's solution with slicing is probably better, the reason you
cannot find it in documentation is because this is a "Pythonism" and a
property of Python list objects.
Ansible uses Jinja for expression evaluation, Jinja compiles into
Python in the end and exposes many of the properties of those Python
objects, neither Ansible nor Jinja document these properties so you
would need to a) know the type of object you are dealing with and b)
look at python documentation for those objects.


--
----------
Brian Coca

Matt Martz

unread,
Sep 12, 2023, 10:20:06 AM9/12/23
to ansible...@googlegroups.com
You can find the information about slicing within the python docs at: https://docs.python.org/3/library/functions.html#slice

Probably some other docs spread around as well that go into more depth.

--
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.

Hearn, Stan J.

unread,
Sep 12, 2023, 10:34:04 AM9/12/23
to ansible...@googlegroups.com
What are some good resources to learn Ansible "pythonism" filters? I struggle with string manipulation, sets, dict merging, etc. In my opinion this is one of the greatest strengths of Ansible.

How did you guys learn it? Did you learn it because you knew python first?

Regards,
Stan

Reply all
Reply to author
Forward
0 new messages