hosts all conflicts with gather_facts - Alpine

This commit is contained in:
2025-11-30 17:27:55 -06:00
parent 6b7957ff23
commit 4fa0182002

View File

@@ -1,156 +1,126 @@
--- ---
## Checks/deploys a Linux system to be managed with Ansible. ## Checks/deploys a Linux system to be managed with Ansible.
- hosts: all gather_facts: yes
gather_facts: yes become: yes
become: yes become_method: doas
become_method: doas tasks:
# Gather system groups
- name: Gather all system groups
ansible.builtin.getent:
database: group
split: ':'
tasks: # Add doas package
- name: Install doas (for Alpine systems).
ansible.builtin.package:
name:
- doas
state: present
# Gather system groups # User account (ansible) configuration
- name: Gather all system groups - name: Add deployment user.
ansible.builtin.getent: user:
database: group name: ansible
split: ':' state: present
- name: Add deployment user to wheel group.
user:
name: ansible
groups: wheel
append: yes
when: "'wheel' in ansible_facts.getent_group"
# Add doas package # Ansible user SSH pub key
- name: Install doas (for Alpine systems). # This is a really stupid way to do it, but alas.
ansible.builtin.package: # This uses an environment variable named ansiblesvc_key in Semaphore which has the ssh-rsa pubkey.
name: - name: Create ssh directory for deployment user.
- doas file:
state: present path: /home/ansible/.ssh
state: directory
owner: ansible
group: ansible
- name: Add deployment user's SSH key.
copy:
content: "{{ ansiblesvc_key }}"
dest: /home/ansible/.ssh/authorized_keys
owner: ansible
group: ansible
# Add required packages because Debian is lame
- name: Install standard packages if not already installed.
# Looking at you LXCs. >.>
ansible.builtin.package:
name:
- curl
- net-tools
- wget
- util-linux
- python3
- iftop
state: present
# User account (ansible) configuration # Give ansible doas rights with no password required.
- name: Add deployment user. - name: Add doas rights with no password for deployment user (Alpine only)
user: lineinfile:
name: ansible dest: /etc/doas.conf
state: present regexp: '^ansible'
- name: Add deployment user to wheel group. line: 'permit keepenv nopass :ansible'
user: state: present
name: ansible validate: 'doas -C %s'
groups: wheel
append: yes
when: "'wheel' in ansible_facts.getent_group"
# Ansible user SSH pub key # User account (nhadmin) configuration, for sysadmin use
# This is a really stupid way to do it, but alas. - name: Create user nhadmin.
# This uses an environment variable named ansiblesvc_key in Semaphore which has the ssh-rsa pubkey. user:
- name: Create ssh directory for deployment user. name: nhadmin
file: state: present
path: /home/ansible/.ssh password: "{{ nhadmin_password | password_hash('sha512') }}"
state: directory shell: /bin/bash
owner: ansible
group: ansible
- name: Add deployment user's SSH key. - name: Add nhadmin to wheel group.
copy: user:
content: "{{ ansiblesvc_key }}" name: nhadmin
dest: /home/ansible/.ssh/authorized_keys groups: wheel
owner: ansible append: yes
group: ansible when: "'wheel' in ansible_facts.getent_group"
# shell:
# cmd: echo "{{ ansiblesvc_key }}" > /home/ansible/.ssh/authorized_keys
# creates: /home/ansible/.ssh/authorized_keys
# Add required packages because Debian is lame # Sysadmin user SSH pub key
- name: Install standard packages if not already installed. # This is a really stupid way to do it, but alas.
# Looking at you LXCs. >.> # This uses an environment variable named nhadmin_key in Semaphore which has the ssh-rsa pubkey.
ansible.builtin.package: - name: Create ssh directory for nhadmin.
name: file:
- curl path: /home/nhadmin/.ssh
- net-tools state: directory
- wget owner: nhadmin
- util-linux group: nhadmin
- python3 - name: Add nhadmin user's SSH key.
- iftop copy:
state: present content: "{{ nhadmin_key }}"
dest: /home/nhadmin/.ssh/authorized_keys
# Give ansible doas rights with no password required. owner: nhadmin
- name: Add doas rights with no password for deployment user (Alpine only) group: nhadmin
lineinfile:
dest: /etc/doas.conf
regexp: '^ansible'
line: 'permit keepenv nopass :ansible'
state: present
validate: 'doas -C %s'
# User account (nhadmin) configuration, for sysadmin use # SSH config updating
- name: Create user nhadmin. - name: Update SSH configuration to disallow root login and disable password authentication.
user: lineinfile:
name: nhadmin dest: /etc/ssh/sshd_config
state: present regexp: "{{ item.regexp }}"
password: "{{ nhadmin_password | password_hash('sha512') }}" line: "{{ item.line }}"
shell: /bin/bash state: present
validate: 'sshd -t -f %s'
with_items:
- regexp: "^PermitRootLogin"
line: "PermitRootLogin no"
- regexp: "^PasswordAuthentication"
line: "PasswordAuthentication no"
- regexp: "^PubkeyAuthentication"
line: "PubkeyAuthentication yes"
- name: Restart SSH service.
service:
name: sshd
state: restarted
- name: Add nhadmin to wheel group. # Delete our network ansible key from the root user.
user: - name: Delete our network ansible key (and other keys) from the root user.
name: nhadmin file:
groups: wheel path: /root/.ssh/authorized_keys
append: yes state: absent
when: "'wheel' in ansible_facts.getent_group"
- name: Add nhadmin to sudo group.
user:
name: nhadmin
groups: sudo
append: yes
when: "'sudo' in ansible_facts.getent_group"
- name: Add nhadmin to systemd-journal group.
user:
name: nhadmin
groups: systemd-journal
append: yes
when: "'systemd-journal' in ansible_facts.getent_group"
# Sysadmin user SSH pub key
# This is a really stupid way to do it, but alas.
# This uses an environment variable named nhadmin_key in Semaphore which has the ssh-rsa pubkey.
- name: Create ssh directory for nhadmin.
file:
path: /home/nhadmin/.ssh
state: directory
owner: nhadmin
group: nhadmin
- name: Add nhadmin user's SSH key.
copy:
content: "{{ nhadmin_key }}"
dest: /home/nhadmin/.ssh/authorized_keys
owner: nhadmin
group: nhadmin
# shell:
# cmd: echo "{{ nhadmin_key }}" > /home/nhadmin/.ssh/authorized_keys
# creates: /home/nhadmin/.ssh/authorized_keys
# SSH config updating
- name: Update SSH configuration to disallow root login and disable password authentication.
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
state: present
validate: 'sshd -t -f %s'
with_items:
- regexp: "^PermitRootLogin"
line: "PermitRootLogin no"
- regexp: "^PasswordAuthentication"
line: "PasswordAuthentication no"
- regexp: "^PubkeyAuthentication"
line: "PubkeyAuthentication yes"
- name: Restart SSH service.
service:
name: sshd
state: restarted
# Delete our network ansible key from the root user.
- name: Delete our network ansible key (and other keys) from the root user.
file:
path: /root/.ssh/authorized_keys
state: absent
# Upgrade all apt packages for good measure.
- name: Upgrade all apt packages
apt: upgrade=dist force_apt_get=yes
when: aptfolder.stat.exists