From be31fead2fdf7f5aaeaa07299d7334c7c1169c6d Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Wed, 24 Feb 2016 00:52:16 +0100 Subject: [PATCH] Create set_hostname role and playbook Turns out we have had many issues with random servers having wrong hostname and /etc/hosts info. This playbook/role allows to configure that by passing -e "target=" as ansible-playbook parameter. Change-Id: I73939ebc65211a840bb41370c22b111112389716 --- playbooks/roles/set_hostname/tasks/main.yml | 13 +++++++++++++ playbooks/roles/set_hostname/templates/hosts.j2 | 2 ++ playbooks/roles/set_hostname/templates/mailname.j2 | 1 + playbooks/set_hostnames.yml | 7 +++++++ 4 files changed, 23 insertions(+) create mode 100644 playbooks/roles/set_hostname/tasks/main.yml create mode 100644 playbooks/roles/set_hostname/templates/hosts.j2 create mode 100644 playbooks/roles/set_hostname/templates/mailname.j2 create mode 100644 playbooks/set_hostnames.yml diff --git a/playbooks/roles/set_hostname/tasks/main.yml b/playbooks/roles/set_hostname/tasks/main.yml new file mode 100644 index 0000000000..e1add7d4cf --- /dev/null +++ b/playbooks/roles/set_hostname/tasks/main.yml @@ -0,0 +1,13 @@ +--- +# Set hostname and /etc/hosts +# Inspired by: +# https://github.com/ansible/ansible/pull/8482) +# https://gist.github.com/rothgar/8793800 +- name: Set /etc/hostname + hostname: name="{{ inventory_hostname.split('.', 1)[0] }}" + +- name: Set /etc/hosts + template: src=hosts.j2 dest=/etc/hosts mode=0644 + +- name: Set /etc/mailname + template: src=mailname.j2 dest=/etc/mailname mode=0644 diff --git a/playbooks/roles/set_hostname/templates/hosts.j2 b/playbooks/roles/set_hostname/templates/hosts.j2 new file mode 100644 index 0000000000..1c3937709d --- /dev/null +++ b/playbooks/roles/set_hostname/templates/hosts.j2 @@ -0,0 +1,2 @@ +127.0.0.1 localhost +127.0.1.1 {{ inventory_hostname }} {{ inventory_hostname.split('.', 1)[0] }} diff --git a/playbooks/roles/set_hostname/templates/mailname.j2 b/playbooks/roles/set_hostname/templates/mailname.j2 new file mode 100644 index 0000000000..b7d75c04ca --- /dev/null +++ b/playbooks/roles/set_hostname/templates/mailname.j2 @@ -0,0 +1 @@ +{{ inventory_hostname.split('.', 1)[0] }} diff --git a/playbooks/set_hostnames.yml b/playbooks/set_hostnames.yml new file mode 100644 index 0000000000..25dbbe814b --- /dev/null +++ b/playbooks/set_hostnames.yml @@ -0,0 +1,7 @@ +--- +# file: set_hostnames.yml +- hosts: "{{ target }}" + gather_facts: no + user: root + roles: + - { role: set_hostname }