From c07ba82d3eca0ae9fd8b9b9810a861d05f5daa8f Mon Sep 17 00:00:00 2001 From: Pilou Date: Wed, 29 Aug 2018 18:14:12 +0200 Subject: [PATCH] Scaleway inventory: allows to connect via private IP (#44342) * scaleway inventory: remove useless duplicate * scaleway inventory: allows to connect using private ip ansible_host was hardcoded and it was not possible to connect using private addresses. This allows to define multiple host variables, values are templates which can use hosts details send by API. For example this config file use private address and defines two variables: plugin: scaleway hostnames: - hostname variables: ansible_host: private_ip state: state image: image.name regions: - ams1 inventory will looks like: { "_meta": { "hostvars": { "testhost": { "ansible_host": "10.1.1.1", "arch": "x86_64", "commercial_type": "START1-M", "hostname": "testhost", "id": "af669464-0c74-4c89-8573-9fe763028448", "image": "CentOS 7.4", "organization": "2cc9a115-380d-4ac0-ba4b-8947eee71325", "public_ipv4": "163.172.1.1", "public_ipv6": "2001:bc8::1", "state": "running", "tags": [ "testtag" ] } } }, [...] } --- lib/ansible/plugins/inventory/scaleway.py | 31 +++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/ansible/plugins/inventory/scaleway.py b/lib/ansible/plugins/inventory/scaleway.py index a7e4baa83a..5ed90bf096 100644 --- a/lib/ansible/plugins/inventory/scaleway.py +++ b/lib/ansible/plugins/inventory/scaleway.py @@ -46,12 +46,20 @@ DOCUMENTATION = ''' - public_ipv6 - hostname - id + variables: + description: 'set individual variables: keys are variable names and + values are templates. Any value returned by the + L(Scaleway API, https://developer.scaleway.com/#servers-server-get) + can be used.' + type: dict ''' EXAMPLES = ''' # scaleway_inventory.yml file in YAML format # Example command line: ansible-inventory --list -i scaleway_inventory.yml +# use hostname as inventory_hostname +# use the private IP address to connect to the host plugin: scaleway regions: - ams1 @@ -59,13 +67,25 @@ regions: tags: - foobar hostnames: - - public_ipv4 + - hostname +variables: + ansible_host: private_ip + state: state + +# use hostname as inventory_hostname and public IP address to connect to the host +plugin: scaleway +hostnames: + - hostname +regions: + - par1 +variables: + ansible_host: public_ip.address ''' import json from ansible.errors import AnsibleError -from ansible.plugins.inventory import BaseInventoryPlugin +from ansible.plugins.inventory import BaseInventoryPlugin, Constructable from ansible.module_utils.scaleway import SCALEWAY_LOCATION from ansible.module_utils.urls import open_url from ansible.module_utils._text import to_native @@ -152,7 +172,7 @@ extractors = { } -class InventoryModule(BaseInventoryPlugin): +class InventoryModule(BaseInventoryPlugin, Constructable): NAME = 'scaleway' def _fill_host_variables(self, host, server_info): @@ -163,7 +183,6 @@ class InventoryModule(BaseInventoryPlugin): "organization", "state", "hostname", - "state" ) for attribute in targeted_attributes: self.inventory.set_variable(host, attribute, server_info[attribute]) @@ -175,7 +194,6 @@ class InventoryModule(BaseInventoryPlugin): if extract_public_ipv4(server_info=server_info): self.inventory.set_variable(host, "public_ipv4", extract_public_ipv4(server_info=server_info)) - self.inventory.set_variable(host, "ansible_host", extract_public_ipv4(server_info=server_info)) if extract_private_ipv4(server_info=server_info): self.inventory.set_variable(host, "private_ipv4", extract_private_ipv4(server_info=server_info)) @@ -233,6 +251,9 @@ class InventoryModule(BaseInventoryPlugin): self.inventory.add_host(group=group, host=hostname) self._fill_host_variables(host=hostname, server_info=host_infos) + # Composed variables + self._set_composite_vars(self.get_option('variables'), host_infos, hostname, strict=False) + def parse(self, inventory, loader, path, cache=True): super(InventoryModule, self).parse(inventory, loader, path) self._read_config_data(path=path)