From ed8151bb5e8440160d0981c15c903e56c8b22e85 Mon Sep 17 00:00:00 2001 From: Kristof Provost Date: Thu, 4 May 2023 22:55:49 +0200 Subject: [PATCH] ansible: set up hostnames based on mac address --- ansible/playbook.yaml | 7 +++++++ ansible/scripts/set_hostname | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 ansible/scripts/set_hostname 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 -- 2.51.0