Skip to content

Hostname Ansible Role

An Ansible role to manage the system hostname and a basic /etc/hosts mapping on Debian, Ubuntu, and Fedora systems.

Features

  • Persist the short hostname to /etc/hostname
  • Optionally ensure a dedicated loopback mapping for FQDN and short hostname in /etc/hosts
  • Works across Debian, Ubuntu, Fedora, and containerized test environments

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 written to /etc/hostname
hostname_fqdn null string Optional fully qualified domain name used before the short hostname in /etc/hosts
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 used for the managed hostname mapping

Testing

Run Molecule against a specific platform:

molecule test -s default -p debian12

Supported Molecule platforms in this role:

  • debian12
  • debian13
  • ubuntu2204
  • ubuntu2404
  • fedora43
  • fedora44

Example Playbooks

Basic

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

Custom

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