An Ansible role to set the fully-qualified domain name of a host

Manual Process

Simply edit the /etc/hosts file and add and entry of 127.0.1.1 [hostname full] [hostname short].

Ansible role: host-fqdn

This only exists as a role because Ansible runs roles first and then tasks and I need to set the fully hostname before some of my other roles run.

At some point I'll add the option to use a real IP address instead of 127.0.1.1 to keep applications like Samba happy.

File structure

host-fqdn

   meta

      main.yaml

   tasks

      main.yaml

meta/main.yaml

--- # Meta for role host-fqdn
galaxy_info:
  #role_name: host-fqdn
  author: Richard Neighbour
  description: Sets the fully-qualified hostname
  license: GPLv3
  min_ansible_version: "2.9"

  platforms:
  - name: Ubuntu
    versions:
    - all

  galaxy_tags: []

dependencies: []

...

 

tasks/main.yaml

--- # Ansible Role Tasks
# Ubuntu 20.04 LTS (and likely others)
# Set the FQDN, taken from inventory.
# 

- name: Set FQDN - taken from inventory
  become: true
  ansible.builtin.lineinfile:
    path: /etc/hosts
    regexp: '^127\.0\.1\.1'
    line: 127.0.1.1 {{ inventory_hostname }} {{ inventory_hostname_short }}

...