Get public facing IP of remote machine (Ansible)

On occasion, you might need to identify the IP address of the machine being ansibled. However, there are various scenarios (AWS instances, alicloud instances, NAT etc) where the public facing IP of your machine may not necessarily be bound directly to an interface on the system you're running ansible against.

This simple role places a call out to a Web based endpoint and sets a fact based upon the response so that you can use it later (for example to whitelist it in firewall rules on another machine).

Details

Snippet

- name: Get System Public IP
  uri:
      url: http://checkip.amazonaws.com/
      return_content: yes
  register: ip_lookup
- set_fact:
      system_ip: "{{ ip_lookup.content | regex_replace('\n','') }}"