0

Until today I had only ONE type specific xml file

~/files:

gg-schema.xml.j2 
lr-schema.xml.j2

and I copied it with template module like this:

- hosts: localhost
  gather_facts: false

  vars:
     types: 
       - lr
       - gg          

  tasks:                     
    - name: "config | copy type dependent schema.xml "
      vars:
         schema_name: "{{ mydoctype }}-schema.xml"
      template:
          src: "files/{{ schema_name }}.j2"
          dest: "/tmp/{{ schema_name }}"
      loop: "{{ types }}"
      loop_control:
         loop_var: mydoctype
         label: "{{ schema_name }}"

The result was:

PLAY [localhost] ***************************************************************
TASK [config | copy doctype dependent schema.xml] ******************************
ok: [localhost] => (item=lr-schema.xml) => {"ansible_loop_var": "mydoctype", "changed": false, "checksum": "b4a2be60cc7b4d88d7d471f37c33906bd83a0f02", "dest": "/tmp/lr-schema.xml", "gid": 30000, "group": "rz", "mode": "0644", "mydoctype": "lr", "owner": "ffeller", "path": "/tmp/lr-schema.xml", "size": 11042, "state": "file", "uid": 10069}
ok: [localhost] => (item=gg-schema.xml) => {"ansible_loop_var": "mydoctype", "changed": false, "checksum": "2792819702d1eae5d9206b717e680c635ebb4e48", "dest": "/tmp/gg-schema.xml", "gid": 30000, "group": "rz", "mode": "0644", "mydoctype": "gg", "owner": "ffeller", "path": "/tmp/gg-schema.xml", "size": 13232, "state": "file", "uid": 10069}       

Now I have TWO schema files for one type (e.g. gg) like this:

~/files:

gg-schema.xml.j2 
gg-orsh-schema.xml.j2
lr-schema.xml.j2

How can I use a with_fileglob to get all files (name staring with type) copied?

ceving
  • 21,900
  • 13
  • 104
  • 178
901Franco
  • 11
  • 3

1 Answers1

0

Try this:

- hosts: localhost
  gather_facts: false

  tasks:
    - name: "config | copy type dependent schema.xml "
      template:
          src: "{{ item }}"
          dest: "/tmp/{{ (item | basename | splitext)[0] }}"
      with_fileglob:
        - "files/*-schema.xml.j2"

Ansible does not support real Shell globbing syntax.

$ ls -1 files/{lr,gg}*-schema.xml.j2
files/gg-orsh-schema.xml.j2
files/gg-schema.xml.j2
files/lr-schema.xml.j2

This will not work:

- hosts: localhost
  gather_facts: false

  vars:
     types:
       - lr
       - gg

  tasks:
    - debug: msg="{{ 'files/{' + ','.join(types) + '}*-schema.xml.j2' }}"
    - name: "config | copy type dependent schema.xml "
      template:
          src: "{{ item }}"
          dest: "/tmp/{{ (item | basename | splitext)[0] }}"
      with_fileglob:
        - "{{ 'files/{' + ','.join(types) + '}*-schema.xml.j2' }}"

The reason is, that Python itself does not support it.

Neither Python 2:

$ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> glob.glob ("files/{lr,gg}*-schema.xml.j2")
[]
>>> glob.glob ("files/*-schema.xml.j2")
['files/gg-schema.xml.j2', 'files/lr-schema.xml.j2', 'files/gg-orsh-schema.xml.j2']
>>>

Nor Python 3:

$ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> glob.glob ("files/{lr,gg}*-schema.xml.j2")
[]
>>> 
ceving
  • 21,900
  • 13
  • 104
  • 178