title: copy and running configfiles programatically in ansible
body: <p>There is a ansible code i am writing. It does two tasks, first is to copy a configuration file to target instance in groups. Second is running that config file to install the application. </p>
  
  <p>I am creating configuration file and inventory programtically so that same suffix is added to both configfile name &amp; groupname in inventory:</p>
  
  <pre><code>Configfile name example :            Equivalent group names:
  myappconf1                             [myapp1]
                                         hostname
  myappconf2                             [myapp2]
                                         hostname
  </code></pre>
  
  <p>This is my code for <strong>copying files</strong></p>
  
  <pre><code>hosts: all
  tasks:
  name: Copy file.role1 to host1
   copy: src=/tmp/myconf1 dest=/tmp
   when:
    - "'myapp1' in group_names"
  
  
  name: Copy Config File two to  to Ldap2
   copy: src=/tmp/myconf2 dest=/tmp
   when:
    - "'myapp2' in group_names"
  </code></pre>
  
  <p>This is my code for <strong>running conf file</strong></p>
  
  <pre><code>hosts: myapp1
  tasks:
    - command: "/tmp/mainapp/update.sh -f myappconf1"
  
  hosts: myapp2
  tasks:
    - command: "/tmp/mainapp/update.sh -f myappconf1"
  </code></pre>
  
  <p>But depending on user input uncertain number of conf files and groups can be created so I would like to do task more programatically. <strong>desired code may look like</strong>:</p>
  
  <pre><code>[task for copying file]
  hosts: ~(myapp)
  tasks:
  - copy: copy the appropriate file to the host 
          example: copy myappconf4 to myapp4
  - command: run the commmand with appropirate file
             example: for myapp3, command: /tmp/mainapp/update.sh -f myappconf3
  </code></pre>
  
  <p>Can someone please suggest me what i can use to make my code more generic and efficient ?</p>