Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
profile: production

exclude_paths:
- .cache/
- .github/
- molecule/

offline: false

supported_ansible_also:
- ">=2.12"

verbosity: 1

warn_list:
- experimental
- yaml[truthy]
- yaml[line-length]
- role-name
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
name: Molecule CI

'on':
pull_request:
branches:
- "master"
- "main"
push:
branches:
- "master"
- "main"
workflow_dispatch:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.13'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Run lint
run: make lint

test:
name: Test (${{ matrix.os }}-${{ matrix.version }})
runs-on: ubuntu-latest
needs: lint
strategy:
fail-fast: false
matrix:
include:
- os: debian
version: '12'
image: geerlingguy/docker-debian12-ansible:latest
- os: ubuntu
version: '2004'
image: geerlingguy/docker-ubuntu2004-ansible:latest
- os: ubuntu
version: '2204'
image: geerlingguy/docker-ubuntu2204-ansible:latest
- os: ubuntu
version: '2404'
image: geerlingguy/docker-ubuntu2404-ansible:latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.13'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Run molecule test
run: molecule test
env:
MOLECULE_OS: ${{ matrix.os }}
MOLECULE_VERSION: ${{ matrix.version }}
MOLECULE_IMAGE: ${{ matrix.image }}
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ Icon
._*
.Spotlight-V100
.Trashes
.vagrant
test
molecule/default/roles

__pycache__
.ansible/
.venv/
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

17 changes: 17 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
extends: default
rules:
line-length:
max: 120
level: warning
comments:
min-spaces-from-content: 1
comments-indentation: false
braces:
max-spaces-inside: 1
octal-values:
forbid-implicit-octal: true
forbid-explicit-octal: true

ignore: |
**/.#*
45 changes: 45 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.PHONY: lint test test-debian12 test-ubuntu2004 test-ubuntu2204 test-ubuntu2404 test-all clean distclean

VENV := .venv
BIN := $(VENV)/bin
export PATH := $(CURDIR)/$(BIN):$(PATH)

$(VENV): requirements-dev.txt
python3 -m venv $(VENV)
$(BIN)/pip install --upgrade pip
$(BIN)/pip install -r requirements-dev.txt
@touch $(VENV)

lint: $(VENV)
$(BIN)/yamllint -c .yamllint defaults tasks vars meta
$(BIN)/ansible-lint -c .ansible-lint defaults tasks vars meta

test: lint test-all

test-debian12: $(VENV)
MOLECULE_OS=debian MOLECULE_VERSION=12 \
MOLECULE_IMAGE=geerlingguy/docker-debian12-ansible:latest \
$(BIN)/molecule test

test-ubuntu2004: $(VENV)
MOLECULE_OS=ubuntu MOLECULE_VERSION=2004 \
MOLECULE_IMAGE=geerlingguy/docker-ubuntu2004-ansible:latest \
$(BIN)/molecule test

test-ubuntu2204: $(VENV)
MOLECULE_OS=ubuntu MOLECULE_VERSION=2204 \
MOLECULE_IMAGE=geerlingguy/docker-ubuntu2204-ansible:latest \
$(BIN)/molecule test

test-ubuntu2404: $(VENV)
MOLECULE_OS=ubuntu MOLECULE_VERSION=2404 \
MOLECULE_IMAGE=geerlingguy/docker-ubuntu2404-ansible:latest \
$(BIN)/molecule test

test-all: test-debian12 test-ubuntu2004 test-ubuntu2204 test-ubuntu2404

clean:
$(BIN)/molecule destroy 2>/dev/null || true

distclean: clean
rm -rf $(VENV)
53 changes: 21 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
## ANXS - hostname [![Build Status](https://travis-ci.com/ANXS/hostname.png)](https://travis-ci.com/ANXS/hostname)
## [ANXS](https://github.com/ANXS) - hostname

