From d0da544aefea36159f920520efe363e1bdf64c34 Mon Sep 17 00:00:00 2001 From: chouseknecht Date: Wed, 25 May 2016 16:28:53 -0400 Subject: [PATCH] Fix for issue 3740. Make docker_service more resilient when targeting older API versions. --- .../modules/cloud/docker/docker_service.py | 65 ++++++++++++++----- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/lib/ansible/modules/cloud/docker/docker_service.py b/lib/ansible/modules/cloud/docker/docker_service.py index 266ab372a5c..cb0baac508f 100644 --- a/lib/ansible/modules/cloud/docker/docker_service.py +++ b/lib/ansible/modules/cloud/docker/docker_service.py @@ -599,25 +599,54 @@ class ContainerManager(DockerBaseClass): for container in service.containers(stopped=True): inspection = container.inspect() # pare down the inspection data to the most useful bits - facts = dict() - facts['cmd'] = inspection['Config']['Cmd'] - facts['labels'] = inspection['Config']['Labels'] - facts['image'] = inspection['Config']['Image'] - facts['state'] = dict( - running=inspection['State']['Running'], - status=inspection['State']['Status'], + facts = dict( + cmd=[], + labels=dict(), + image=None, + state=dict( + running=None, + status=None + ), + networks=dict() ) - facts['networks'] = dict() - for key, value in inspection['NetworkSettings']['Networks'].items(): - facts['networks'][key] = dict( - aliases=inspection['NetworkSettings']['Networks'][key]['Aliases'], - globalIPv6=inspection['NetworkSettings']['Networks'][key]['GlobalIPv6Address'], - globalIPv6PrefixLen=inspection['NetworkSettings']['Networks'][key]['GlobalIPv6PrefixLen'], - IPAddress=inspection['NetworkSettings']['Networks'][key]['IPAddress'], - IPPrefixLen=inspection['NetworkSettings']['Networks'][key]['IPPrefixLen'], - links=inspection['NetworkSettings']['Networks'][key]['Links'], - macAddress=inspection['NetworkSettings']['Networks'][key]['MacAddress'], - ) + if inspection['Config'].get('Cmd', None) is not None: + facts['cmd'] = inspection['Config']['Cmd'] + if inspection['Config'].get('Labels', None) is not None: + facts['labels'] = inspection['Config']['Labels'] + if inspection['Config'].get('Image', None) is not None: + facts['image'] = inspection['Config']['Image'] + if inspection['State'].get('Running', None) is not None: + facts['state']['running'] = inspection['State']['Running'] + if inspection['State'].get('Status', None) is not None: + facts['state']['status'] = inspection['State']['Status'] + + if inspection.get('NetworkSettings') and inspection['NetworkSettings'].get('Networks'): + networks = inspection['NetworkSettings']['Networks'] + for key in networks: + facts['networks'][key] = dict( + aliases=[], + globalIPv6=None, + globalIPv6PrefixLen=0, + IPAddress=None, + IPPrefixLen=0, + links=None, + macAddress=None, + ) + if networks[key].get('Aliases', None) is not None: + facts['networks'][key]['aliases'] = networks[key]['Aliases'] + if networks[key].get('GlobalIPv6Address', None) is not None: + facts['networks'][key]['globalIPv6'] = networks[key]['GlobalIPv6Address'] + if networks[key].get('GlobalIPv6PrefixLen', None) is not None: + facts['networks'][key]['globalIPv6PrefixLen'] = networks[key]['GlobalIPv6PrefixLen'] + if networks[key].get('IPAddress', None) is not None: + facts['networks'][key]['IPAddress'] = networks[key]['IPAddress'] + if networks[key].get('IPPrefixLen', None) is not None: + facts['networks'][key]['IPPrefixLen'] = networks[key]['IPPrefixLen'] + if networks[key].get('Links', None) is not None: + facts['networks'][key]['links'] = networks[key]['Links'] + if networks[key].get('MacAddress', None) is not None: + facts['networks'][key]['macAddress'] = networks[key]['MacAddress'] + result['ansible_facts'][service.name][container.name] = facts return result