Created
April 1, 2019 15:42
-
-
Save WhittlesJr/96fc4bf13804b07854e62f23eb5cc487 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; My IP address data is shaped like this: | |
(def data {"host-a" {:ip "1.1.1.1"} | |
"host-b" {:ip "2.2.2.2"} | |
"container" {"host-c" {:ip "3.3.3.3"} | |
"host-d" {:ip "4.4.4.4"}}}) | |
;; This logic turns that data into a map ("set" in nix) that would work for `networking.hosts`: | |
(defn build-hosts [ip-data & [name-prefix]] | |
(reduce-kv (fn [result hostname {:keys [ip] :as info}] | |
(let [full-hostname (if name-prefix | |
(str name-prefix "-" hostname) | |
hostname)] | |
(if ip | |
(assoc result ip [full-hostname]) | |
(merge result (build-hosts info hostname))))) | |
{} | |
ip-data)) | |
(build-hosts data) | |
=> {"1.1.1.1" ["host-a"], | |
"2.2.2.2" ["host-b"], | |
"3.3.3.3" ["container-host-c"], | |
"4.4.4.4" ["container-host-d"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment