fix: hostname and domain setup

This commit is contained in:
mrjk 2025-07-10 01:59:50 -04:00
parent a5e773a007
commit 55ba4275ee
2 changed files with 26 additions and 0 deletions

View File

@ -9,3 +9,8 @@ system_packages__default: []
system_accounts: "{{ vars | dict2items | selectattr('key', 'match', '^system_accounts__.*') | map(attribute='value') | list | flatten | unique }}" system_accounts: "{{ vars | dict2items | selectattr('key', 'match', '^system_accounts__.*') | map(attribute='value') | list | flatten | unique }}"
system_packages: "{{ vars | dict2items | selectattr('key', 'match', '^system_packages__.*') | map(attribute='value') | list | flatten | unique }}" system_packages: "{{ vars | dict2items | selectattr('key', 'match', '^system_packages__.*') | map(attribute='value') | list | flatten | unique }}"
# system_hostname: "{{ ansible_hostname }}"
system_hostname: "{{ inventory_hostname | split('.') | first }}"
system_domain: "{{ (inventory_hostname | split('.'))[1:] | join('.') }}"
# system_domain: "lan.test"
system_fqdn: "{{ system_hostname }}.{{ system_domain }}"

View File

@ -8,7 +8,11 @@
role_config: role_config:
system_accounts: "{{ system_accounts }}" system_accounts: "{{ system_accounts }}"
system_packages: "{{ system_packages }}" system_packages: "{{ system_packages }}"
system_hostname: "{{ system_hostname }}"
system_domain: "{{ system_domain }}"
system_fqdn: "{{ system_fqdn }}"
tasks: tasks:
- "Configure system hostname, domain and fqdn"
- Ensure system security groups are present - Ensure system security groups are present
- Create system users from system_accounts - Create system users from system_accounts
- Ensure ssh keys are correctly deployed - Ensure ssh keys are correctly deployed
@ -18,6 +22,23 @@
tags: tags:
- config_show - config_show
# Configure system fqdn
# ==========================
- name: Configure system hostname in /etc/hosts
ansible.builtin.lineinfile:
path: /etc/hosts
regexp: '^127\.0\.1\.1 '
line: '127.0.1.1 {{ system_fqdn }} {{ system_hostname }}'
state: present
register: machine_name_changed
- name: "Apply system hostname (changed={{ machine_name_changed.changed }})"
ansible.builtin.shell:
cmd: "test '{{ ansible_check_mode|lower }}' == true || hostnamectl set-hostname {{ system_fqdn }}"
when: machine_name_changed.changed
changed_when: true
check_mode: false
# Configure groups and users # Configure groups and users
# ========================== # ==========================