From 19381ecffbd723297190f32f1f607ba0a6dc2ce2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Harald=20Jens=C3=A5s?= <hjensas@redhat.com>
Date: Sat, 30 Jun 2018 21:07:55 +0200
Subject: [PATCH] EC2MetadatIp using get_attr

The route to metadata service is set up in host_routes
of ctlplane subnets by extraconf post deploy::
  extraconfig/post_deploy/undercloud_ctlplane_network.py

Use get_attr on the server resource to resolve attribute
value from the subnet(s) and pass it to the parameter
'EC2MetadatIp' used in the THT/network/config/* templates.

Changes the default for 'EC2MetadatIp' to ''.
Removes the comment that the value should be overriden in
parameters_defaults. It also removes the parameter from
network-environment templates.

A conditinal is used in  puppet/role.role.j2.yaml so that
the parameter value is used whenever it is not '' (the
default) to provide backwards compatibility in case the
user set a different value for this parameter in
network-environment.yaml.

When deploying a routed control plane the network config
templates would previously need to be updated to carry
'EC2MetadatIpLeafX' parameters for each leaf.  By getting
the value to pass from the server resource this change
reduces the required nic-config template customisation.
(Reduces the risk of user error.)

Partial: blueprint tripleo-routed-networks-templates
Change-Id: I9c019ec840a44ca8c5f98be55daea365bc6554ec
---
 .../net-config-multinode-os-net-config.yaml   |  5 ++++
 ci/common/net-config-multinode.yaml           |  5 ++++
 ci/common/net-config-simple-bridge.yaml       |  6 +++++
 .../network-environment.yaml                  |  1 -
 .../nic-configs/ceph-storage.yaml             |  6 +++--
 .../nic-configs/cinder-storage.yaml           |  6 +++--
 .../nic-configs/compute.yaml                  |  6 +++--
 .../nic-configs/controller.yaml               |  6 +++--
 .../nic-configs/swift-storage.yaml            |  6 +++--
 .../multiple-nics/network-environment.yaml    |  1 -
 .../nic-configs/ceph-storage.yaml             |  6 +++--
 .../nic-configs/cinder-storage.yaml           |  6 +++--
 .../multiple-nics/nic-configs/compute.yaml    |  6 +++--
 .../multiple-nics/nic-configs/controller.yaml |  6 +++--
 .../nic-configs/swift-storage.yaml            |  6 +++--
 .../public-bond/network-environment.yaml      |  1 -
 .../public-bond/nic-configs/ceph-storage.yaml |  6 +++--
 .../nic-configs/cinder-storage.yaml           |  6 +++--
 .../public-bond/nic-configs/compute.yaml      |  6 +++--
 .../public-bond/nic-configs/controller.yaml   |  6 +++--
 .../nic-configs/swift-storage.yaml            |  6 +++--
 environments/network-environment-v6.j2.yaml   |  4 ++-
 environments/network-environment.j2.yaml      |  3 ++-
 net-config-bond.j2.yaml                       |  5 ++++
 net-config-bridge.j2.yaml                     |  5 ++++
 net-config-linux-bridge.j2.yaml               |  7 +++---
 net-config-noop.j2.yaml                       |  5 ++++
 net-config-standalone.j2.yaml                 |  5 ++++
 ...g-static-bridge-with-external-dhcp.j2.yaml |  6 +++--
 net-config-static-bridge.j2.yaml              |  6 +++--
 net-config-static.j2.yaml                     |  6 +++--
 net-config-undercloud.j2.yaml                 |  5 ++++
 .../controller-no-external.j2.yaml            |  6 +++--
 .../bond-with-vlans/controller-v6.j2.yaml     |  6 +++--
 .../config/bond-with-vlans/role.role.j2.yaml  |  6 +++--
 .../config/multiple-nics/compute-dvr.j2.yaml  |  6 +++--
 .../multiple-nics/controller-v6.j2.yaml       |  6 +++--
 .../config/multiple-nics/role.role.j2.yaml    |  6 +++--
 .../controller-v6.j2.yaml                     |  6 +++--
 .../role.role.j2.yaml                         |  6 +++--
 .../controller-no-external.j2.yaml            |  6 +++--
 .../single-nic-vlans/controller-v6.j2.yaml    |  6 +++--
 .../config/single-nic-vlans/role.role.j2.yaml |  6 +++--
 puppet/role.role.j2.yaml                      | 18 +++++++++++++
 ...-attrs-ec2metadataip-aa28f3c030f13c9c.yaml | 25 +++++++++++++++++++
 45 files changed, 209 insertions(+), 66 deletions(-)
 create mode 100644 releasenotes/notes/use-subnet-attrs-ec2metadataip-aa28f3c030f13c9c.yaml

diff --git a/ci/common/net-config-multinode-os-net-config.yaml b/ci/common/net-config-multinode-os-net-config.yaml
index 6c5cfbeb2a..09cee756f3 100644
--- a/ci/common/net-config-multinode-os-net-config.yaml
+++ b/ci/common/net-config-multinode-os-net-config.yaml
@@ -52,6 +52,11 @@ parameters:
     default: 1300
     description: The mtu of the OvS bridge
     type: number
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
 
 resources:
 
diff --git a/ci/common/net-config-multinode.yaml b/ci/common/net-config-multinode.yaml
index 3cf40e1236..226ea43062 100644
--- a/ci/common/net-config-multinode.yaml
+++ b/ci/common/net-config-multinode.yaml
@@ -48,6 +48,11 @@ parameters:
     description: The default route of the control plane network. (The parameter
       is automatically resolved from the ctlplane subnet's gateway_ip attribute.)
     type: string
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
 
 resources:
   OsNetConfigImpl:
diff --git a/ci/common/net-config-simple-bridge.yaml b/ci/common/net-config-simple-bridge.yaml
index 575984c392..0d6617eb26 100644
--- a/ci/common/net-config-simple-bridge.yaml
+++ b/ci/common/net-config-simple-bridge.yaml
@@ -41,6 +41,12 @@ parameters:
     default: ''
     description: IP address/subnet on the management network
     type: string
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
+
 resources:
   OsNetConfigImpl:
     type: OS::Heat::SoftwareConfig
diff --git a/ci/environments/network/multiple-nics-ipv6/network-environment.yaml b/ci/environments/network/multiple-nics-ipv6/network-environment.yaml
index 860aa32e96..bfc2ab0579 100644
--- a/ci/environments/network/multiple-nics-ipv6/network-environment.yaml
+++ b/ci/environments/network/multiple-nics-ipv6/network-environment.yaml
@@ -7,7 +7,6 @@ resource_registry:
   OS::TripleO::CephStorage::Net::SoftwareConfig: nic-configs/ceph-storage.yaml
 
 parameter_defaults:
-  EC2MetadataIp: 192.168.24.1
   ExternalNetCidr: 2001:db8:fd00:1000::/64
   ExternalAllocationPools: [{"start": "2001:db8:fd00:1000::10", "end": "2001:db8:fd00:1000:ffff:ffff:ffff:fffe"}]
   ExternalInterfaceDefaultRoute: 2001:db8:fd00:1000::1
diff --git a/ci/environments/network/multiple-nics-ipv6/nic-configs/ceph-storage.yaml b/ci/environments/network/multiple-nics-ipv6/nic-configs/ceph-storage.yaml
index e55c7f2e4a..3c72fef5ae 100644
--- a/ci/environments/network/multiple-nics-ipv6/nic-configs/ceph-storage.yaml
+++ b/ci/environments/network/multiple-nics-ipv6/nic-configs/ceph-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics-ipv6/nic-configs/cinder-storage.yaml b/ci/environments/network/multiple-nics-ipv6/nic-configs/cinder-storage.yaml
index de5d919635..853ab6fc96 100644
--- a/ci/environments/network/multiple-nics-ipv6/nic-configs/cinder-storage.yaml
+++ b/ci/environments/network/multiple-nics-ipv6/nic-configs/cinder-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics-ipv6/nic-configs/compute.yaml b/ci/environments/network/multiple-nics-ipv6/nic-configs/compute.yaml
index b2fe4f72a6..2aceec6bdc 100644
--- a/ci/environments/network/multiple-nics-ipv6/nic-configs/compute.yaml
+++ b/ci/environments/network/multiple-nics-ipv6/nic-configs/compute.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics-ipv6/nic-configs/controller.yaml b/ci/environments/network/multiple-nics-ipv6/nic-configs/controller.yaml
index a119e720ad..700f24a36a 100644
--- a/ci/environments/network/multiple-nics-ipv6/nic-configs/controller.yaml
+++ b/ci/environments/network/multiple-nics-ipv6/nic-configs/controller.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics-ipv6/nic-configs/swift-storage.yaml b/ci/environments/network/multiple-nics-ipv6/nic-configs/swift-storage.yaml
index de5d919635..853ab6fc96 100644
--- a/ci/environments/network/multiple-nics-ipv6/nic-configs/swift-storage.yaml
+++ b/ci/environments/network/multiple-nics-ipv6/nic-configs/swift-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics/network-environment.yaml b/ci/environments/network/multiple-nics/network-environment.yaml
index 4f5e0b5274..1d544d1b27 100644
--- a/ci/environments/network/multiple-nics/network-environment.yaml
+++ b/ci/environments/network/multiple-nics/network-environment.yaml
@@ -7,7 +7,6 @@ resource_registry:
   OS::TripleO::CephStorage::Net::SoftwareConfig: nic-configs/ceph-storage.yaml
 
 parameter_defaults:
-  EC2MetadataIp: 192.168.24.1
   ExternalNetCidr: 10.0.0.0/24
   ExternalAllocationPools: [{"start": "10.0.0.10", "end": "10.0.0.50"}]
   ExternalInterfaceDefaultRoute: 10.0.0.1
diff --git a/ci/environments/network/multiple-nics/nic-configs/ceph-storage.yaml b/ci/environments/network/multiple-nics/nic-configs/ceph-storage.yaml
index 6e3ac01a03..47c18a109a 100644
--- a/ci/environments/network/multiple-nics/nic-configs/ceph-storage.yaml
+++ b/ci/environments/network/multiple-nics/nic-configs/ceph-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics/nic-configs/cinder-storage.yaml b/ci/environments/network/multiple-nics/nic-configs/cinder-storage.yaml
index 4ca0188b09..9ed88118a9 100644
--- a/ci/environments/network/multiple-nics/nic-configs/cinder-storage.yaml
+++ b/ci/environments/network/multiple-nics/nic-configs/cinder-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics/nic-configs/compute.yaml b/ci/environments/network/multiple-nics/nic-configs/compute.yaml
index 444726d581..7c00aeab3a 100644
--- a/ci/environments/network/multiple-nics/nic-configs/compute.yaml
+++ b/ci/environments/network/multiple-nics/nic-configs/compute.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics/nic-configs/controller.yaml b/ci/environments/network/multiple-nics/nic-configs/controller.yaml
index f58604af83..9294fa6220 100644
--- a/ci/environments/network/multiple-nics/nic-configs/controller.yaml
+++ b/ci/environments/network/multiple-nics/nic-configs/controller.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/multiple-nics/nic-configs/swift-storage.yaml b/ci/environments/network/multiple-nics/nic-configs/swift-storage.yaml
index 4ca0188b09..9ed88118a9 100644
--- a/ci/environments/network/multiple-nics/nic-configs/swift-storage.yaml
+++ b/ci/environments/network/multiple-nics/nic-configs/swift-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/public-bond/network-environment.yaml b/ci/environments/network/public-bond/network-environment.yaml
index 6ff32a7049..4924b5b9a0 100644
--- a/ci/environments/network/public-bond/network-environment.yaml
+++ b/ci/environments/network/public-bond/network-environment.yaml
@@ -7,7 +7,6 @@ resource_registry:
   OS::TripleO::CephStorage::Net::SoftwareConfig: nic-configs/ceph-storage.yaml
 
 parameter_defaults:
-  EC2MetadataIp: 192.168.24.1
   ExternalNetCidr: 10.0.0.0/24
   ExternalAllocationPools: [{"start": "10.0.0.10", "end": "10.0.0.50"}]
   ExternalInterfaceDefaultRoute: 10.0.0.1
diff --git a/ci/environments/network/public-bond/nic-configs/ceph-storage.yaml b/ci/environments/network/public-bond/nic-configs/ceph-storage.yaml
index 7b8091f499..bf6eb79ef4 100644
--- a/ci/environments/network/public-bond/nic-configs/ceph-storage.yaml
+++ b/ci/environments/network/public-bond/nic-configs/ceph-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/public-bond/nic-configs/cinder-storage.yaml b/ci/environments/network/public-bond/nic-configs/cinder-storage.yaml
index 94f23c9d7e..e7326565c4 100644
--- a/ci/environments/network/public-bond/nic-configs/cinder-storage.yaml
+++ b/ci/environments/network/public-bond/nic-configs/cinder-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/public-bond/nic-configs/compute.yaml b/ci/environments/network/public-bond/nic-configs/compute.yaml
index 66622a149e..61b1ded96a 100644
--- a/ci/environments/network/public-bond/nic-configs/compute.yaml
+++ b/ci/environments/network/public-bond/nic-configs/compute.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/public-bond/nic-configs/controller.yaml b/ci/environments/network/public-bond/nic-configs/controller.yaml
index 896995ee11..79dbc4e2a8 100644
--- a/ci/environments/network/public-bond/nic-configs/controller.yaml
+++ b/ci/environments/network/public-bond/nic-configs/controller.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/ci/environments/network/public-bond/nic-configs/swift-storage.yaml b/ci/environments/network/public-bond/nic-configs/swift-storage.yaml
index 94f23c9d7e..e7326565c4 100644
--- a/ci/environments/network/public-bond/nic-configs/swift-storage.yaml
+++ b/ci/environments/network/public-bond/nic-configs/swift-storage.yaml
@@ -77,8 +77,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 
 resources:
diff --git a/environments/network-environment-v6.j2.yaml b/environments/network-environment-v6.j2.yaml
index 5242fea944..a2f38518c8 100644
--- a/environments/network-environment-v6.j2.yaml
+++ b/environments/network-environment-v6.j2.yaml
@@ -24,8 +24,10 @@ parameter_defaults:
   # ControlPlaneDefaultRoute: It is no longer a requirement to provide this
   #                           parameter. The attribute is resolved from the
   #                           ctlplane subnet(s).
+  # EC2MetadataIp: It is no longer a requirement to provide this parameter. The
+  #                attribute is resolved from the ctlplane subnet(s).
+  #
   #
-  EC2MetadataIp: 192.168.24.1  # Generally the IP of the Undercloud
 {% for network in networks if network.enabled|default(true) %}
 {%-     if network.name != 'Tenant' %}
   # Customize the IP subnets to match the local environment
diff --git a/environments/network-environment.j2.yaml b/environments/network-environment.j2.yaml
index a44a3dd8d1..25b64745e2 100644
--- a/environments/network-environment.j2.yaml
+++ b/environments/network-environment.j2.yaml
@@ -20,8 +20,9 @@ parameter_defaults:
   # ControlPlaneDefaultRoute: It is no longer a requirement to provide this
   #                           parameter. The attribute is resolved from the
   #                           ctlplane subnet(s).
+  # EC2MetadataIp: It is no longer a requirement to provide this parameter. The
+  #                attribute is resolved from the ctlplane subnet(s).
   #
-  EC2MetadataIp: 192.168.24.1  # Generally the IP of the Undercloud
 {% for network in networks if network.enabled|default(true) %}
   # Customize the IP subnet to match the local environment
 {%-     if network.ipv6|default(false) %}
diff --git a/net-config-bond.j2.yaml b/net-config-bond.j2.yaml
index 537efac323..4e67c8095e 100644
--- a/net-config-bond.j2.yaml
+++ b/net-config-bond.j2.yaml
@@ -27,6 +27,11 @@ parameters:
     description: The default route of the control plane network. (The parameter
       is automatically resolved from the ctlplane subnet's gateway_ip attribute.)
     type: string
+  EC2MetadataIp: # Here for compatability, not used.
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
   {%- for network in networks %}
   {{network.name}}IpSubnet:
     default: ''
diff --git a/net-config-bridge.j2.yaml b/net-config-bridge.j2.yaml
index c1b922122b..0f9b83cbfd 100644
--- a/net-config-bridge.j2.yaml
+++ b/net-config-bridge.j2.yaml
@@ -17,6 +17,11 @@ parameters:
     description: The default route of the control plane network. (The parameter
       is automatically resolved from the ctlplane subnet's gateway_ip attribute.)
     type: string
+  EC2MetadataIp: # Here for compatability, not used.
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
   {%- for network in networks %}
   {{network.name}}IpSubnet:
     default: ''
diff --git a/net-config-linux-bridge.j2.yaml b/net-config-linux-bridge.j2.yaml
index 6a838e63f8..5c5c5491fd 100644
--- a/net-config-linux-bridge.j2.yaml
+++ b/net-config-linux-bridge.j2.yaml
@@ -23,10 +23,11 @@ parameters:
       The subnet CIDR of the control plane network. (The parameter is
       automatically resolved from the ctlplane subnet's cidr attribute.)
     type: string
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
-    default: 169.254.169.254/32
 resources:
   OsNetConfigImpl:
     type: OS::Heat::SoftwareConfig
diff --git a/net-config-noop.j2.yaml b/net-config-noop.j2.yaml
index 8be8f7baf8..14cd711607 100644
--- a/net-config-noop.j2.yaml
+++ b/net-config-noop.j2.yaml
@@ -20,6 +20,11 @@ parameters:
     description: The default route of the control plane network. (The parameter
       is automatically resolved from the ctlplane subnet's gateway_ip attribute.)
     type: string
+  EC2MetadataIp: # Here for compatability, not used.
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
   {%- for network in networks %}
   {{network.name}}IpSubnet:
     default: ''
diff --git a/net-config-standalone.j2.yaml b/net-config-standalone.j2.yaml
index 7480ca38a9..8ae2bb8dc8 100644
--- a/net-config-standalone.j2.yaml
+++ b/net-config-standalone.j2.yaml
@@ -46,6 +46,11 @@ parameters:
         tripleoclient to provide an easy means to pass in custom net configs
         for a standalone node.
     type: json
+  EC2MetadataIp: # Here for compatability, not used.
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
 
 conditions:
   standalone_net_config_override:
diff --git a/net-config-static-bridge-with-external-dhcp.j2.yaml b/net-config-static-bridge-with-external-dhcp.j2.yaml
index 0beff02aa2..4a2c08ddcd 100644
--- a/net-config-static-bridge-with-external-dhcp.j2.yaml
+++ b/net-config-static-bridge-with-external-dhcp.j2.yaml
@@ -28,8 +28,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/net-config-static-bridge.j2.yaml b/net-config-static-bridge.j2.yaml
index f1a6bd363b..ab32b55f8c 100644
--- a/net-config-static-bridge.j2.yaml
+++ b/net-config-static-bridge.j2.yaml
@@ -28,8 +28,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/net-config-static.j2.yaml b/net-config-static.j2.yaml
index 75aa65636c..34509d1325 100644
--- a/net-config-static.j2.yaml
+++ b/net-config-static.j2.yaml
@@ -27,8 +27,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/net-config-undercloud.j2.yaml b/net-config-undercloud.j2.yaml
index 7720d71f1b..1dfc2e884c 100644
--- a/net-config-undercloud.j2.yaml
+++ b/net-config-undercloud.j2.yaml
@@ -42,6 +42,11 @@ parameters:
     default: {}
     description: Custom JSON data to be used to override the os-net-config data in this template. This is meant to be used by net_config_override parameter in tripleoclient to provide an easy means to pass in custom net configs for the Undercloud.
     type: json
+  EC2MetadataIp: # Here for compatability, not used.
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
 
 conditions:
   undercloud_net_config_override:
diff --git a/network/config/bond-with-vlans/controller-no-external.j2.yaml b/network/config/bond-with-vlans/controller-no-external.j2.yaml
index 62e922a5d5..e6efbfbd07 100644
--- a/network/config/bond-with-vlans/controller-no-external.j2.yaml
+++ b/network/config/bond-with-vlans/controller-no-external.j2.yaml
@@ -46,8 +46,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
   BondInterfaceOvsOptions:
     default: ''
diff --git a/network/config/bond-with-vlans/controller-v6.j2.yaml b/network/config/bond-with-vlans/controller-v6.j2.yaml
index a676459188..73b1c6d050 100644
--- a/network/config/bond-with-vlans/controller-v6.j2.yaml
+++ b/network/config/bond-with-vlans/controller-v6.j2.yaml
@@ -44,8 +44,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
   BondInterfaceOvsOptions:
     default: bond_mode=active-backup
diff --git a/network/config/bond-with-vlans/role.role.j2.yaml b/network/config/bond-with-vlans/role.role.j2.yaml
index b6a8c9e3d0..d9d4a5f8d0 100644
--- a/network/config/bond-with-vlans/role.role.j2.yaml
+++ b/network/config/bond-with-vlans/role.role.j2.yaml
@@ -42,8 +42,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
   BondInterfaceOvsOptions:
     default: bond_mode=active-backup
diff --git a/network/config/multiple-nics/compute-dvr.j2.yaml b/network/config/multiple-nics/compute-dvr.j2.yaml
index e631862454..68d403b5eb 100644
--- a/network/config/multiple-nics/compute-dvr.j2.yaml
+++ b/network/config/multiple-nics/compute-dvr.j2.yaml
@@ -44,8 +44,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/network/config/multiple-nics/controller-v6.j2.yaml b/network/config/multiple-nics/controller-v6.j2.yaml
index fde66f50c0..9622be7107 100644
--- a/network/config/multiple-nics/controller-v6.j2.yaml
+++ b/network/config/multiple-nics/controller-v6.j2.yaml
@@ -43,8 +43,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/network/config/multiple-nics/role.role.j2.yaml b/network/config/multiple-nics/role.role.j2.yaml
index e9686564b0..5e1bab8947 100644
--- a/network/config/multiple-nics/role.role.j2.yaml
+++ b/network/config/multiple-nics/role.role.j2.yaml
@@ -42,8 +42,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 {%- if role.name == 'ComputeOvsDpdk' %}
   NumDpdkInterfaceRxQueues:
diff --git a/network/config/single-nic-linux-bridge-vlans/controller-v6.j2.yaml b/network/config/single-nic-linux-bridge-vlans/controller-v6.j2.yaml
index d5a398f42f..f8dae080dc 100644
--- a/network/config/single-nic-linux-bridge-vlans/controller-v6.j2.yaml
+++ b/network/config/single-nic-linux-bridge-vlans/controller-v6.j2.yaml
@@ -43,8 +43,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/network/config/single-nic-linux-bridge-vlans/role.role.j2.yaml b/network/config/single-nic-linux-bridge-vlans/role.role.j2.yaml
index e2a1b02fc4..326ce5ba21 100644
--- a/network/config/single-nic-linux-bridge-vlans/role.role.j2.yaml
+++ b/network/config/single-nic-linux-bridge-vlans/role.role.j2.yaml
@@ -42,8 +42,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/network/config/single-nic-vlans/controller-no-external.j2.yaml b/network/config/single-nic-vlans/controller-no-external.j2.yaml
index e6ab1efb55..ade5e01073 100644
--- a/network/config/single-nic-vlans/controller-no-external.j2.yaml
+++ b/network/config/single-nic-vlans/controller-no-external.j2.yaml
@@ -46,8 +46,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/network/config/single-nic-vlans/controller-v6.j2.yaml b/network/config/single-nic-vlans/controller-v6.j2.yaml
index 4fc3c2fc1d..c7eee6f73d 100644
--- a/network/config/single-nic-vlans/controller-v6.j2.yaml
+++ b/network/config/single-nic-vlans/controller-v6.j2.yaml
@@ -43,8 +43,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/network/config/single-nic-vlans/role.role.j2.yaml b/network/config/single-nic-vlans/role.role.j2.yaml
index fd2ea24176..d5b30794b2 100644
--- a/network/config/single-nic-vlans/role.role.j2.yaml
+++ b/network/config/single-nic-vlans/role.role.j2.yaml
@@ -42,8 +42,10 @@ parameters:
     default: []
     description: A list of DNS servers (2 max for some implementations) that will be added to resolv.conf.
     type: comma_delimited_list
-  EC2MetadataIp: # Override this via parameter_defaults
-    description: The IP address of the EC2 metadata server.
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
     type: string
 resources:
   OsNetConfigImpl:
diff --git a/puppet/role.role.j2.yaml b/puppet/role.role.j2.yaml
index 661bbbe6b8..6f770ba8be 100644
--- a/puppet/role.role.j2.yaml
+++ b/puppet/role.role.j2.yaml
@@ -255,6 +255,11 @@ parameters:
     description: The default route of the control plane network. (The parameter
       is automatically resolved from the ctlplane subnet's gateway_ip attribute.)
     type: string
+  EC2MetadataIp:
+    default: ''
+    description: The IP address of the EC2 metadata server. (The parameter
+      is automatically resolved from the ctlplane subnet's host_routes attribute.)
+    type: string
 
   ServerDeletionPolicy:
     description: Whether to retain or delete servers on deletion of the stack
@@ -332,6 +337,9 @@ conditions:
   ctlplane_default_route_set:
     not:
       equals: [{get_param: ControlPlaneDefaultRoute}, '']
+  ec2metadaip_set:
+    not:
+      equals: [{get_param: EC2MetadataIp}, '']
 
 resources:
   {{server_resource_name}}:
@@ -462,6 +470,16 @@ resources:
           - ctlplane_default_route_set
           - {get_param: ControlPlaneDefaultRoute}
           - {get_attr: [{{server_resource_name}}, addresses, ctlplane, 0, subnets, 0, gateway_ip]}
+      EC2MetadataIp:
+        if:
+          - ec2metadaip_set
+          - {get_param: EC2MetadataIp}
+          - yaql:
+              # NOTE(hjensas): DeployedServer sets the EC2MetadataIp in DeployedServerEnvironment.
+              # Return an empty string if there are no host_routes in the subnet. (Standalone and Container Undercloud)
+              expression: switch($.data = null => '', $.data != null => $.data.where($.destination = "169.254.169.254/32").nexthop.first())
+              data: {get_attr: [{{server_resource_name}}, addresses, ctlplane, 0, subnets, 0, host_routes]}
+
   {%- for network in networks %}
       {{network.name}}IpSubnet: {get_attr: [{{network.name}}Port, ip_subnet]}
   {%- endfor %}
diff --git a/releasenotes/notes/use-subnet-attrs-ec2metadataip-aa28f3c030f13c9c.yaml b/releasenotes/notes/use-subnet-attrs-ec2metadataip-aa28f3c030f13c9c.yaml
new file mode 100644
index 0000000000..0abe2b8324
--- /dev/null
+++ b/releasenotes/notes/use-subnet-attrs-ec2metadataip-aa28f3c030f13c9c.yaml
@@ -0,0 +1,25 @@
+---
+features:
+  - |
+    It is no longer a requirement to provide the parameter: ``EC2MetadataIp``
+    in the environment when deploying. Now ``get_attr`` on the server resource
+    is used to resolve the value from the ``ctlplane`` subnet's ``host_routes``
+    attribute.. A conditional is used to determine if the user provided the
+    parameter in the environment. If the user provided the parameter, the user
+    provided value is used.
+upgrade:
+  - |
+    Since the the ``EC2MetadataIp`` can now be resolved from the ``ctlplane``
+    subnet(s) this parameter can be removed from the environment
+    (``network-environment.yaml``).
+
+    .. Note:: Prior to removing the parameter ensure that the property of
+              the ``ctlplane`` subnet(s) is correct. In case it is not, update
+              ``undercloud.conf`` with the correct configuration and re-run the
+              ``openstack undercloud install`` command to ensure the property
+              is set correctly.
+
+    .. Note:: ``EC2MetadataIp`` is now passed to the network config template
+              when the resource is created. Because of this the parameter must
+              be defined in the network config template, even if it is not
+              used.