title: Ansible: Include playbook according to inventory variable
body: <p>I am trying to set up Ansible to be able to run a playbook according to what inventory group the host is in. For example, in the inventory, we have:</p>
  
  <pre><code>[group1]
  host1.sub.domain.tld ansible_host=10.0.0.2
  ...
  [group1:vars]
  whatsmyplaybook=build-server.yml
  </code></pre>
  
  <p>Then we want to make a simple playbook that will more or less redirect to the playbook that is in the inventory:</p>
  
  <pre><code>---
  - name: Load Playbook from inventory
    include: "{{hostvars[server].whatsmyplaybook}}"
  </code></pre>
  
  <p>Where the "<code>server</code>" variable would be the host's FQDN, passed in from the command line:</p>
  
  <pre><code>ansible-playbook whatsmyplaybook.yml -e "server=host1.sub.domain.tld"
  </code></pre>
  
  <p>Our reasoning for this would be to have a server bootstrap itself from a fresh installation (PXE boot), where it will only really know its FQDN, then have a firstboot script SSH to our Ansible host and kick off the above command. However, when we do this, we get the below error:</p>
  
  <pre><code>ERROR! 'hostvars' is undefined
  </code></pre>
  
  <p>This suggests that the inventory is not parsed until a <code>host</code> list is provided, which sucks a lot. Is there another way to accomplish this?</p>