Brian so the basic idea is using ansible to build out switch templates 
leveraging variables that are specific to each switch device. 

For the purpose of this test I've made the template very simple I need to 
populate the variable {{ from inventory_hostname }} and {{ip}} into the 
template below for each switch. 

The {{ inventory hostname }} variable is retrieved from iterating over the 
hosts:routers in the play as you mention above

The {{ip}} variable is retrieved from host_vars/tor01.a01.yml and 
host_vars/tor01.a02.yml file 

See example below for one of these switches. Again this is being kept very 
simple for demo purposes. 

[vagrant@dev4 host_vars]$ more tor01.a01.yml
hostname: tor01.a01
ip : 10.1.1.1

[vagrant@dev4 host_vars]$

The basic template is below:

[vagrant@dev4 templates]$ more base.j2
!
hostname {{ inventory_hostname}}
!
!
interface GigabitEthernet0/0
 ip access-group INTERNET in
 ip address {{ ip }}
 no ip redirects
 no ip proxy-arp
 ip nat outside
 ip virtual-reassembly
 duplex auto
 speed auto
 no cdp enable
 !

[vagrant@dev4 templates]$

When I run the playbook it combines the variables and the templates to 
create a populated file for each switch that can be uploaded to the device. 

[vagrant@dev4 RTR-TEMPLATE]$ ansible-playbook -i test site.yml


PLAY [Generate router configuration files] 
************************************

TASK: [router | Generate configuration files] 
*********************************
changed: [tor01.a02]
changed: [tor01.a01]

PLAY RECAP 
********************************************************************
tor01.a01                  : ok=1    changed=1    unreachable=0    failed=0
tor01.a02                  : ok=1    changed=1    unreachable=0    failed=0


This is fine as currently I only have two devices to template tor01.a01 and 
tor01.a02  but if this was several hundred devices I'd prefer to keep 
variables in single YAML file below and loop over this to extract the 
{{ip}} for each hostname? 
Is this currently possible with Ansible?
I've tried to do this but can't seem to access the variable {{ip}} within 
tor_switches.yml and not sure how I load variables from this file into 
Ansible for use in playbook?

[vagrant@dev4 RTR-TEMPLATE]$ cat host_vars/tor_switches.yml
---
hostname:
   tor01.a01:
  ip : 10.1.1.1
   tor01.a02:
  ip : 10.1.1.2
[vagrant@dev4 RTR-TEMPLATE]$

On Monday, 8 December 2014 19:39:58 UTC, Brian Coca wrote:
>
> ansible has 2 iterators, the main one is the play, it iterates over hosts 
>
> - hosts: routers 
>
> iterates over every host in the routers group 
>
>
> the second iterator is with_ which acts at task level 
>
> - debug: msg={{item}} 
>   with_items: 
>      - 1 
>      - 2 
>
> this will iterate over 1 and 2 for every host in the play. 
>
> sometimes you want to target hosts from a different group from the one 
> you are targetting in play, most common examples is load balancers 
>
> - hosts: webnodes 
>   tasks: 
>       - shell: disable node {{inventory_hostname}} 
>         delegate_to: "{{item}}" 
>         with_items: groups['loadbalancers'] 
>
> As to the specifics on how this fits with your setup that requries not 
> as much the structure of your files but what is your intent, what you 
> want to accomplish. 
>
> -- 
> Brian Coca 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/302e6fa1-e57e-430d-a591-a09f785382a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to