From b4d66c57618f85118e9fd58927dbcc0d526f7123 Mon Sep 17 00:00:00 2001 From: Joshua Fernandes Date: Tue, 28 May 2024 14:44:01 +1000 Subject: [PATCH] use ifconfig.me to get the public IP regardless of cloud provider --- README.md | 1 - defaults/main.yml | 1 - tasks/network.yml | 66 +++++++---------------------------------------- 3 files changed, 9 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 1e0b056..06e8a82 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,6 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults | `besu_systemd_state` | restarted | The default option for the systemd service state | | `besu_identity` | ___unset___ | Configuration of Identity in the Client ID | | `besu_host_ip` | "" | The host IP that Besu uses for the P2P network. This specifies the host on which P2P listens | -| `besu_default_ip` | "{{ default(ansible_host) \| default('127.0.0.1') }}" | The fallback default for `besu_host_ip` | | `besu_max_peers` | ___unset___ | The maximum number of P2P connections you can establish | | `besu_network` | mainnet | The network that this node will join. Other values are 'ropsten', 'rinkeby', 'goerli', 'classic', 'mordor', 'kotti', 'dev' and 'custom' | | `besu_genesis_path` | ___unset___ | The path to the genesis file, only valid when `besu_network` is `custom` | diff --git a/defaults/main.yml b/defaults/main.yml index 56f19cb..c157693 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -36,7 +36,6 @@ besu_launchd_dir: /Library/LaunchAgents # Besu config besu_host_ip: "" -besu_default_ip: "127.0.0.1" besu_network: mainnet besu_sync_mode: FAST besu_p2p_port: 30303 diff --git a/tasks/network.yml b/tasks/network.yml index b225a5b..447a664 100644 --- a/tasks/network.yml +++ b/tasks/network.yml @@ -1,69 +1,21 @@ --- -- name: Get the default ipv4 address +- name: Get the default ipv4 address - and set this as the default fallback set_fact: - besu_default_ip: >- + besu_host_ip: >- {{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] if hostvars[inventory_hostname]['ansible_default_ipv4']['address'] is defined else '127.0.0.1' }} - besu_aws_public_ip: "" - besu_gcp_public_ip: "" - besu_azure_public_ip: "" - -- name: Check if running on AWS - uri: - url: http://169.254.169.254/latest/meta-data/public-ipv4 - method: GET - return_content: yes - status_code: 200 - register: aws_public_ip_output - ignore_errors: True - -- name: Set the host ip if we are in AWS - set_fact: - besu_host_ip: "{{ aws_public_ip_output.content }}" - when: aws_public_ip_output.status == 200 - -- name: Check if running on Azure + +- name: Fetch public IP uri: - url: http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2017-08-01&format=text + url: http://ifconfig.me/ip method: GET - headers: - Metadata: true return_content: yes status_code: 200 - register: azure_public_ip_output + register: besu_public_ip_output ignore_errors: True - when: ( aws_public_ip_output.status != 200 ) - -- name: Set the host ip if we are in Azure - set_fact: - besu_host_ip: "{{ azure_public_ip_output.content }}" - when: ( aws_public_ip_output.status != 200 ) and - ( azure_public_ip_output.status == 200 ) - -- name: Check if running on GCP - uri: - url: http://169.254.169.254/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip - method: GET - headers: - Metadata-Flavor: Google - return_content: yes - status_code: 200 - register: gcp_public_ip_output - ignore_errors: True - when: ( aws_public_ip_output.status != 200 ) and - ( azure_public_ip_output.status != 200 ) - -- name: Set the host ip if we are in GCP - set_fact: - besu_host_ip: "{{ gcp_public_ip_output.content }}" - when: ( aws_public_ip_output.status != 200 ) and - ( azure_public_ip_output.status != 200 ) and - ( gcp_public_ip_output == 200 ) -- name: Fallback to the ansible default ip +- name: Set the host ip set_fact: - besu_host_ip: "{{ besu_default_ip }}" - when: ( aws_public_ip_output.status != 200 ) and - ( azure_public_ip_output.status != 200 ) and - ( gcp_public_ip_output != 200 ) + besu_host_ip: "{{ besu_public_ip_output.content }}" + when: besu_public_ip_output.status == 200