From 79ed9e66603642c9b496f2efd4bc52c42383f46d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 7 Feb 2023 00:33:42 +0100 Subject: add vastly improved openwrt_setup module --- library/openwrt_setup.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/library/openwrt_setup.sh b/library/openwrt_setup.sh index 6f3730f8..a23a2c37 100644 --- a/library/openwrt_setup.sh +++ b/library/openwrt_setup.sh @@ -1,5 +1,70 @@ #!/bin/sh +## This is based on: https://github.com/gekmihesg/ansible-openwrt/blob/master/library/openwrt_setup.sh -echo '{}' + +. /usr/share/libubox/jshn.sh + + +add_ubus_fact() { + set -- ${1//!/ } + ubus list "$2" > /dev/null 2>&1 || return + local json="$($ubus call "$2" "$3" 2>/dev/null)" + echo -n "$seperator\"$1\":$json" + seperator="," +} + +main() { + ubus="/bin/ubus" + seperator="," + echo '{"changed":false,"ansible_facts":' + dist="OpenWRT" + dist_version="NA" + dist_release="NA" + test -f /etc/openwrt_release && { + . /etc/openwrt_release + dist="${DISTRIB_ID:-$dist}" + dist_version="${DISTRIB_RELEASE:-$dist_version}" + dist_release="${DISTRIB_CODENAME:-$dist_release}" + } || test ! -f /etc/os-release || { + . /etc/os-release + dist="${NAME:-$dist}" + dist_version="${VERSION_ID:-$dist_version}" + } + dist_major="${dist_version%%.*}" + json_set_namespace facts + json_init + json_add_string ansible_hostname "$(cat /proc/sys/kernel/hostname)" + json_add_string ansible_distribution "$dist" + json_add_string ansible_distribution_major_version "$dist_major" + json_add_string ansible_distribution_release "$dist_release" + json_add_string ansible_distribution_version "$dist_version" + json_add_string ansible_os_family OpenWRT + json_add_boolean ansible_is_chroot "$([ -r /proc/1/root/. ] && + { [ / -ef /proc/1/root/. ]; echo $?; } || + { [ "$(ls -di / | awk '{print $1}')" -eq 2 ]; echo $?; } + )" + dist_facts="$(json_dump)" + json_cleanup + json_set_namespace result + echo "${dist_facts%\}*}" + for fact in \ + info!system!info \ + devices!network.device!status \ + services!service!list \ + board!system!board \ + wireless!network.wireless!status \ + ; do + add_ubus_fact "openwrt_$fact" + done + echo "$seperator"'"openwrt_interfaces":{' + seperator="" + for net in $($ubus list); do + [ "${net#network.interface.}" = "$net" ] || + add_ubus_fact "${net##*.}!$net!status" + done + echo '}}}' +} + +main exit 0 -- cgit v1.2.3