From 2fcc88b7537d44b2d91887977baad0ca1ba242e9 Mon Sep 17 00:00:00 2001 From: Andrew Schoen Date: Tue, 14 Apr 2015 09:09:06 -0500 Subject: [PATCH] Setup static IPs for baremetal machines running ubuntu. Signed-off-by: Andrew Schoen --- roles/testnode/files/scripts/static-ip.sh | 66 +++++++++++++++++++++++ roles/testnode/tasks/setup-ubuntu.yml | 4 ++ roles/testnode/tasks/static_ip.yml | 12 +++++ 3 files changed, 82 insertions(+) create mode 100644 roles/testnode/files/scripts/static-ip.sh create mode 100644 roles/testnode/tasks/static_ip.yml diff --git a/roles/testnode/files/scripts/static-ip.sh b/roles/testnode/files/scripts/static-ip.sh new file mode 100644 index 0000000..c4cfa41 --- /dev/null +++ b/roles/testnode/files/scripts/static-ip.sh @@ -0,0 +1,66 @@ +#!/bin/bash +## {{ ansible_managed }} + +dontrun=$(grep -ic inet\ static /etc/network/interfaces) +if [ $dontrun -eq 0 ] +then +cidr=$(ip addr show dev eth0 | grep -iw inet | awk '{print $2}') +ip=$(echo $cidr | cut -d'/' -f1) +miracheck=$(uname -n | grep -ic mira) +armcheck=$(uname -m | grep -ic arm) +netmask=$(ipcalc $cidr | grep -i netmask | awk '{print $2}') +gateway=$(route -n | grep ^0.0 | awk '{print $2}') +broadcast=$(ipcalc $cidr | grep -i broad | awk '{print $2}') +octet1=$(echo $ip | cut -d'.' -f1) +octet2=$(echo $ip | cut -d'.' -f2) +octet3=$(echo $ip | cut -d'.' -f3) +octet4=$(echo $ip | cut -d'.' -f4) +octet3=$(($octet3 + 13)) +if [ $armcheck -gt 0 ] +then +dev=eth1 +else +dev=eth2 +fi +if [ $miracheck -gt 0 ] +then +sed -i "s/iface eth0 inet dhcp/\ +iface eth0 inet static\n\ + address $ip\n\ + netmask $netmask\n\ + gateway $gateway\n\ + broadcast $broadcast\n\ +\n\ +/g" /etc/network/interfaces +else +typicacheck=$(uname -n | grep -ic typica) +if [ $typicacheck -gt 0 ] +then +sed -i "s/iface eth0 inet dhcp/\ +iface eth0 inet static\n\ + address $ip\n\ + netmask $netmask\n\ + gateway $gateway\n\ + broadcast $broadcast\n\ + up route add -net 10.99.118.0\/24 gw 172.20.133.1 dev eth0\n\ + up route add -net 10.214.128.0\/20 gw 172.20.133.1 dev eth0\n\ + up route add -net 10.214.0.0\/20 gw 172.20.133.1 dev eth0\n\ +\n\ +/g" /etc/network/interfaces +else +sed -i "s/iface eth0 inet dhcp/\ +iface eth0 inet static\n\ + address $ip\n\ + netmask $netmask\n\ + gateway $gateway\n\ + broadcast $broadcast\n\ +\n\ +auto $dev\n\ +iface $dev inet static\n\ + address $octet1.$octet2.$octet3.$octet4\n\ + netmask $netmask\ +/g" /etc/network/interfaces +fi +fi +fi +touch /static-ip-setup diff --git a/roles/testnode/tasks/setup-ubuntu.yml b/roles/testnode/tasks/setup-ubuntu.yml index 8a4f7e5..4b64db4 100644 --- a/roles/testnode/tasks/setup-ubuntu.yml +++ b/roles/testnode/tasks/setup-ubuntu.yml @@ -116,3 +116,7 @@ - include: nfs.yml tags: - nfs + +# is not run on virtual machines +- include: static_ip.yml + when: "'vps' not in group_names" diff --git a/roles/testnode/tasks/static_ip.yml b/roles/testnode/tasks/static_ip.yml new file mode 100644 index 0000000..2876055 --- /dev/null +++ b/roles/testnode/tasks/static_ip.yml @@ -0,0 +1,12 @@ +--- +- name: Set up static IP in /etc/hosts. + lineinfile: + dest: /etc/hosts + line: "{{ ansible_default_ipv4['address'] }} {{ ansible_fqdn }} {{ ansible_hostname }}" + regexp: "^127.0.1.1" + backrefs: yes + state: present + +# this script was ported directly from chef. +- name: Set up static IP and 10gig interface. + script: scripts/static-ip.sh creates=/static-ip-setup -- 2.39.5