├── .gitignore ├── README.md ├── ansible-create-users.jpg ├── create_users.yml ├── hosts ├── pub_keys ├── jaroslav.pub └── maruna.pub └── users.yml /.gitignore: -------------------------------------------------------------------------------- 1 | *.[jJ][pP][gG] 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ansible-create-users 2 | 3 | Create on each server (file hosts) users from the list (file users.yml). 4 | 5 | ![ansible-create-users](https://github.com/msergiy87/ansible-create-users/blob/master/ansible-create-users.jpg) 6 | 7 | User Settings 8 | ------------ 9 | 10 | - Shell = /bin/bash 11 | - Add the public key in pub_keys dir (username.pub) 12 | - In users.yml specify that user should have the right to sudo or not 13 | 14 | Distros tested 15 | ------------ 16 | 17 | Currently, this is only tested on Ubuntu 14.04 as a client and server machine. It should theoretically work on older versions of Ubuntu or Debian based systems. 18 | 19 | Usage 20 | ------------ 21 | - install ansible 22 | - create keys 23 | - configure client server authorized_keys 24 | - upload repository and change 25 | - run command 26 | 27 | ``` 28 | ansible-playbook -i hosts create_users.yml 29 | ``` 30 | -------------------------------------------------------------------------------- /ansible-create-users.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/msergiy87/ansible-create-users/057374db6830fb73e5c2a87faa653377f7f246b2/ansible-create-users.jpg -------------------------------------------------------------------------------- /create_users.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: all 3 | user: root 4 | 5 | # vars: 6 | # users: 7 | # - username: galya 8 | # use_sudo: no 9 | 10 | # - username: kolya 11 | # use_sudo: yes 12 | 13 | tasks: 14 | - include_vars: users.yml 15 | 16 | - name: Add users | create users, shell, home dirs 17 | user: name={{ item.username }} shell=/bin/bash createhome=yes comment='create with ansible' 18 | with_items: '{{users}}' 19 | 20 | - name: Setup | authorized key upload 21 | authorized_key: user={{ item.username }} 22 | key="{{ lookup('file', 'pub_keys/{{ item.username }}.pub') }}" 23 | # path='/home/{{ item.username }}/.ssh/authorized_keys' 24 | # manage_dir=no 25 | with_items: '{{users}}' 26 | 27 | - name: Sudoers | update sudoers file and validate 28 | lineinfile: "dest=/etc/sudoers 29 | insertafter=EOF 30 | line='{{ item.username }} ALL=(ALL) NOPASSWD: ALL' 31 | regexp='^{{ item.username }} .*' 32 | state=present" 33 | when: '{{ item.use_sudo }} == True' 34 | with_items: '{{users}}' 35 | -------------------------------------------------------------------------------- /hosts: -------------------------------------------------------------------------------- 1 | [servers] 2 | 172.16.22.100 3 | -------------------------------------------------------------------------------- /pub_keys/jaroslav.pub: -------------------------------------------------------------------------------- 1 | ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXn2ZB7riS0wu23idJGYp5Yu16PlJHYR+BhfNh9vRNbj7pdqQnDRgc4UVjB12GhFJzYz1QkdQ6xO2YPJww6KFNdZeJO9CTsxnrkBn/EsN19r20d7QR84TwwR6fbVYORBXfrnONANjGtCRIVOtmyRcsvapEp/OXl3gCYlXpPBwXvdoS0gRR+24b29SmGjfd3B8P59OUUBHZwnHXSCVo+B1Sl1gxvet2lAcJfrJdlvNCG2vgKYZRZ2kvxX1NFfOT+L3rpQw9PhDA8OXsdjlk00imkb3p+lFZ7YcylOy722V6EZqKRIQRDAhVuLlgCw08qJUkBRDYA8+GxESCs2Egjhrp jaroslav@host 2 | -------------------------------------------------------------------------------- /pub_keys/maruna.pub: -------------------------------------------------------------------------------- 1 | ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXn2ZB7riS0wu23idJGYp5Yu16PlJHYR+BhfNh9vRNbj7pdqQnDRgc4UVjB12GhFJzYz1QkdQ6xO2YPJww6KFNdZeJO9CTsxnrkBn/EsN19r20d7QR84TwwR6fbVYORBXfrnONANjGtCRIVOtmyRcsvapEp/OXl3gCYlXpPBwXvdoS0gRR+24b29SmGjfd3B8P59OUUBHZwnHXSCVo+B1Sl1gxvet2lAcJfrJdlvNCG2vgKYZRZ2kvxX1NFfOT+L3rpQw9PhDA8OXsdjlk00imkb3p+lFZ7YcylOy722V6EZqKRIQRDAhVuLlgCw08qJUkBRDYA8+GxESCs2Egjhrp maruna@host 2 | -------------------------------------------------------------------------------- /users.yml: -------------------------------------------------------------------------------- 1 | --- 2 | users: 3 | - username: maruna 4 | use_sudo: no 5 | 6 | - username: jaroslav 7 | use_sudo: yes 8 | --------------------------------------------------------------------------------