made use of username of user logged in and running 'sudo' for particular tasks, added true 'become' attrbute to some tasks

This commit is contained in:
2026-06-10 14:01:49 -04:00
parent fc565fa3ce
commit 50c7b6a56d
2 changed files with 11 additions and 2 deletions

View File

@@ -1,11 +1,17 @@
#SPDX-License-Identifier: MIT-0 #SPDX-License-Identifier: MIT-0
--- ---
# tasks file for roles/init-vps # tasks file for roles/init-vps
- name: Acquiring current user
when: ansible_facts["system"] == "Linux"
ansible.builtin.shell:
cmd: "echo {{ ansible_user }}"
register: current_user
- name: Checking whether administrative login used - name: Checking whether administrative login used
when: ansible_user not in (admins | map(attribute="username") | list) and ansible_user != "root" when: ansible_user not in (admins | map(attribute="username") | list) and ansible_user != "root"
ansible.builtin.fail: ansible.builtin.fail:
msg: Must use administrative user for subsequent tasks msg: Must use administrative user for subsequent tasks
- name: Hardening SSH service - name: Hardening SSH service
become: true
ansible.builtin.copy: ansible.builtin.copy:
src: sshd_config.d/harden.conf src: sshd_config.d/harden.conf
dest: /etc/ssh/sshd_config.d/harden.conf dest: /etc/ssh/sshd_config.d/harden.conf

View File

@@ -2,6 +2,11 @@
--- ---
# tasks file for roles/init-vps # tasks file for roles/init-vps
# @TODO complete below tasks # @TODO complete below tasks
- name: Acquiring current user
when: ansible_facts["system"] == "Linux"
ansible.builtin.shell:
cmd: "echo {{ ansible_user }}"
register: current_user
- name: Checking whether administrative login used - name: Checking whether administrative login used
when: ansible_user not in (admins | map(attribute="username") | list) when: ansible_user not in (admins | map(attribute="username") | list)
ansible.builtin.fail: ansible.builtin.fail:
@@ -46,8 +51,6 @@
when: ansible_facts["system"] == "Linux" when: ansible_facts["system"] == "Linux"
become: true become: true
ansible.builtin.blockinfile: ansible.builtin.blockinfile:
# src: sshd_config.d/sftp.conf.j2
# dest: /etc/ssh/sshd_config.d/sftp.conf
path: /etc/ssh/sshd_config.d/sftp.conf path: /etc/ssh/sshd_config.d/sftp.conf
block: | block: |
Match Group {{ item.name }} Match Group {{ item.name }}