diff --git a/README.md b/README.md index 0093e35..13b6c7c 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ Following roles have been defined to make creating a server configuration easy: - **certificate** issues a given certificate - **bootstrap** defines a way to connect to a server which has not been configured yet - **common** defines the installation of common packages and common configurations like firewall +- **hostname** configures the hostname for a given host diff --git a/roles/hostname/defaults/main.yml b/roles/hostname/defaults/main.yml new file mode 100644 index 0000000..76f3936 --- /dev/null +++ b/roles/hostname/defaults/main.yml @@ -0,0 +1,3 @@ +--- + +hostname: "{{ fqdn | regex_replace('^([^\\.]*)\\..*$', '\\1') }}" diff --git a/roles/hostname/handlers/main.yml b/roles/hostname/handlers/main.yml new file mode 100644 index 0000000..8cb7c98 --- /dev/null +++ b/roles/hostname/handlers/main.yml @@ -0,0 +1,6 @@ +--- + +- name: restart systemd-hostnamed + systemd: + name: systemd-hostnamed + state: restarted diff --git a/roles/hostname/tasks/main.yml b/roles/hostname/tasks/main.yml new file mode 100644 index 0000000..eb57cc9 --- /dev/null +++ b/roles/hostname/tasks/main.yml @@ -0,0 +1,23 @@ +--- + +- name: Set local hostname + copy: + content: "{{ hostname }}" + dest: /etc/hostname + notify: restart systemd-hostnamed + +- name: Set hostname and fqdn as alias for ipv4 + lineinfile: + path: /etc/hosts + regexp: '^127\.0\.0\.1\s' + line: "127.0.0.1 {{ fqdn }} {{ hostname }} localhost *.localhost" + notify: restart systemd-hostnamed + +- name: Set hostname and fqdn as alias for ipv6 + lineinfile: + path: /etc/hosts + regexp: '^::1\s' + line: "::1 {{ fqdn }} {{ hostname }} localhost *.localhost ip6-localhost ip6-loopback" + notify: restart systemd-hostnamed + +- meta: flush_handlers