ansible-community / community-examples

Repository for community examples
GNU Affero General Public License v3.0
4 stars 1 forks source link

possible example: convert path string to a list of all the contained paths #3

Open samccann opened 2 years ago

samccann commented 2 years ago

This came from an email in the ansible-projects mailing list over the summer, authored by Dick Visser. @acozine suggested it might make a good example.

The original query was:

Is there any simple expression/filter magic that would allow me to convert a path string to a list of all the contained paths.

So: "a/b/c/d"

would become:

[ "a", "a/b" , "a/b/c" , "a/b/c/d" ]

The proposed example solution:

---
- hosts: localhost
 connection: local
 gather_facts: no
 vars:
   p: /usr/local/bin/opt/etc/opt/var/tmp/sbin/lib/blah/something/else
 tasks:
   - set_fact:
       parts: "{{ p.split('/')|difference(['']) }}"
   - set_fact:
       all: "{{ all | default([]) | union([parts[0:idx+1]|join('/')]) }}"
     loop: "{{ parts }}"
     loop_control:
       index_var: idx
   - debug: var=all
samccann commented 2 years ago

Not sure how we proceed with this, but I figured it was worth listing here to help us work out what comes next. Things that come to mind:

  1. How do we attribute the original author when it came from a mailing list like this?
  2. Do we have CI that can test these examples?
  3. Does it help to have things like what the example is doing? If so, how do we format that in this example repo?
  4. Does it help having the output of the example?

Here's the output just in case :

TASK [debug] **************************************************************************************************
ok: [localhost] =>
 all:
 - usr
 - usr/local
 - usr/local/bin
 - usr/local/bin/opt
 - usr/local/bin/opt/etc
 - usr/local/bin/opt/etc/var
 - usr/local/bin/opt/etc/var/tmp
 - usr/local/bin/opt/etc/var/tmp/sbin
 - usr/local/bin/opt/etc/var/tmp/sbin/lib
 - usr/local/bin/opt/etc/var/tmp/sbin/lib/blah
 - usr/local/bin/opt/etc/var/tmp/sbin/lib/blah/something
 - usr/local/bin/opt/etc/var/tmp/sbin/lib/blah/something/else

PLAY RECAP ****************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0
failed=0    skipped=0    rescued=0    ignored=0
samccann commented 2 years ago

FWIW - bcoca had the following feedback on this example: FYI, looping over set_fact is not recommended unless you specifically want static values, using map/select/reject filters is recommended in these cases, with examples in the complex data manipulation page https://docs.ansible.com/ansible/latest/user_guide/complex_data_manipulation.html

And briantist had the following feedback: in addition to what bcoca added, was going to say that referencing a variable within a variable's "definition" only really works in set_fact (because of how it's implemented), and causes infinite recursion in most other places, which can be confusing.