Capture a Directory Name in a path for later use

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.


This is my feeble attempt.

  • hosts: all
    • name: Capture poetry directory name
      become: true
      paths: /var/cache/poetry/virtualenvs/
      file_type: directory
      patterns: “finance--py
      use_regex: true
      register: poetry_path

This site is good to checking regular expressions:

Try something like:

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.

1 Like

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