ansible: set up hostnames based on mac address
authorKristof Provost <kp@FreeBSD.org>
Thu, 4 May 2023 20:55:49 +0000 (22:55 +0200)
committerKristof Provost <kp@FreeBSD.org>
Thu, 4 May 2023 21:25:41 +0000 (23:25 +0200)
ansible/playbook.yaml
ansible/scripts/set_hostname [new file with mode: 0755]

index 472fe4e..5983ac9 100644 (file)
@@ -1,3 +1,10 @@
+- name: Set hostname
+  hosts: localhost
+  vars:
+  tasks:
+    - name: set system hostname
+      ansible.builtin.script: scripts/set_hostname
+
 - name: test playbook
   hosts: domoticamachines
   tasks:
diff --git a/ansible/scripts/set_hostname b/ansible/scripts/set_hostname
new file mode 100755 (executable)
index 0000000..8500549
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+find_if()
+{
+       if ifconfig em0 1>/dev/null 2>/dev/null;
+       then
+               echo "em0"
+               return
+       fi
+
+       if ifconfig igb0 1>/dev/null 2>/dev/null;
+       then
+               echo "igb0"
+               return
+       fi
+
+       echo "lo0"
+}
+
+host_id()
+{
+       ifconfig `find_if` | awk '/ether/ { printf($2); }'
+}
+
+find_hostname()
+{
+       if [ "$(host_id)" == "52:54:00:12:34:56" ];
+       then
+               echo "qemu"
+               return
+       fi
+
+       echo "unknown"
+}
+
+sysrc hostname="$(find_hostname)"
+/etc/rc.d/hostname start