title: Is there a way to reference group variables when importing in Ansible?
body: <p>In my Ansible scripts, is there a way to reference a path variable when importing a playbook?</p>
  
  <p>I have set up some variables with the path to different files in my Ansible file hierarchy that I would like to reference when importing and including playbook and variable files.  The path variables are defined in a file in the group_vars/all/ directory so they are loaded automatically.  </p>
  
  <p>When I try to import a playbook and reference one of my path variables, I get an error that the variable is not defined.</p>
  
  <p>Here's an example.  I created a simple playbook file that I call include.yml:</p>
  
  <pre><code>---
  - name: Include playbook
    import_playbook: "{{ base_dir }}/foo.yml"
  </code></pre>
  
  <p>The playbook imports another playbook called foo.yml in a directory defined in the variable, base_dir.</p>
  
  <p>Here is foo.yml:</p>
  
  <pre><code>---
  - name:
    hosts: localhost
    gather_facts: no
    tasks:
      - name: Check that group_vars/all variable loaded
        debug:
          var: test_dir
  </code></pre>
  
  <p>In the group_vars/all/ directory, I define base_dir and test_dir in a file called dirs.yml:</p>
  
  <pre><code>base_dir: "{{ playbook_dir }}"
  test_dir: "{{ base_dir }}/foo"
  </code></pre>
  
  <p>When I run:</p>
  
  <pre><code>ansible-playbook include.yml
  </code></pre>
  
  <p>I expect that Ansible will import and run foo.yml, which prints the value of the test_dir variable which references base_dir.</p>
  
  <p>Instead, I get the error:</p>
  
  <pre><code>ERROR! 'base_dir' is undefined
  </code></pre>
  
  <p>If I run:</p>
  
  <pre><code>ansible-playbook include.yml --extra-vars base_dir="."
  </code></pre>
  
  <p>then it runs as expected.</p>
  
  <p>It appears that the import occurs before the group_vars variables are loaded.  </p>
  
  <p>If true, this is inconvenient because I would like to define my file paths in global variables that can be referenced by multiple playbooks, instead of hardcoding them in all of my playbooks.  Is there a way around this issue?</p>