In Ansible is there a way to use with_items and host variables at the same time?
<p>So I have 3 hosts I want to run a playbook on.  Each host needs 3 files (all with the same names).</p>

<p>the files are going to be</p>

<ul>
<li>lacpbond.100</li>
<li>lacpbond.200</li>
<li>lacpbond.300</li>
</ul>

<p>Each file has a unique IP address that goes into it based on the vars/ file.</p>

<p>The task looks like this-></p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>- name: configure subinterface bonds
  template: src="ifcfg-lacpbondsub.j2" dest=/etc/sysconfig/network-scripts/ifcfg-lacpbond.{{item.vlan}}
  with_items:
    - { vlan: "100" }
    - { vlan: "200" }
    - { vlan: "300" }
  tags:
    - bonding
    </code></pre>
</div>
</div>
</p>

<p>So the vars looks like this-></p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>  server01:
    loopbacks:
      ipv4: "10.0.0.100"
    SVIS:
      100:
        ipv4: "192.168.0.1"
        prefix: "28"
      200:
        ipv4: "192.168.1.1"
        prefix: "28"
      300:
        ipv4: "192.168.2.1"
        prefix: "28"</code></pre>
</div>
</div>
</p>

<p>Now here comes the problem.  I am not sure how to use with_items and vars in the same time so I can use with_items to defer which variable to use.... this would greatly simplify the complexity of the playbook</p>

<p>here is the template file-></p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>{% set host = interfaces[ansible_hostname] -%}
{% set VLAN = item.vlan -%}

DEVICE=lacpbond.{{item.vlan}}
IPADDR={{host.SVIS.{{item.vlan}}.ipv4}}
ONBOOT=yes
BOOTPROTO=none
VLAN=yes</code></pre>
</div>
</div>
</p>

<p>So the above works obviously if I don't use the {{}} within another {{}}.  But you can see what I am trying.  I can use item.X by itself fine, and I can use anything from vars/ fine.  But I don't know how to do something like
host.SVIS[VLAN].ipv4....</p>

<p>is this possible?  Otherwise I will need 3 tasks with 3 templates.... and if i need more files this is not as scalable....</p>

<p>Your question is a bit unclear (in part because of the issue I pointed out in my comment), but if I understand what you're asking, you can just do something like:</p>

<pre><code>IPADDR={{host.SVIS[item.vlan].ipv4}}
</code></pre>

<p>See the <a href="http://jinja.pocoo.org/docs/dev/templates/#variables" rel="nofollow">Variables</a> section of the Jinja documentation, which says:</p>

<blockquote>
  <p>The following lines do the same thing:</p>

<pre><code>{{ foo.bar }} 
{{ foo['bar'] }}
</code></pre>
</blockquote>

<p><strong>Update</strong></p>

<p>You are getting that error ("AnsibleUndefinedVariable: 'dict object' has no attribute u'100'") because the keys in your dictionary are integers, but the values of the <code>vlan</code> keys in your with_items loop are strings.  That is, <code>host.SVIS[100]</code> exists, but <code>hosts.SVIS['100']</code> does not exist.</p>

<p>Given this playbook:</p>

<pre><code>- hosts: localhost
  vars:
    interfaces:
      server01:
          loopbacks:
            ipv4: "10.0.0.100"
          SVIS:
            100:
              ipv4: "192.168.0.1"
              prefix: "28"
            200:
              ipv4: "192.168.1.1"
              prefix: "28"
            300:
              ipv4: "192.168.2.1"
              prefix: "28"
    ansible_hostname: server01
  tasks:
    - name: configure subinterface bonds
      template:
        src: "ifcfg-lacpbondsub.j2"
        dest: ./ifcfg-lacpbond.{{item.vlan}}
      with_items:
        - { vlan: 100 }
        - { vlan: 200 }
        - { vlan: 300 }
      tags:
        - bonding
</code></pre>

<p>And this template:</p>

<pre><code>{% set host = interfaces[ansible_hostname] -%}

DEVICE=lacpbond.{{item.vlan}}
IPADDR={{host.SVIS[item.vlan].ipv4}}
ONBOOT=yes
BOOTPROTO=none
VLAN=yes
Raw
</code></pre>

<p>I get three files:</p>

<pre><code>$ ls ifcfg-lacpbond.*
ifcfg-lacpbond.100  ifcfg-lacpbond.200  ifcfg-lacpbond.300
</code></pre>

<p>The content of each looks something like:</p>

<pre><code>DEVICE=lacpbond.100
IPADDR=192.168.0.1
ONBOOT=yes
BOOTPROTO=none
VLAN=yes
</code></pre>

python ansible ansible-playbook