回答:
あなたは使用することができwith_fileglob、あなたのテンプレートディレクトリからファイルのリストを取得し、このようなJ2の拡張子を取り除くためにフィルタを使用します。..
- name: create x template
  template:
    src: {{ item }}
    dest: /tmp/{{ item | basename | regex_replace('\.j2','') }}
  with_fileglob:
    - ../templates/*.j2
              regex_replace行末で一致する必要があるメモ\.j2$。
                    Michael DeHaan(Ansibleの作成者)がCoderWallに投稿し、非常によく似た問題について話しました。ニーズ(権限や所有権など)に応じて調整および拡張できます。投稿の関連部分は次のとおりです。
これはwith_items、「」と単一のnotifyステートメントを使用して簡素化できます。タスクのいずれかが変更されると、プレイブックの実行の最後に再起動する必要があるのとまったく同じ方法でサービスに通知されます。
 - name:  template everything for fooserv
   template: src={{item.src}} dest={{item.dest}}
   with_items:
      - { src: 'templates/foo.j2', dest: '/etc/splat/foo.conf' }
      - { src: 'templates/bar.j2', dest: '/etc/splat/bar.conf' }
   notify: 
      - restart fooserv
複数の一意の引数を取るタスクがあるためitem、「template:」行で「」を言うだけでなくwith_items、ハッシュ(辞書)変数とともに使用することに注意してください。必要に応じて、リストを使用して少し短くすることもできます。これは文体的な好みです:
 - name:  template everything for fooserv
   template: src={{item.0}} dest={{item.1}}
   with_items:
      - [ 'templates/foo.j2', '/etc/splat/foo.conf' ]
      - [ 'templates/bar.j2', '/etc/splat/bar.conf' ]
   notify: 
      - restart fooserv
もちろんgroupvars/webservers、webserversグループに必要なすべての変数を定義する「」ファイルやvarsfiles、プレイブック内の「」ディレクティブからロードされたYAMLファイルなど、別のファイルでリストを定義することもできます。どうすればクリーンアップできるのか見てください。
- name: template everything for fooserv
  template: src={{item.src}} dest={{item.dest}}
  with_items: {{fooserv_template_files}}
  notify: 
      - restart fooserv
              template: src=templates/{{item}}.j2 dest=/etc/splat/{{item}}.confし、次に項目のプレーンなリストを使用しますwith_items:   - foo   - bar
                    template: src={{item.src}} dest={{item.dest}}(つまりでは${var}なく{{var}})
                    ラッセルの答えは機能しますが、改善が必要です
- name: create x template
- template: src={{ item }} dest=/tmp/{{ item | basename | regex_replace('.j2','') }}
- with_fileglob:
   - files/*.j2
regex_replaceの正規表現が間違っていたので、すべての$のモミは行く必要があります。第二に、すべてのファイルはテンプレートディレクトリではなくファイルディレクトリにあるべきです
ファイルツリーのアクションを支援できるファイルツリールックアッププラグインを作成しました。
ファイルツリー内のファイルを再帰処理し、ファイルプロパティに基づいてアクション(テンプレートやコピーなど)を実行できます。相対パスが返されるため、ターゲットシステムでファイルツリーを簡単に再作成できます。
- name: Template complete tree
  template:
    src: '{{ item.src }}'
    dest: /web/{{ item.path }}
    force: yes
  with_filetree: some/path/
  when: item.state == 'file'
より読みやすいプレイブックになります。
以下のコマンドは、テンプレート内のj2ファイルを再帰的に検索し、それを宛先に移動するのに役立ちました。宛先へのテンプレートの再帰コピーを探している人に役立つことを願っています。
     - name: Copying the templated jinja2 files
       template: src={{item}} dest={{RUN_TIME}}/{{ item | regex_replace(role_path+'/templates','') | regex_replace('\.j2', '') }}
       with_items: "{{ lookup('pipe','find {{role_path}}/templates -type f').split('\n') }}"
              ディレクトリから実際のファイルのリストを自動的に取得し、後で反復する可能性があります。
- name:         get the list of templates to transfer
  local_action: "shell ls templates/* | sed 's~.*/~~g'"
  register:     template_files
- name:         iterate and send templates
  template:     src=templates/{{ item }} dest=/mydestination/{{ item }}
  with_items:
  - "{{ template_files.stdout.splitlines() }}"
              print0、などをサポートするシェルユーティリティを使用してfindから、分割すること\u0000です。
                    
with_fileglob常にから動作しfiles/ますが、でテンプレートに到達できます../templates/mytemplate/*。stackoverflow.com/a/27407566/1695680