title: How do you get group name of the executing role in ansible
body: <p>I have a use case where I need to create a directory in all hosts in a group whose name will be the name of the group.</p>
  
  <p>Eg :
  I create a dynamic inventory file with output of the form :</p>
  
  <pre><code>{
     "db": ["host1", "host2", "host3"],
     "logs": ["host2"],
     "misc": ["host4"]
  }
  </code></pre>
  
  <p>I need to create a directory by the name db in every host of the group db and directory named log in every host of the group log.</p>
  
  <p>My playbook so far looks like :</p>
  
  <pre><code>- hosts: db
    tasks:
    - name: create db directory
      file: path=db state=directory
  
  - hosts: logs
    tasks:
    - name: create logs directory
      file: path=logs state=directory
  </code></pre>
  
  <p>The number of groups is at least 10+. This may grow in the future. 
  I want to write something that will be easily extendable and manageable for the future groups.</p>
  
  <p>I read that ansible doesn't provide a default fact or variable for group name for the task. I see that this can be best done if I provide a variable like groupname in host.</p>
  
  <p>Is there a better solution that could use looping instead of repeating the same task?</p>