create the library directory and the ansible module
mkdir library ; cat > library/get_ip_default_gw.sh <<'EOF'
#!/bin/bash
# get ip connected to default gateway
ip=$( ip route | awk '/^default/ { print $5 }' | xargs ip -4 address show | awk '/inet / { print $2 }' | cut -d/ -f1 )
test -s $ip && echo "{ \"failed\": true, \"erro\": \"IP not found! }" || echo "{ \"ip\": \"$ip\" }"
EOF
test module using ansible ad hoc command
ANSIBLE_LIBRARY=./library ansible localhost -m get_ip_default_gw
test module using ansible playbook
ANSIBLE_LIBRARY=./library ansible-playbook -c local /dev/stdin <<EOF
---
- hosts: localhost
gather_facts: no
tasks:
- name: 'get ip connected to default gateway'
get_ip_default_gw:
register: result
- debug:
var: result.ip
EOF