Ansible role that sets the hostname and FQDN of the node.
[![CI Status](https://img.shields.io/github/actions/workflow/status/anxs/hostname/ci.yml)](https://github.com/ANXS/hostname/actions/workflows/ci.yml)
[![Maintenance](https://img.shields.io/maintenance/yes/2026.svg)](https://github.com/ANXS/hostname)
[![Ansible Role](https://img.shields.io/ansible/role/d/anxs/hostname)](https://galaxy.ansible.com/ui/standalone/roles/ANXS/hostname/)
[![License](https://img.shields.io/github/license/ANXS/hostname)](https://github.com/ANXS/hostname/blob/master/LICENSE)

Ansible role that sets the hostname and FQDN of a node based on its inventory name, keeping `/etc/hostname` and `/etc/hosts` in sync for IPv4 and IPv6.

#### Variables
## Requirements & Dependencies

This depends on your ansible hosts inventory.
* Ansible 2.12 or higher.
* Ubuntu 20.04+ or Debian 12+.
* Depends on `ANXS.apt` (installed via Galaxy).

Add the hosts to your inventory with their FQDN (e.g. foo.bar.com), and the role will take care of setting your hostname accordingly (hostname: foo, FQDN: foo.bar.com).
## Usage

If you just name it with the hostname in the inventory, it will similarly work (hostname set, but no FQDN attached to it).
Add hosts to your inventory with their FQDN (e.g. `foo.bar.com`), and the role will set the short hostname and FQDN accordingly (hostname: `foo`, FQDN: `foo.bar.com`). A bare short name in inventory works too — hostname is set without an attached FQDN.

```yaml
hostname_avahi: no # You may optionall install avahi-mdns and libnss-mdns. This is useful in vagrant.
```
## Variables

#### Example
Some commonly adjusted variables. See [`defaults/main.yml`](https://github.com/ANXS/hostname/blob/master/defaults/main.yml) for the full set.

Your inventory file should look like this:
* `hostname_avahi` (default `false`) installs `avahi-daemon` and `libnss-mdns` for mDNS-based name resolution. Useful for Vagrant and LAN discovery.

```yaml
foo.bar.com ansible_ssh_host=xxx.xxx.xxx.xxx ansible_ssh_port=22
baz.bar.com ansible_ssh_host=xxx.xxx.xxx.xxx ansible_ssh_port=22
```
## Testing

And the structure of the files in your host_vars folders should match accordingly:
Tests use [Molecule](https://github.com/ansible/molecule) with Docker and [Testinfra](https://testinfra.readthedocs.io/). Run the full suite with `make test`, or target a specific platform (e.g. `make test-ubuntu2404`).

```
- host_vars
|- foo.bar.com
|- baz.bar.com
```
The test suite verifies hostname and `/etc/hostname` are set from inventory, `/etc/hosts` carries correct IPv4 and IPv6 entries, and the legacy `127.0.1.1` Raspberry Pi override is removed when present. Tests run across Ubuntu 20.04/22.04/24.04 and Debian 12.

## Note on AI Usage

#### Testing
This project comes with a VagrantFile, this is a fast and easy way to test changes to the role, fire it up with `vagrant up`
This project has been developed with AI assistance. Contributions making use of AI generated content are welcome, however they _must_ be human reviewed prior to submission as pull requests, or issues. All contributors must be able to fully explain and defend any AI generated code, documentation, issues, or tests they submit. Contributions making use of AI must have this explicitly declared in the pull request or issue. This also applies to utilization of AI for reviewing of pull requests.

See [vagrant docs](https://docs.vagrantup.com/v2/) for getting setup with vagrant


#### License

Licensed under the MIT License. See the LICENSE file for details.


#### Feedback, bug-reports, requests, ...
## Feedback, bug-reports, requests, ...

Are [welcome](https://github.com/ANXS/hostname/issues)!
20 changes: 0 additions & 20 deletions Vagrantfile

This file was deleted.

2 changes: 1 addition & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---

hostname_avahi: no
hostname_avahi: false
27 changes: 18 additions & 9 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
---
galaxy_info:
author: pjan vandaele
company: ANXS
description: sets the hostname and FQDN
min_ansible_version: 2.4.2.0
license: MIT
role_name: "hostname"
namespace: "anxs"
author: "ANXS Contributors"
company: "ANXS"
description: "sets the hostname and FQDN"
min_ansible_version: "2.12"
license: "MIT"
platforms:
- name: Ubuntu
versions:
- all
categories:
- focal
- jammy
- noble
- name: Debian
versions:
- bookworm
galaxy_tags:
- system
- hostname
- networking

dependencies:
- role: ANXS.apt
dependencies:
- role: "ANXS.apt"
7 changes: 7 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Converge
hosts: all
become: true
gather_facts: true
roles:
- role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
41 changes: 41 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
dependency:
name: galaxy

driver:
name: docker

platforms:
- name: "anxs-hostname-${MOLECULE_OS:-ubuntu}-${MOLECULE_VERSION:-2404}"
image: "${MOLECULE_IMAGE:-geerlingguy/docker-ubuntu2404-ansible:latest}"
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
privileged: true
pre_build_image: true
cgroupns_mode: host
command: ${MOLECULE_DOCKER_COMMAND:-""}

provisioner:
name: ansible
config_options:
defaults:
remote_tmp: /tmp/.ansible-${USER}/tmp/
env:
ANSIBLE_ROLES_PATH: "${MOLECULE_PROJECT_DIRECTORY}/..:${HOME}/.ansible/roles"

verifier:
name: testinfra

scenario:
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy
6 changes: 6 additions & 0 deletions molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- hosts: all
tasks:
- name: "early apt-get update"
apt:
update_cache: true
9 changes: 9 additions & 0 deletions molecule/default/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# Temporary pin to GitHub master until ANXS Galaxy namespace case-sensitivity
# issue is resolved and a new ANXS.apt release can be published. The Galaxy
# v2.1.0 tarball has a jinja list-concatenation bug that stringifies the
# package list and crashes hostname's converge.
# See: memory://planning/anxs-galaxy-and-branch-migration
- src: https://github.com/ANXS/apt
name: ANXS.apt
version: master
Loading
Loading