You can execute this playbook passing the target hostname and the username as extra-vars
Example:
$ ansible-playbook create-sudo-user.yml -e "hostname=server1" -e "user=demo"
---
- hosts: "{{ hostname }}"
become: yes
tasks:
- name: create {{ user }} group
group:
local: yes
name: "{{ user }}"
state: present
system: yes
- name: create {{ user }} user
user:
name: "{{ user }}"
shell: /bin/bash
home: /home/{{ user }}
create_home: True
append: yes
groups: wheel
system: True
state: present
- name: create .ssh directory
file:
path: /home/{{ user }}/.ssh
owner: "{{ user }}"
group: "{{ user }}"
mode: '0700'
state: directory
- name: create ssh public key
copy:
content: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqQ0Hcy9LTKBM7waWkoZevOaQFyzMKgpyRPDJ8Da90zKBYtT2vdZno53cCDQ1X+/jEKmIwBLLLCLYc0RYbYNQDrcSDVpuhAflUptm7+4R8JsVrD9OKbJrXtz/5KTud98Eev8yp+GY2wYUSRSKM2sPlmOdWdBilhzmjAYbJ10w8D/57E+6z4I/LfOYqWy3gkXHqMd6uS4bW5PAAFzCbaS0+gQby526l0wrXzvrBPjHXMqgVdGtgTmxVBAp+hKxjEGXyfeJDp3DqqQuDxForyoPiwHnGnwZlhclHcO+46kdoTW+TWcTfU8Bcm+0w4m9BEFB0LTP4FBXnEIiB0BpsoklP
dest: /home/{{ user }}/.ssh/authorized_keys
owner: "{{ user }}"
group: "{{ user }}"
mode: '0600'
- name: add {{ user }} sudoers permission
copy:
content: |
{{ user }} ALL=(ALL) NOPASSWD: ALL
dest: /etc/sudoers.d/{{ user }}
owner: root
group: root
mode: '0644'