Ansible user add for RHEL and Ubuntu

in ubuntu •  6 years ago  (edited)

This is a quick ansible script to add a user to either RHEL or Ubuntu and even hide said user from the gui login if you want. A customer needed this to limit the users posted on the login page when logging via the GUI.

Cut and paste this into a file named user-someusername.yml
To run use "asible-playbooks -i hostIP, user-someusername.yml".

- hosts: all
  user: root
  vars:
    userName: <username>
    userFull: <Full Username>
    userPassword: <user your hash from the /etc/passwd file>
    userKey: <generate a key pair using "ssh-keygen -t rsa -b 4096" and cut-n-paste your pub key here. >

  tasks:
### RHEL
  - name: Make sure the wheel group is working in Redhat.
    lineinfile: "dest=/etc/sudoers regexp='^# %wheel' line='%wheel ALL=(ALL) ALL'"
    when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

  - name: Add {{ userName }} account with sudo perms.
    user: name={{ userName }} comment={{ userFull }} password={{ userPassword }} groups=wheel append=yes
    when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

  - name: Hidding users from the GNOME login screen.
    lineinfile:
      dest: /etc/gdm/custom.conf
      state: present
      line: "{{ item.line }}"
    with_items:
      - { line: '[greeter]' }
      - { line: 'Exclude=root,{{ userName }}' }
    when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'

### DEBIAN
  - name: Add {{ userName }} account with sudo perms.
    user: name={{ userName }} comment={{ userFull }} shell=/bin/bash createhome=yes groups=sudo password={{ userPassword }} append=yes
    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

  - name: Creating the AccountsService file for {{ userName }}
    file: path=/var/lib/AccountsService/users/{{ userName }} state=touch mode=0755
    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

  - name: Hidding new user from GUI logon screen
    lineinfile:
      dest: /var/lib/AccountsService/users/{{ userName }}
      line: "{{ item.line }}"
    with_items:
      - { line: '[User]' }
      - { line: 'SystemAccount=true' }
    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

### ALL OS
  - name: Add user's pub key.
    authorized_key: user={{ userName }} key={{ userKey }} state=present

  - name: Creating INFO file for new user
    lineinfile: dest=/home/{{ userName }}/INFO state=present create=yes line="somepassword"`
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @zooraw! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!