# Capture a Directory Name in a path for later use

**URL:** <https://forum.ansible.com/t/capture-a-directory-name-in-a-path-for-later-use/3799>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [February 9, 2024, 9:42pm UTC](https://forum.ansible.com/t/capture-a-directory-name-in-a-path-for-later-use/3799 "2024-02-09T21:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cryptosooner1](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@cryptosooner1](https://forum.ansible.com/u/cryptosooner1)\
**Post date:** [February 9, 2024, 9:42pm UTC](https://forum.ansible.com/t/capture-a-directory-name-in-a-path-for-later-use/3799/1 "2024-02-09T21:42:42Z")

</div>

I am trying to capture the name of directory after a poetry install for later use cause I need to build a path for a community.general.sefcontext command.

The YkNvIedi in the path below is created at install. I have no idea what it is going to be beforehand but I need to capture it.

/var/cache/poetry/virtualenvs/ **finance-YkNvIedi-py3.11** /bin/uwsgi

This is my feeble attempt.

- hosts: all  
tasks:
  - name: Capture poetry directory name  
become: true  
find:  
paths: /var/cache/poetry/virtualenvs/  
file\_type: directory  
patterns: “finance-_-py_”  
use\_regex: true  
register: poetry\_path

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [February 9, 2024, 10:06pm UTC](https://forum.ansible.com/t/capture-a-directory-name-in-a-path-for-later-use/3799/2 "2024-02-09T22:06:20Z")

</div>

This site is good to checking regular expressions:

> **[regex101: build, test, and debug regex](https://regex101.com/)**
>
> Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.

Try something like:

```yaml
patterns: '^finance-[a-zA-Z]{1,12}-py[0-1.]{3,5}$'

```

The `^` matches the start, `[a-zA-Z]` matches upper and lower case letters and `{1,12}` is between 1 and 12 of them, if it is always going to be 8 letter long use `{8}`, `[0-9.]{3,5}$` matches between 3 and 5 numbers and dots and the end of the string.

---

<div class="post-metadata">

**Author:** ![cryptosooner1](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@cryptosooner1](https://forum.ansible.com/u/cryptosooner1)\
**Post date:** [February 12, 2024, 6:05pm UTC](https://forum.ansible.com/t/capture-a-directory-name-in-a-path-for-later-use/3799/3 "2024-02-12T18:05:10Z")

</div>

> [@chris](#):
>
> `[0-1.]`

Thanks for the link. After experimenting some this works in ansible.

patterns: ‘finance-…-py…’

path: /var/cache/poetry/virtualenvs/finance-Zx6szD7U-py3.11

but this does not…

patterns: ‘finance-[a-zA-Z0-9.]{1,8}-py…’

matched: 0

Any idea?

By the way, this does work on [regex101.com](http://regex101.com)

finance-[a-zA-Z0-9.]{1,8}-py…
