The example Ansible dynamic inventory script exits with a KeyError exception.
This Bug Report affects these Traffic Control components:
- Automation (Ansible)
Current behavior:
While processing the server list from TO into an inventory, the TO.py dynamic inventory script encounters a KeyError exception:
./TO.py --list --username "myuser" --password "mypass" --url "to.example.com"
ERROR:root:'ipAddress'
Traceback (most recent call last):
File "/Users/dce07/Downloads/apache-trafficcontrol-6.0.2/infrastructure/ansible/dynamic.inventory/./TO.py", line 241, in <module>
INVENTORY = AnsibleInventory(
File "/Users/dce07/Downloads/apache-trafficcontrol-6.0.2/infrastructure/ansible/dynamic.inventory/./TO.py", line 180, in to_inventory
return self.generate_inventory_list(self.to_url)
File "/Users/dce07/Downloads/apache-trafficcontrol-6.0.2/infrastructure/ansible/dynamic.inventory/./TO.py", line 123, in generate_inventory_list
out['_meta']['hostvars'][fqdn]['server_ipAddress'] = server['ipAddress']
KeyError: 'ipAddress'
I believe this is due to differences in the JSON returned by the TO API in v2.0 vs v3.0
Expected behavior:
The dynamic inventory script should return valid JSON, without any errors, for example:
./TO.py --list --username "myuser" --password "mypass" --url "to.example.com" | jq '._meta.hostvars | length'
139
Steps to reproduce:
- Install the traffic_control python client from github, as per the instructions.
- Run TO.py with the --list argument and a valid user, password and URL : ./TO.py --list --username "myuser" --password "mypass" --url "to.example.com"
I have been having similar issues with API 3.0 because it won't return ipAddress and ipAddress6 anymore. This is now change with interfaces.
This is quite a breaking change and I think the "main" interfaces should still be populated as the existing 2.0 fields.
In API (up to 2.0):
$ ./toApi.sh /api/2.0/servers?hostName=cdn1cdedge0001 | jq
{
"response": [
{
"interfaceMtu": 1500,
"interfaceName": "eth0",
"ip6Address": "2001:578:30:9101:68:1:14:141/64",
"ip6Gateway": "2001:578:30:9101::1",
"ipAddress": "68.1.14.141",
"ipGateway": "68.1.14.129",
"ipNetmask": "255.255.255.192",
After 3.0:
"interfaces": [
{
"ipAddresses": [
{
"address": "68.1.14.141/26",
"gateway": "68.1.14.129",
"serviceAddress": true
},
{
"address": "2001:578:30:9101:68:1:14:141/64",
"gateway": "2001:578:30:9101::1",
"serviceAddress": true
}
],
"maxBandwidth": null,
"monitor": true,
"mtu": 1500,
"name": "eth0"
}
],
In 6.x the Ansible scripts need a version of the TO client that defaults to using APIv2 - specifically because of that network interface change. What version is your trafficops package?