Skip to content

Hostname Ansible Role

An Ansible role to manage the system hostname and a basic /etc/hosts mapping on Debian-based systems.

Features

  • Set static hostname (and optional pretty hostname)
  • Optionally ensure a 127.0.1.1 mapping for FQDN and short hostname in /etc/hosts
  • Works with systemd via hostnamectl (default)

Installation

roles:
  - name: hostname
    src: https://gitlab.com/niclas-zone/tools/ansible/roles/hostname.git
    version: 1.0.0
    scm: git

Role Variables

Variable Default Value Type Description
hostname {{ inventory_hostname_short }} string Short hostname to set
hostname_fqdn null string Fully qualified domain name (FQDN) to set as the static hostname
hostname_pretty null string Optional pretty hostname (e.g., descriptive name)
update_etc_hosts true boolean Whether to update /etc/hosts with a loopback mapping
etc_hosts_ipv4_loopback "127.0.1.1" string IPv4 loopback address for /etc/hosts mapping
hostname_use "systemd" string Method to set hostname (auto, systemd, file, all)

Example Playbooks

Basic

- hosts: servers
  become: true
  roles:
    - role: hostname

Custom

- hosts: servers
  become: true
  roles:
    - role: hostname
      vars:
        hostname_fqdn: "node01.example.com"
        hostname_pretty: "Node 01"