title: How to loop in Ansible $var number of times?
body: <p>I want to run a loop in Ansible the number of times which is defined <em>in</em> a variable. Is this possible somehow?</p>
  
  <p>Imagine a list of servers and we want to create some numbered files on each server. These values are defined in vars.yml:</p>
  
  <pre><code>server_list:
    server1:
      name: server1
      os: Linux
      num_files: 3
    server2:
      name: server2
      os: Linux
      num_files: 2
  </code></pre>
  
  <p>The output I desire is that the files <code>/tmp/1</code>, <code>/tmp/2</code> and <code>/tmp/3</code> are created on server1, <code>/tmp/1</code> and <code>/tmp/2</code> are created on server2. I have tried to write a playbook using <code>with_nested</code>, <code>with_dict</code> and <code>with_subelements</code> but I can't seem to find any way to to this:</p>
  
  <pre><code>- hosts: "{{ target }}"
  
    tasks:
  
      - name: Load vars
        include_vars: vars.yml
  
      - name: Create files
        command: touch /tmp/{{ loop_index? }}
        with_dict: {{ server_list[target] }}
        loop_control:
          loop_var: {{ item.value.num_files }}
  </code></pre>
  
  <p>If I needed to create 50 files on each server I can see how I could do this if I were to have a list variable for each server with 50 items in it list which is simply the numbers 1 to 50, but that would be a self defeating use of Ansible.</p>