How-to Variable in Variable

104 views
Skip to first unread message

bonheu...@gmail.com

unread,
Oct 28, 2016, 9:40:04 AM10/28/16
to Ansible Project
Hi everyone

I'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.

Thanks
Bonh

Kai Stian Olstad

unread,
Oct 28, 2016, 10:46:30 AM10/28/16
to ansible...@googlegroups.com
> *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

bonheu...@gmail.com

unread,
Oct 29, 2016, 6:42:27 AM10/29/16
to Ansible Project, ansible-pr...@olstad.com
Hi

Still have an error

failed: [NRT001] (item={u'model': u'NRT4451', u'hostname': u'NRT001'}) => {"failed": true, "item": {"hostname": "NRT001", "model": "NRT4451"}, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute u'NRT4451'"}

Kai Stian Olstad

unread,
Oct 29, 2016, 7:06:57 AM10/29/16
to ansible...@googlegroups.com
On 29. okt. 2016 12:42, bonheu...@gmail.com wrote:
> Hi
>
> Still have an error
>
> *failed: [NRT001] (item={u'model': u'NRT4451', u'hostname': u'NRT001'}) =>
> {"failed": true, "item": {"hostname": "NRT001", "model": "NRT4451"}, "msg":
> "AnsibleUndefinedVariable: 'dict object' has no attribute u'NRT4451'"}*

Is your variable definition in the roles vars/main.yml, if so this
should work.

interface {{ vars[item.model]['wan'] }}

--
Kai Stian Olstad

Vikas Kumar

unread,
Oct 29, 2016, 7:32:47 AM10/29/16
to Ansible Project
Have a look here.

bonheu...@gmail.com

unread,
Oct 29, 2016, 8:23:27 AM10/29/16
to Ansible Project, ansible-pr...@olstad.com
interface {{ vars[item.model]['wan'] }} works like a charm. 
Thanks guys! really appreciate!

I was struggling to find the magic predefined variable name to point to vars/main.yml.
Where is that documented? I looked here and here
Reply all
Reply to author
Forward
0 new messages