title: Pass a variable dynamically into Ansible template while iterating a list in playbook
body: <p>Ansible gurus I need your help. I have a template with a variable I need to set dynamically while iterating through a list in the playbook. It's not clear how to "inject" a variable to the template to use while iterating.</p>
  
  <p>Objective is to create many config files in a directory, one for each var in a list. For example if the list is <code>A, B, C</code> I would want files A.conf, B.conf, C.conf respectively. </p>
  
  <p>Within the template config file, there is a variable that needs to be set with the A, B, or C respectively as well. I tried to use the <code>set_fact</code> and set variable from the list item and reference. I tried to reference <code>{{ item }}</code> within. All failed?</p>
  
  <pre><code>apps:
   - one
   - two
   - three
  
  tasks:
      - name: Install Logstash config
        template: src={{ upload_dir }}/deploy/logstash-etl.conf dest=/opt/logstash/bin/etl-{{ item }}.conf
        set_fact:
          app: "{{ item }}"
        delegate_to: "{{ logstash_host }}"
        with_items: apps
  </code></pre>
  
  <p>Within the template file I've tried to reference <code>{{ app }}</code> and <code>{{ item }}</code> and I cannot seem to get it to recognize the dynamic variable to set it's value from the current value of the iteration.</p>
  
  <p>Expected if I had apps: one, two, three would be 3 files in a folder each named etl-one.conf, etl-two.conf, etl-three.conf respectively and within each file a variable in the config dynamically set like "topic_id": "etl.one.Event" for etl-one.conf and "topic_id": "etl.two.Event" for etl-two.conf respectively.</p>
  
  <p>Many thanks for any ideas. The <code>set_fact</code> was my latest attempt and that fails with a </p>
  
  <pre><code>ERROR: multiple actions specified in task: 'template' and 'Install Logstash config'
  exit status 1
  </code></pre>