From: Kristof Provost Date: Thu, 4 May 2023 20:55:49 +0000 (+0200) Subject: ansible: set up hostnames based on mac address X-Git-Url: https://git.sigsegv.be/?a=commitdiff_plain;h=ed8151bb5e8440160d0981c15c903e56c8b22e85;p=pennestraat-domotica ansible: set up hostnames based on mac address --- diff --git a/ansible/playbook.yaml b/ansible/playbook.yaml index 472fe4e..5983ac9 100644 --- a/ansible/playbook.yaml +++ b/ansible/playbook.yaml @@ -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 index 0000000..8500549 --- /dev/null +++ b/ansible/scripts/set_hostname @@ -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