From 56ae3bfec2dfc06e1c07a82de346f0b9ca867424 Mon Sep 17 00:00:00 2001 From: Trond Hasle Amundsen Date: Mon, 10 Jun 2019 08:01:05 +0200 Subject: [PATCH] Add support for IPv6 for Openstack in terraform.py via metadata (#4716) * Add support for IPv6 for Openstack in terraform.py via metadata * document terraform.py metadata variables for openstack --- contrib/terraform/openstack/README.md | 24 ++++++++++++++++++++++++ contrib/terraform/terraform.py | 14 ++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/contrib/terraform/openstack/README.md b/contrib/terraform/openstack/README.md index 95fc53bc4..61d5cd3a4 100644 --- a/contrib/terraform/openstack/README.md +++ b/contrib/terraform/openstack/README.md @@ -325,6 +325,30 @@ $ ssh-add ~/.ssh/id_rsa If you have deployed and destroyed a previous iteration of your cluster, you will need to clear out any stale keys from your SSH "known hosts" file ( `~/.ssh/known_hosts`). +#### Metadata variables + +The [python script](../terraform.py) that reads the +generated`.tfstate` file to generate a dynamic inventory recognizes +some variables within a "metadata" block, defined in a "resource" +block (example): + +``` +resource "openstack_compute_instance_v2" "example" { + ... + metadata { + ssh_user = "ubuntu" + prefer_ipv6 = true + python_bin = "/usr/bin/python3" + } + ... +} +``` + +As the example shows, these let you define the SSH username for +Ansible, a Python binary which is needed by Ansible if +`/usr/bin/python` doesn't exist, and whether the IPv6 address of the +instance should be preferred over IPv4. + #### Bastion host Bastion access will be determined by: diff --git a/contrib/terraform/terraform.py b/contrib/terraform/terraform.py index 24a95622e..60ca3f8e5 100755 --- a/contrib/terraform/terraform.py +++ b/contrib/terraform/terraform.py @@ -239,10 +239,16 @@ def openstack_host(resource, module_name): attrs['private_ipv4'] = raw_attrs['network.0.fixed_ip_v4'] try: - attrs.update({ - 'ansible_ssh_host': raw_attrs['access_ip_v4'], - 'publicly_routable': True, - }) + if 'metadata.prefer_ipv6' in raw_attrs and raw_attrs['metadata.prefer_ipv6'] == "1": + attrs.update({ + 'ansible_ssh_host': re.sub("[\[\]]", "", raw_attrs['access_ip_v6']), + 'publicly_routable': True, + }) + else: + attrs.update({ + 'ansible_ssh_host': raw_attrs['access_ip_v4'], + 'publicly_routable': True, + }) except (KeyError, ValueError): attrs.update({'ansible_ssh_host': '', 'publicly_routable': False})