---
- name: set public key on remote hosts & set reomte hosts sudoers file
hosts: all
# 此脚本要求所有的主机 root 用户密码相同
become: no
remote_user: root
vars:
- remoteuser:
user1: rhce
tasks:
- name: set secondary group for rhce
user:
name: "{{ remoteuser['user1'] }}"
groups: wheel
append: yes
register: new_user
- name: Set authorized key taken from file
ansible.posix.authorized_key:
user: "{{ new_user['name'] }}"
state: present
key: "{{ lookup('file', '/home/student/.ssh/id_rsa.pub') }}"
- name: Change file ownership, group and permissions
ansible.builtin.file:
path: /etc/sudoers
mode: '0644'
- name: Ensure /etc/suders no passwd
ansible.builtin.lineinfile:
path: /etc/sudoers
insertafter: '^%sudo '
line: '%wheel ALL=(ALL:ALL) NOPASSWD: ALL'
- name: Change file ownership, group and permissions
ansible.builtin.file:
path: /etc/sudoers
owner: root
group: root
mode: '0440'