Created
December 27, 2021 10:48
-
-
Save urpylka/c9ce7e12c7cba3562141f37413cc1ac1 to your computer and use it in GitHub Desktop.
Ansible playbook to add user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This playbook adds users to target linux servers with 'adm' group | |
# Author: Artem Smirnov <[email protected]> | |
# Using: | |
# 1. You need to add user certificates to './files' dir or to ./, | |
# where it has a next name format '<username>.pub' | |
# <username> will be used as username on servers. | |
# 2. ansible-playbook useradd.yml -i hosts -e 'target=<some_targets>' | |
# Required depedencies: | |
# ansible-galaxy collection install community.general ansible.posix | |
# It doesn't support --diff option due to user_module | |
# Additional information about user_module and authorized_key_module, you can find below: | |
# https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html | |
# https://docs.ansible.com/ansible/2.3/authorized_key_module.html | |
--- | |
- hosts: '{{ target }}' | |
gather_facts: "False" | |
become: yes | |
tasks: | |
- name: Add users to remote hosts | |
ansible.builtin.user: | |
name: "{{ item | basename | regex_replace('.pub','') }}" | |
password: "{{ lookup('community.general.random_string') | password_hash('sha512', 'mysecretsalt') }}" | |
shell: /bin/bash | |
state: present | |
system: yes | |
append: yes | |
groups: | |
- adm | |
loop: "{{ lookup('fileglob', '*.pub', wantlist=True) }}" | |
- name: Set authorized key taken from files | |
ansible.posix.authorized_key: | |
user: "{{ item | basename | regex_replace('.pub','') }}" | |
state: present | |
key: "{{ lookup('file', item) }}" | |
loop: "{{ lookup('fileglob', '*.pub', wantlist=True) }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment