Guest User

User Creation with Sudo & PEM key -- Ansible Automation

a guest
Apr 24th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.28 KB | None | 0 0
  1. # Creating a User with Adding into Sudoers file. And also providing user to public key login Access.
  2. # Author : Ashok Kalakoti
  3. # Date: 2017
  4.  
  5. ---
  6. - hosts: all
  7.   become: yes
  8.   become_user: root
  9.  
  10.   tasks:
  11.     - name: Copy sudoers file for safety
  12.       command: cp -f /etc/sudoers /etc/sudoers.tmp
  13.  
  14.     - name: Create sudoers file backup
  15.       command: cp -f /etc/sudoers /etc/sudoers.bak
  16.  
  17.     - name: Create admins group
  18.       group: name=admins system=yes state=present
  19.  
  20.     - name: make sure we can sudo as admin group
  21.       lineinfile: dest=/etc/sudoers.tmp state=present regexp='^%admins' line='%admins ALL=(ALL) ALL'
  22.  
  23.     - name: also make sure ssh-agent works via sudo
  24.       lineinfile: dest=/etc/sudoers.tmp state=present regexp='^Defaults env_keep\+\=SSH_AUTH_SOCK' line='Defaults env_keep+=SSH_AUTH_SOCK'
  25.  
  26.     - name: User Adding and Appending in Sudoers file
  27.       user:
  28.        name: ashok
  29.        groups: admins
  30.        append: yes
  31.        password: $1$Ashok$zyIc8nUhsB0urEKYNlktk.
  32.  
  33.     - name: Final sudoers file check
  34.       shell: visudo -q -c -f /etc/sudoers.tmp && cp -f /etc/sudoers.tmp /etc/sudoers
  35.      
  36.     - name: Set up authorized keys for the Ashok user
  37.       authorized_key: user=ashok key="{{item}}"
  38.       with_file:
  39.        - /home/ansadm/.ssh/id_rsa.pub
Add Comment
Please, Sign In to add comment