69 lines
1.8 KiB
YAML
69 lines
1.8 KiB
YAML
---
|
|
## Installing packages
|
|
- name: Install sudo on apt systems
|
|
when: (ansible_facts['distribution'] == "Debian") or
|
|
(ansible_facts['distribution'] == "Ubuntu")
|
|
ansible.builtin.apt:
|
|
name:
|
|
- sudo
|
|
update-cache: true
|
|
|
|
- name: Ensure /etc/resolv.conf exists with DNS servers
|
|
when: (ansible_facts['distribution'] == "Alpine")
|
|
ansible.builtin.copy:
|
|
dest: /etc/resolv.conf
|
|
content: |
|
|
nameserver 192.168.0.1
|
|
nameserver 8.8.8.8
|
|
mode: '0644'
|
|
owner: root
|
|
group: root
|
|
become: true
|
|
|
|
# The same commands for Alpine
|
|
- name: Install sudo on Alpine
|
|
when: (ansible_facts['distribution'] == "Alpine")
|
|
community.general.apk:
|
|
name: sudo
|
|
update_cache: true
|
|
remote_user: ansible
|
|
|
|
- name: Enable sudo for root and human_admin_user
|
|
when: (ansible_facts['distribution'] == "Alpine")
|
|
ansible.builtin.copy:
|
|
dest: /etc/sudoers.d/80-ansible-created-users
|
|
content: |
|
|
root ALL=(ALL) ALL:ALL
|
|
{{ human_admin_user }} ALL=(ALL) ALL:ALL
|
|
remote_user: ansible
|
|
|
|
|
|
## Creating and setting up the ansible user
|
|
## First, create sshusers group to grant ssh access
|
|
- name: Ensure group "sshusers" exists
|
|
ansible.builtin.group:
|
|
name: sshusers
|
|
state: present
|
|
|
|
- name: Ensure group "sudo" exists
|
|
ansible.builtin.group:
|
|
name: sudo
|
|
state: present
|
|
|
|
|
|
## Add the user to sshusers (for ssh access) and sudo (gain root access)
|
|
- name: Create a new user with a password for Ansible
|
|
ansible.builtin.user:
|
|
name: ansible
|
|
password: "{{ ansible_user_passwd_hash }}"
|
|
|
|
groups: sshusers,sudo
|
|
append: true
|
|
|
|
## Since password authentication in SSH will be disabled, we need to add an authorized key
|
|
- name: Set authorized key taken from file
|
|
ansible.posix.authorized_key:
|
|
user: ansible
|
|
state: present
|
|
key: "{{ ansible_ssh_key }}"
|