title: Accessing nested variable variables in Ansible
body: <p>Here is my <code>group_vars/all</code> file:</p>
  <pre><code>app_env: staging
  
  staging:
    app_a:
      db_host: localhost
    app_b:
      db_host: localhost
  production:
    app_a:
      db_host: app_a-db.example.net
    app_b:
      db_host: app_b-db.example.com
  </code></pre>
  <p>If <code>app_env</code> environment has to be production, I overwrite this via inventory variables. This way, all deployments are staging unless you make them production explicitly.</p>
  <p>So, when I want to print the variable in a playbook, I can do</p>
  <pre><code>---
    - debug: var={{app_env}}.app_a.db_host
  </code></pre>
  <p>This works!</p>
  <p>But how can I access the variable in another module, i.e. <code>lineinfile</code>?</p>
  <p>Some Examples that didn't work out:</p>
  <pre><code>- lineinfile: dest=/etc/profile line='export APP_A_DB_HOST=&quot;{{ app_env.app_a.db_host }}&quot;'
  - lineinfile: dest=/etc/profile line='export APP_A_DB_HOST=&quot;{{ app_env[app_a][db_host] }}&quot;'
  - lineinfile: dest=/etc/profile line='export APP_A_DB_HOST=&quot;{{ {{app_env}}.app_a.db_host }}&quot;'
  </code></pre>
  <p>Working solutions would be using the <code>set_fact</code> module (double lines of code, not really smart) or including different variable files, depending on <code>app_env</code>.</p>
  <p>But I really would like to know if there's a notation to access nested variable variables ;)</p>