title: Ansible - Define Inventory at run time
body: <p>I am liitle new to ansible so bear with me if my questions are a bit basic.</p>
  
  <p>Scenario:</p>
  
  <p>I have a few group of Remote hosts such as [EPCs] [Clients] and [Testers]
  I am able to configure them just the way I want them to be. </p>
  
  <p>Problem:</p>
  
  <p>I need to write a playbook, which when runs, asks the user for the inventory at run time.
  As an example when a playbook is run the user should be prompted in the following way:
  "Enter the number of EPCs you want to configure"
  "Enter the number of clients you want to configure"
  "Enter the number of testers you want to configure"</p>
  
  <p>What should happen:</p>
  
  <p>Now for instance the user enters 2,5 and 8 respectively. 
  Now the playbook should only address the first 2 nodes in the group [EPCs], the first 5 nodes in group [Clients] and the first 7 nodes in the group [Testers] . 
  I don't want to create a large number of sub-groups, for instance if I have 20 EPCs, then I don't want to define 20 groups for my EPCs, I want somewhat of a dynamic inventory, which should automatically configure the machines according to the user input at run time using the <strong>vars_prompt</strong> option or something similar to that</p>
  
  <p>Let me post a partial part of my playbook for better understanding of what is to happen:</p>
  
  <pre><code>---
  - hosts: epcs # Now this is the part where I need a lot of flexibility
  
    vars_prompt:
      name: "what is your name?"
      quest: "what is your quest?"
  
    gather_facts: no
  
    tasks:
  
    - name: Check if path exists
      stat: path=/home/khan/Desktop/tobefetched/file1.txt
      register: st
  
    - name: It exists
      debug: msg='Path existence verified!'
      when: st.stat.exists
  
     - name: It doesn't exist
       debug: msg="Path does not exist"
       when: st.stat.exists == false
  
     - name: Copy file2 if it exists
       fetch: src=/home/khan/Desktop/tobefetched/file2.txt dest=/home/khan/Desktop/fetched/   flat=yes
       when: st.stat.exists
  
     - name: Run remotescript.sh and save the output of script to output.txt on the Desktop
       shell: cd /home/imran/Desktop; ./remotescript.sh &gt; output.txt
  
     - name: Find and replace a word in a file placed on the remote node using variables
       shell: cd /home/imran/Desktop/tobefetched; sed -i 's/{{name}}/{{quest}}/g' file1.txt
  
      tags:
         - replace
  </code></pre>
  
  <p>@gli I tried your solution, I have a group in my inventory named test with two nodes in it. When I enter <strong>0..1</strong> I get:</p>
  
  <pre><code>TASK: [echo sequence] ********************************************************* 
  changed: [vm2] =&gt; (item=some_prefix0)
  changed: [vm1] =&gt; (item=some_prefix0)
  changed: [vm1] =&gt; (item=some_prefix1)
  changed: [vm2] =&gt; (item=some_prefix1)
  </code></pre>
  
  <p>Similarly when I enter <strong>1..2</strong> I get:</p>
  
  <pre><code>TASK: [echo sequence] ********************************************************* 
  changed: [vm2] =&gt; (item=some_prefix1)
  changed: [vm1] =&gt; (item=some_prefix1)
  changed: [vm2] =&gt; (item=some_prefix2)
  changed: [vm1] =&gt; (item=some_prefix2)
  </code></pre>
  
  <p>Likewise when I enter <strong>4..5</strong> (nodes not even present in the inventory, I get:</p>
  
  <pre><code>TASK: [echo sequence] ********************************************************* 
  changed: [vm1] =&gt; (item=some_prefix4)
  changed: [vm2] =&gt; (item=some_prefix4)
  changed: [vm1] =&gt; (item=some_prefix5)
  changed: [vm2] =&gt; (item=some_prefix5)
  </code></pre>
  
  <p>Any help would be really appreciated. Thanks!</p>