Hi,
…I#m having a problem supplying a dictionary as parameter to a role. Seems it’s converted to Unicode and don’t know how to proceed. The basic idea is to have a task separated in a role and call it with different input parameters during the playbook. Looks like when assigning a parameter using “{{ }}” I almost got where I want to go (iterate over the supplied param dict), but somehow I’m unable to do that?!
Any hint greatly appreciated!
Thanks,
Andreas
Test-inventory:
ansible@test:~$ cat test_inv.yml
---
all:
vars:
vlans:
3800:
name: TEST-VLAN-3800
otv_extended: true
3801:
name: TEST-VLAN-3801
otv_extended: true
3802:
name: TEST-VLAN-3802
otv_extended: true
test:
hosts:
localhost
Playbook:
ansible@test:~$ cat test_pb.yml
---
- name: call role and supply dict w/o jinja2 template
hosts: all
# connection: network_cli
gather_facts: no
roles:
- role: role_test
vl: vlans
tasks:
- debug:
var: vlans
- name: call role and supply dict w/ jinja2 template
hosts: all
# connection: network_cli
gather_facts: no
roles:
- role: role_test
vl: "{{ vlans }}"
Running the Playbook shows the following output:
ansible@test:~$ ansible-playbook -i test_inv.yml test_pb.yml
PLAY [call role and supply dict w/o jinja2 template] ***********************************************************************************************************************************************************
TASK [role_test : show original dict] **************************************************************************************************************************************************************************
ok: [localhost] => {
"vlans": {
"3800": {
"name": "TEST-VLAN-3800",
"otv_extended": true
},
"3801": {
"name": "TEST-VLAN-3801",
"otv_extended": true
},
"3802": {
"name": "TEST-VLAN-3802",
"otv_extended": true
}
}
}
TASK [role_test : show orignal dict - vlan 3801] ***************************************************************************************************************************************************************
ok: [localhost] => {
"vlans[3801]": {
"name": "TEST-VLAN-3801",
"otv_extended": true
}
}
TASK [role_test : loop over original dict] *********************************************************************************************************************************************************************
ok: [localhost] => (item=3800) => {
"item": 3800
}
ok: [localhost] => (item=3801) => {
"item": 3801
}
ok: [localhost] => (item=3802) => {
"item": 3802
}
TASK [role_test : show supplied dict] **************************************************************************************************************************************************************************
ok: [localhost] => {
"vlans": {
"3800": {
"name": "TEST-VLAN-3800",
"otv_extended": true
},
"3801": {
"name": "TEST-VLAN-3801",
"otv_extended": true
},
"3802": {
"name": "TEST-VLAN-3802",
"otv_extended": true
}
}
}
#
# in the following section param was set without “{{ }}” and assigned as string “vlan”
#
TASK [role_test : show supplied dict - vlan 3801] **************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute '3801'\n\nThe error appears to have been in '/code/roles/role_test/tasks/main.yml': line 26, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# error-msg: ansible.parsing.yaml.objects.AnsibleUnicode object has no element 3801\n- name: show supplied dict - vlan 3801\n ^ here\n"}
...ignoring
TASK [role_test : loop over supplied dict] *********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: vlans. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}
...ignoring
TASK [debug] ***************************************************************************************************************************************************************************************************
ok: [localhost] => {
"vlans": {
"3800": {
"name": "TEST-VLAN-3800",
"otv_extended": true
},
"3801": {
"name": "TEST-VLAN-3801",
"otv_extended": true
},
"3802": {
"name": "TEST-VLAN-3802",
"otv_extended": true
}
}
}
#
# in the following section param was set using “{{ vlans }}”
#
PLAY [call role and supply dict w/ jinja2 template] ************************************************************************************************************************************************************
TASK [role_test : show original dict] **************************************************************************************************************************************************************************
ok: [localhost] => {
"vlans": {
"3800": {
"name": "TEST-VLAN-3800",
"otv_extended": true
},
"3801": {
"name": "TEST-VLAN-3801",
"otv_extended": true
},
"3802": {
"name": "TEST-VLAN-3802",
"otv_extended": true
}
}
}
TASK [role_test : show orignal dict - vlan 3801] ***************************************************************************************************************************************************************
ok: [localhost] => {
"vlans[3801]": {
"name": "TEST-VLAN-3801",
"otv_extended": true
}
}
TASK [role_test : loop over original dict] *********************************************************************************************************************************************************************
ok: [localhost] => (item=3800) => {
"item": 3800
}
ok: [localhost] => (item=3801) => {
"item": 3801
}
ok: [localhost] => (item=3802) => {
"item": 3802
}
TASK [role_test : show supplied dict] **************************************************************************************************************************************************************************
ok: [localhost] => {
"<type 'dict'>": "VARIABLE IS NOT DEFINED!"
}
TASK [role_test : show supplied dict - vlan 3801] **************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '3801'\n\nThe error appears to have been in '/code/roles/role_test/tasks/main.yml': line 26, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# error-msg: ansible.parsing.yaml.objects.AnsibleUnicode object has no element 3801\n- name: show supplied dict - vlan 3801\n ^ here\n"}
...ignoring
TASK [role_test : loop over supplied dict] *********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: {3800: {u'name': u'TEST-VLAN-3800', u'otv_extended': True}, 3801: {u'name': u'TEST-VLAN-3801', u'otv_extended': True}, 3802: {u'name': u'TEST-VLAN-3802', u'otv_extended': True}}. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}
...ignoring
PLAY RECAP *****************************************************************************************************************************************************************************************************
localhost : ok=13 changed=0 unreachable=0 failed=0