Sudoers config and hostname config working

This commit is contained in:
2026-09-09 16:50:19 -04:00
parent 18233ebf74
commit 7090ea5585
9 changed files with 184 additions and 19 deletions
@@ -0,0 +1,3 @@
# code: language=ansible
ipv4_only: true
+14 -5
View File
@@ -1,4 +1,4 @@
--- # code: language=ansible
- name: Configure ssh and firewall - name: Configure ssh and firewall
hosts: hosts:
@@ -7,11 +7,18 @@
become: true become: true
pre_tasks: pre_tasks:
- name: Set IP facts - name: Set IPv4 facts
become: false become: false
ansible.builtin.set_fact: ansible.builtin.set_fact:
ipv4_address: "{{ query('community.dns.lookup', inventory_hostname, type='A', nxdomain_handling = 'fail') | first }}" ipv4_address: "{{ query('community.dns.lookup', inventory_hostname, type='A', nxdomain_handling='fail') | first }}"
ipv6_address: "{{ query('community.dns.lookup', inventory_hostname, type='AAAA', nxdomain_handling = 'fail') | first }}"
- name: Set IPv6 facts
become: false
when:
- not (ipv4_only | default(false))
ansible.builtin.set_fact:
ipv6_address: "{{ query('community.dns.lookup', inventory_hostname, type='AAAA', nxdomain_handling='fail') | first }}"
- name: Install basic utilities - name: Install basic utilities
community.general.pacman: community.general.pacman:
@@ -22,4 +29,6 @@
update_cache: true update_cache: true
roles: roles:
- sshd # - hostname
- sudoers
# - sshd
+23
View File
@@ -0,0 +1,23 @@
# code: language=ansible
- name: Add IPv4 address to /etc/hosts
ansible.builtin.lineinfile:
path: /etc/hosts
line: "{{ ipv4_address }} {{ inventory_hostname }} {{ inventory_hostname_short }}"
- name: Ensure IPv4 loopback exists in /etc/hosts
ansible.builtin.lineinfile:
path: /etc/hosts
line: "127.0.1.1 {{ inventory_hostname }} {{ inventory_hostname_short }}"
- name: Add IPv6 address to /etc/hosts
when:
- not (ipv4_only | default(false))
ansible.builtin.lineinfile:
path: /etc/hosts
line: "{{ ipv6_address }} {{ inventory_hostname }} {{ inventory_hostname_short }}"
- name: Set hostname
ansible.builtin.hostname:
name: "{{ inventory_hostname }}"
use: systemd
+1
View File
@@ -0,0 +1 @@
# code: language=ansible
+2 -2
View File
@@ -1,4 +1,4 @@
#code: language=ansible # code: language=ansible
- name: Restart sshd - name: Restart sshd
ansible.builtin.systemd_service: ansible.builtin.systemd_service:
@@ -11,4 +11,4 @@
ansible.builtin.systemd_service: ansible.builtin.systemd_service:
name: systemd-networkd.service name: systemd-networkd.service
scope: system scope: system
state: restarted state: restarted
+43 -4
View File
@@ -1,4 +1,4 @@
#code: language=ansible # code: language=ansible
- name: Disable link local addressing - name: Disable link local addressing
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
@@ -26,7 +26,7 @@
[Link] [Link]
ActivationPolicy=always-up ActivationPolicy=always-up
RequiredForOnline=yes RequiredForOnline=yes
RequiredFamilyForOnline=both RequiredFamilyForOnline={{ sshd_required_family_for_online }}
create: false create: false
group: root group: root
insertbefore: '^\[Network\]\s*$' insertbefore: '^\[Network\]\s*$'
@@ -69,7 +69,23 @@
notify: notify:
- Restart sshd - Restart sshd
- name: Configure sshd_config - name: Add an ssh group
ansible.builtin.group:
local: false
name: ssh
state: present
system: true
- name: Add user to ssh group
ansible.builtin.user:
append: true
groups:
- ssh
local: false
name: "{{ sshd_user }}"
state: present
- name: Configure sshd_config (IPv4)
ansible.builtin.lineinfile: ansible.builtin.lineinfile:
backrefs: false backrefs: false
create: true create: true
@@ -82,7 +98,28 @@
path: "/etc/ssh/sshd_config" path: "/etc/ssh/sshd_config"
regexp: "{{ item.pattern }}" regexp: "{{ item.pattern }}"
state: present state: present
loop: "{{ sshd_config_lines }}" loop: "{{ sshd_ipv4_config_lines }}"
loop_control:
label: "{{ item.label }}"
notify:
- Restart sshd
- name: Configure sshd_config (IPv6)
when:
- not (ipv4_only | default(false))
ansible.builtin.lineinfile:
backrefs: false
create: true
encoding: "utf-8"
firstmatch: false
group: root
line: "{{ item.line }}"
mode: "0664"
owner: root
path: "/etc/ssh/sshd_config"
regexp: "{{ item.pattern }}"
state: present
loop: "{{ sshd_ipv6_config_lines }}"
loop_control: loop_control:
label: "{{ item.label }}" label: "{{ item.label }}"
notify: notify:
@@ -110,6 +147,8 @@
label: "{{ item.comment }}" label: "{{ item.comment }}"
- name: Configure UFW IPv6 rules - name: Configure UFW IPv6 rules
when:
- not (ipv4_only | default(false))
community.general.ufw: community.general.ufw:
comment: "{{ item.comment }}" comment: "{{ item.comment }}"
direction: "in" direction: "in"
+18 -8
View File
@@ -1,8 +1,13 @@
#code: language=ansible # code: language=ansible
sshd_port: 22 sshd_port: 22
sshd_config_lines: sshd_user: ladmin
sshd_address_family: "{{ (ipv4_only | default(false)) | ansible.builtin.ternary('inet', 'any') }}"
sshd_required_family_for_online: "{{ (ipv4_only | default(false)) | ansible.builtin.ternary('ipv4', 'both') }}"
sshd_ipv4_config_lines:
- label: PubkeyAuthentication - label: PubkeyAuthentication
pattern: '^\s*#*\s*PubkeyAuthentication\s*(yes|no)$' pattern: '^\s*#*\s*PubkeyAuthentication\s*(yes|no)$'
line: 'PubkeyAuthentication yes' line: 'PubkeyAuthentication yes'
@@ -29,19 +34,24 @@ sshd_config_lines:
line: 'UsePAM yes' line: 'UsePAM yes'
- label: PermitRootLogin - label: PermitRootLogin
pattern: '^\s*#*\s*PermitRootLogin\s*(yes|no|prohibit-password)$' pattern: '^\s*#*\s*PermitRootLogin\s*(yes|no|prohibit-password)$'
line: 'PermitRootLogin prohibit-password' line: 'PermitRootLogin no'
- label: AddressFamily - label: AddressFamily
pattern: '^\s*#*\s*AddressFamily\s*(inet|inet6|any)$' pattern: '^\s*#*\s*AddressFamily\s*(inet|inet6|any)$'
line: 'AddressFamily any' line: "AddressFamily {{ sshd_address_family }}"
- label: ListenAddress (IPv4) - label: ListenAddress (IPv4)
pattern: "^\\s*#*\\s*ListenAddress\\s*(0\\.0\\.0\\.0|{{ ipv4_address | replace('.', '\\.') }})$" pattern: "^\\s*#*\\s*ListenAddress\\s*(0\\.0\\.0\\.0|{{ ipv4_address | replace('.', '\\.') }})$"
line: "ListenAddress {{ ipv4_address }}" line: "ListenAddress {{ ipv4_address }}"
- label: ListenAddress (IPv6)
pattern: "^\\s*#*\\s*ListenAddress\\s*(::|{{ ipv6_address}})$"
line: "ListenAddress {{ ipv6_address }}"
- label: Port - label: Port
pattern: '^\s*#*\s*Port\s*[0-9]{1,5}$' pattern: '^\s*#*\s*Port\s*[0-9]{1,5}$'
line: "Port {{ sshd_port }}" line: "Port {{ sshd_port }}"
- label: AllowGroups
pattern: '^\s*#*\s*AllowGroups\s*.*$'
line: "AllowGroups ssh"
sshd_ipv6_config_lines:
- label: ListenAddress (IPv6)
pattern: "^\\s*#*\\s*ListenAddress\\s*(::|{{ ipv6_address }})$"
line: "ListenAddress {{ ipv6_address }}"
sshd_ufw_ipv4_rules: sshd_ufw_ipv4_rules:
- comment: SSH Clients v4 - comment: SSH Clients v4
@@ -55,4 +65,4 @@ sshd_ufw_ipv6_rules:
- comment: "SSH Clients v6" - comment: "SSH Clients v6"
source: "2600:4040:593d:8b30::/64" source: "2600:4040:593d:8b30::/64"
- comment: "SSH Servers v6" - comment: "SSH Servers v6"
source: "2600:4040:593d:8b10::/64" source: "2600:4040:593d:8b10::/64"
+76
View File
@@ -0,0 +1,76 @@
# code: language=ansible
- name: Ensure sudo is installed
community.general.pacman:
name:
- sudo
state: present
update_cache: true
- name: Create the sudo group
ansible.builtin.group:
local: false
name: sudo
state: present
system: true
- name: Get target users current groups
ansible.builtin.getent:
database: group
split: ':'
register: sudoers_user_groups_result
- name: Set empty sudoers_user_target_groups
ansible.builtin.set_fact:
sudoers_user_target_groups: []
- name: Loop group list and select groups containing sudoer user
when:
- sudoers_user in sudoers_user_groups_result.ansible_facts.getent_group[item]
ansible.builtin.set_fact:
sudoers_user_target_groups: "{{ sudoers_user_target_groups + [item] }}"
loop: "{{ sudoers_user_groups_result.ansible_facts.getent_group.keys() | list }}"
loop_control:
label: "{{ item }}"
- name: Filter sudoers_user_target_groups for wheel and users groups, and ensure it contains sudo group
ansible.builtin.set_fact:
sudoers_user_target_groups: "{{ ((sudoers_user_target_groups | difference(['wheel', 'users'])) + ['sudo']) | unique }}"
- name: Add user to sudo group and remove from wheel/users
ansible.builtin.user:
append: false
comment: "{{ sudoers_user_comment }}"
groups: "{{ sudoers_user_target_groups }}"
local: false
name: "{{ sudoers_user }}"
state: present
- name: Add sudoers entry
community.general.sudoers:
commands:
- ALL
defaults:
- '!fqdn' # Ensures using shortname only when assessing hostname in sudo rule
group: sudo
host: "{{ inventory_hostname_short }}"
name: sudo_group
nopassword: false
runas: root
state: present
sudoers_path: "/etc/sudoers.d"
validation: required
- name: Remove default sudoers file
ansible.builtin.file:
path: /etc/sudoers.d/90-cloud-init-users
state: absent
- name: Disable root login
ansible.builtin.user:
expires: 0
name: root
password: '!'
password_lock: true
shell: /usr/bin/nologin
state: present
+4
View File
@@ -0,0 +1,4 @@
# code: language=ansible
sudoers_user: ladmin
sudoers_user_comment: "Local Administrator"