Repository: ansible/ansible

Issue:
Scaleway inventory on achived machines
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
The Scaleway dynamic inventory doesn't work if it matches a box which has an archived state. 

```
 [WARNING]:  * Failed to parse scaleway-inventory.yml with auto plugin: 'NoneType' object has no attribute '__getitem__'
```
This can be explained by the fact an archived VM doesn't have a location defined through the Scaleway API

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0
  config file = /Users/abarbare/.ansible.cfg
  configured module search path = [u'/Users/abarbare/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.6.0/libexec/lib/python2.7/site-packages/ansible
  executable location = /usr/local/Cellar/ansible/2.6.0/bin/ansible
  python version = 2.7.15 (default, Jun 17 2018, 12:46:58) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
N/A
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

Create two Scaleway box, with an `ansible` tag on it. Archive one of the box and try to use the following inventory:

<!--- Paste example playbooks or commands between quotes below -->
```yaml
plugin: scaleway
regions:
  - par1
tags:
  - ansible
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
```
$ ansible -i scaleway-inventory.yml all -m ping
<box-up-public-ip> | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
$ ansible -i scaleway-inventory.yml all -m ping
 [WARNING]:  * Failed to parse scaleway-inventory.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory

 [WARNING]:  * Failed to parse scaleway-inventory.yml with ini plugin: /Users/abarbare/Documents/dbaas/image-factory/postgres96/ansible/scaleway-inventory.yml:1: Expected key=value host variable assignment, got: scaleway

 [WARNING]:  * Failed to parse scaleway-inventory.yml with auto plugin: 'NoneType' object has no attribute '__getitem__'

 [WARNING]: Unable to parse ansible/scaleway-inventory.yml as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty,  only localhost is available. Note that the implicit localhost does not match 'all'

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/inventory/scaleway.py b/lib/ansible/plugins/inventory/scaleway.py
--- a/lib/ansible/plugins/inventory/scaleway.py
+++ b/lib/ansible/plugins/inventory/scaleway.py
@@ -102,15 +102,24 @@ def extract_public_ipv4(server_info):
 
 
 def extract_private_ipv4(server_info):
-    return server_info["private_ip"]
+    try:
+        return server_info["private_ip"]
+    except (KeyError, TypeError):
+        return None
 
 
 def extract_hostname(server_info):
-    return server_info["hostname"]
+    try:
+        return server_info["hostname"]
+    except (KeyError, TypeError):
+        return None
 
 
 def extract_server_id(server_info):
-    return server_info["id"]
+    try:
+        return server_info["id"]
+    except (KeyError, TypeError):
+        return None
 
 
 def extract_public_ipv6(server_info):
@@ -120,6 +129,19 @@ def extract_public_ipv6(server_info):
         return None
 
 
+def extract_tags(server_info):
+    try:
+        return server_info["tags"]
+    except (KeyError, TypeError):
+        return None
+
+
+def extract_zone(server_info):
+    try:
+        return server_info["location"]["zone_id"]
+    except (KeyError, TypeError):
+        return None
+
 extractors = {
     "public_ipv4": extract_public_ipv4,
     "private_ipv4": extract_private_ipv4,
@@ -161,8 +183,12 @@ def _get_zones(self, config_zones):
         return set(SCALEWAY_LOCATION.keys()).intersection(config_zones)
 
     def match_groups(self, server_info, tags):
-        server_zone = server_info["location"]["zone_id"]
-        server_tags = server_info["tags"]
+        server_zone = extract_zone(server_info=server_info)
+        server_tags = extract_tags(server_info=server_info)
+
+        # If a server does not have a zone, it means it is archived
+        if server_zone is None:
+            return set()
 
         # If no filtering is defined, all tags are valid groups
         if tags is None:


Test patch: