A D Vishnu Prasad

Director of Cloud Engineering @ Team8Solutions, Freelancer

Provide Ssh Access to User Using Ansible

How to provide production server access to users using ansible?

Assumptions:

You have ssh access to prod servers as Ubuntu user.

Ansible Script for adding user
add_user.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ansible-playbook add_user.yml --extra-vars "user=advp key=advp.pub" -i ad
---
- hosts: all
  remote_user: ubuntu
  become: True
  become_method: sudo
  become_user: root

  tasks:
    - name: Add user
      user:
            name="{{ user }}"
            shell=/bin/bash
            state=present
            groups=staff
            append=yes
            createhome=yes
            password=<password SALT>
            update_password=on_create

    - name: Add keys to the users
      authorized_key:
        key="{{ lookup('file', '{{ key }}') }}"
        user="{{ user }}"
        state=present

Add a single user:

1
ansible-playbook add_user.yml --extra-vars "user=advp key=<user/pub/key/path>"  -i inventory/ad

don’t forget to change the user and key location in the above command.