with_items specific variables

Hi,

New to Ansible and probably not using it the way most are. I’m a network engineer and am trying to use it to generate config files using the template module. It’s working well for one file but I’d like to extend it to support multiple “hosts” that use different variables.

In my hosts file, I only have localhost defined (since I’m just writing data to files). I have a role “switch” that includes one task using with_items set to “hostname: item1” etc. I am wondering if you can specify a vars_files for each of the “items”? I tried to set vars_files in the playbook site.yml to “vars/roles/switch/vars/{{item.hostname}}.yml” but that doesn’t work. I think I could “trick” Ansible by defining multiple hosts to resolve to 127.0.0.1 in the hosts file but would rather avoid that. Any suggestions?

Ummm, I’m not entirely sure what you want to do but you can easily use a dict to set the variables…

hostlist:

  • { hostname: ‘name1’, item1: ‘value1A’. item2: ‘value1B’ }

  • { hostname: ‘name2’, item1: ‘value2A’. item2: ‘value2B’ }

Then you have {{ item.hostname }}. {{ item.item1 }} and {{ item.item2 }}

Does this help?