create folder adds [' my var ’]

24 views
Skip to first unread message

Gisbert Haas

unread,
Oct 20, 2020, 5:54:34 PM10/20/20
to Ansible Project
I have the following regex in my playbook:
   - name: get business ID
     set_fact:
       my_var: "{{ inventory_hostname | regex_findall('.+?(?=-).+?(?=-).+?(?=-).+?(?=-)+') }}"

the debug returns the right value:

ok: [DE-LEI-GRO-GROLE-F001-FW01-MDF] => {
    "my_var": [
        "DE-LEI-GRO-GROLE"

when using create directory
   - name: create directory
     file:
       path: /home/vinn/devicebackups/{{ my_var }}
       state: directory

its creates the directory adding [' ’]

"path": "/home/vinn/devicebackups/[''DE-LEI-GRO-GROLE’]",


any idea why??

thanks a lot

Vladimir Botka

unread,
Oct 20, 2020, 7:54:35 PM10/20/20
to Gisbert Haas, ansible...@googlegroups.com
On Tue, 20 Oct 2020 14:54:34 -0700 (PDT)
Gisbert Haas <gisbe...@gmail.com> wrote:

> I have the following regex in my playbook:
> - name: get business ID
> set_fact:
> my_var: "{{ inventory_hostname |
> regex_findall('.+?(?=-).+?(?=-).+?(?=-).+?(?=-)+') }}"
>
> the debug returns the right value:
>
> ok: [DE-LEI-GRO-GROLE-F001-FW01-MDF] => {
> "my_var": [
> "DE-LEI-GRO-GROLE"

The bracket '[' tells us the variable my_var is a list. It's not
clear how the set_fact above managed to create the list.

> when using create directory
> - name: create directory
> file:
> path: /home/vinn/devicebackups/{{ my_var }}
> state: directory
>
> its creates the directory adding [' ’]
>
> "path": "/home/vinn/devicebackups/[''DE-LEI-GRO-GROLE’]",
>
> any idea why??

The variable my_var is a list.

--
Vladimir Botka

Vladimir Botka

unread,
Oct 20, 2020, 8:05:25 PM10/20/20
to Gisbert Haas, ansible...@googlegroups.com
On Wed, 21 Oct 2020 01:54:11 +0200
Vladimir Botka <vbo...@gmail.com> wrote:

> On Tue, 20 Oct 2020 14:54:34 -0700 (PDT)
> Gisbert Haas <gisbe...@gmail.com> wrote:
> > - name: get business ID
> > set_fact:
> > my_var: "{{ inventory_hostname |
> > regex_findall('.+?(?=-).+?(?=-).+?(?=-).+?(?=-)+') }}"
> >
> > ok: [DE-LEI-GRO-GROLE-F001-FW01-MDF] => {
> > "my_var": [
> > "DE-LEI-GRO-GROLE"

> The bracket '[' tells us the variable my_var is a list. It's not
> clear how the set_fact above managed to create the list.

The filter regex_findall returns a list of matches
https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/filter/core.py#L137

--
Vladimir Botka

Gisbert Haas

unread,
Oct 21, 2020, 6:24:33 AM10/21/20
to Ansible Project
thanks

could it be that hyphen - are not allowed in the variable string?

Reply all
Reply to author
Forward
0 new messages