On 28. okt. 2016 11:26, [email protected] wrote:
Hi everyoneI'm new to Ansible and I'm trying to automate the generation of Cisco router configuration files. For the same config template, I will have several hardware models and I'd like to keep the code simple. #main.yml (roles tasks) --- - name: Generate configuration files template: src=CE-base.j2 dest=/home/bonh/ansible/playbooks/configs/{{item. hostname}}.txt with_items: "{{ CE }}" #main.yml (variable definition) --- CE: - { hostname: NRT001, model: NRT4451 } - { hostname: NRT002, model: NRT2921 } NRT4451: wan: GigabitEthernet0/0/1 b2b: GigabitEthernet0/0/2 lan: GigabitEthernet0/0/0 NRT2921: wan: GigabitEthernet0/1 b2b: GigabitEthernet0/2 lan: GigabitEthernet0/0 #CE-base.j2 (base template) --- interface {{ [item.model].wan }} The desired result is to have the following #NRT001.txt interface GigabitEthernet0/0/1 #NRT002.txt interface GigabitEthernet0/1 However I'm getting an error. *failed: [NRT001] (item={u'model': u'NRT4451', u'hostname': u'NRT001'}) => {"failed": true, "item": {"hostname": "NRT001", "model": "NRT4451"}, "msg": "AnsibleUndefinedVariable: 'list object' has no attribute 'wan'"}* I'm not a hardcore devOps and still learning. I tried the approach "How do I access a variable name programmatically?" in http://docs.ansible.com/ansible/faq.html but no success. I think that's because my item has not a 'wan' object. Of course if I use interface {{ NRT4451.wan }} it works fine. Can someone help? I'm hoping it's just a syntax mistake.
"How do I access a variable name programmatically" is the correct way to do it
https://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-name-programmatically interface {{ hostvars[inventory_hostname][item.model]['wan'] }} -- Kai Stian Olstad -- 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/9253f1e5-98ea-2c65-eecc-26e7f7ac63e7%40olstad.com. For more options, visit https://groups.google.com/d/optout.
