0

Is it a way to use shell globing to specify several files as a src in template module?

- name: Copy keys    
  template:    
    src: "{{ item }}"/RPM-*     
    dest: /etc/pki/{{ item }}/
  loop:
    - gf
0

1 Answer 1

0

You can use Ansible's fileglob loookupto do this. You would pass in your patterns as a list of arguments to with_fileglob, like this (rather than putting the pattern in the src argument of the template module):

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Copy keys
      template:
        src: "{{ item }}"
        dest: "./pki/{{ '/'.join(item.split('/')[-2:]) }}"
      with_fileglob:
        - "gf/RPM-*"
        - "xz/RPM-*"

This copies files into ./pki/gf/RPM-... and ./pki/xz/RPM-.... The expression '/'.join(item.split('/')[-2:]) takes a pathname like /some/path/to/gf/RPM-foo and transforms it into gf/RPM-foo.

Note that you need to ensure the target directories exist first, which is probably going to require a separate task preceding this one to create them.

2
  • 1
    I'd love a comment to go along with that -1!
    – larsks
    Commented Aug 28, 2022 at 1:08
  • (downvoting trolls, nevermind...)
    – thomasb
    Commented Mar 3, 2023 at 13:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .