title: Ansible: Jinja2 doesn't read dictionary object correctly
body: <p>EDIT: I HAD THE WRONG FILE OPEN IN ATOM, CONTINUE TO MY ANSWER TO KNOW HOW.</p>
  <p>I have a playbook that is supposed to spin up websites for three fictional company names, however in one of my Jinja2 templates, it doesn't find a variable that is clearly located in a dictionary. Any thoughts are greatly appreciated!</p>
  <p>Ansible Version:</p>
  <pre><code>ansible 2.10.13
    config file = None
    configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
    executable location = /usr/local/bin/ansible
    python version = 3.6.8 (default, Jan 27 2021, 01:17:18) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
  </code></pre>
  <p>playbook.yml</p>
  <pre><code>---
  - hosts: company_servers
  
    vars:
      company_site:
        totsuki:
          companyname: UA High School
          apache_dir: totsuki_academy
          filename: totsuki_academy
          companyid: 1
        uahs:
          companyname: UA High School
          apache_dir: ua_high_school
          filename: ua_highschool
          companyid: 2
        we:
          companyname: Wayne Enterprises
          apache_dir: wayne_enterprises
          filename: ua_highschool
          companyid: 3
  
    tasks:
      - name: Install Apache
        dnf:
          name: httpd
          state: latest
      - name: Create Website Folders
        file:
          path: /var/www/html/{{ item.value.apache_dir }}
          state: directory
          mode: 0775
        with_dict: &quot;{{company_site}}&quot;
      - name: Create HTML files
        template:
          src: htmltemplate2.html.j2
          dest: /var/www/html/{{ item.value.apache_dir }}/index.html
          owner: apache
          group: apache
          mode: 0775
        with_dict: &quot;{{company_site}}&quot;
      - name: Apache Preference Import
        template:
          src: httpd.conf
          dest: /etc/httpd/conf/httpd.conf
          mode: 0775
      - name: Create Configuration Folders
        file:
          path: /etc/httpd/{{ item }}
          state: directory
          mode: 0775
          loop:
            - sites-enabled
            - sites-available
      - name: Apache Preference Import
        template:
          src: httpd.conf
          dest: /etc/httpd/sites-available
          mode: 0775
      - name: Create a symbolic link
        file:
          src: /etc/httpd/sites-available
          dest: /etc/httpd/sites-enabled
          state: link
      - name: Restart Apache
        service:
          name: httpd
          state: restarted
      - name: Stop Firewall
        service:
          name: firewalld
          state: stopped
  </code></pre>
  <p>Infuriating Error:</p>
  <pre><code>The full traceback is:
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py&quot;, line 1066, in do_template
      res = j2_concat(rf)
    File &quot;&lt;template&gt;&quot;, line 14, in root
    File &quot;/usr/local/lib/python3.6/site-packages/jinja2/runtime.py&quot;, line 903, in _fail_with_undefined_error
      raise self._undefined_exception(self._undefined_message)
  jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'companyname'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py&quot;, line 139, in run
      resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
    File &quot;/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py&quot;, line 1103, in do_template
      raise AnsibleUndefinedVariable(e)
  ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'
  failed: [192.168.100.6] (item={'key': 'we', 'value': {'companyname': 'Wayne Enterprises', 'apache_dir': 'wayne_enterprises', 'filename': 'ua_highschool', 'companyid': 3}}) =&gt; {
      &quot;ansible_loop_var&quot;: &quot;item&quot;,
      &quot;changed&quot;: false,
      &quot;item&quot;: {
          &quot;key&quot;: &quot;we&quot;,
          &quot;value&quot;: {
              &quot;apache_dir&quot;: &quot;wayne_enterprises&quot;,
              &quot;companyid&quot;: 3,
              &quot;companyname&quot;: &quot;Wayne Enterprises&quot;,
              &quot;filename&quot;: &quot;ua_highschool&quot;
          }
      },
      &quot;msg&quot;: &quot;AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'&quot;
  }
  The full traceback is:
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py&quot;, line 1066, in do_template
      res = j2_concat(rf)
    File &quot;&lt;template&gt;&quot;, line 14, in root
    File &quot;/usr/local/lib/python3.6/site-packages/jinja2/runtime.py&quot;, line 903, in _fail_with_undefined_error
      raise self._undefined_exception(self._undefined_message)
  jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'companyname'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.6/site-packages/ansible/plugins/action/template.py&quot;, line 139, in run
      resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
    File &quot;/usr/local/lib/python3.6/site-packages/ansible/template/__init__.py&quot;, line 1103, in do_template
      raise AnsibleUndefinedVariable(e)
  ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'
  failed: [192.168.100.7] (item={'key': 'we', 'value': {'companyname': 'Wayne Enterprises', 'apache_dir': 'wayne_enterprises', 'filename': 'ua_highschool', 'companyid': 3}}) =&gt; {
      &quot;ansible_loop_var&quot;: &quot;item&quot;,
      &quot;changed&quot;: false,
      &quot;item&quot;: {
          &quot;key&quot;: &quot;we&quot;,
          &quot;value&quot;: {
              &quot;apache_dir&quot;: &quot;wayne_enterprises&quot;,
              &quot;companyid&quot;: 3,
              &quot;companyname&quot;: &quot;Wayne Enterprises&quot;,
              &quot;filename&quot;: &quot;ua_highschool&quot;
          }
      },
      &quot;msg&quot;: &quot;AnsibleUndefinedVariable: 'dict object' has no attribute 'companyname'&quot;
  }
  </code></pre>
  <p>Also, I have noticed that Wayne enterprises 'filename' wasn't correct and I did adjust that.
  It says <em>object has no attribute 'companyname'</em></p>
  <p>and here is my html.j2</p>
  <pre><code>&lt;html&gt;
  
    &lt;head&gt;
      &lt;title&gt;{{ item.value.companyname }}&lt;/title&gt;
    &lt;/head&gt;
  
    &lt;body&gt;
      &lt;h1&gt;Welcome to the {{ item.value.companyname }} website&lt;/h1&gt;
      IP Address: {{ ansible_default_ipv4.address }}
    &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>