Fabric ScriptTo Create Users

Hi today I got a chance or I would like to say a requirement from my team,  a boring task of creating same user on all Linux system in my environment, total number of Linux system in my environment is 175 +.  Creating  a user on 175 system manually would take more than a day and more over is a repetitive task.

I remembered fabric which was one of my college assignment to automating some basic task,  two years out of college never got a chance to work on fabric ever. I took this as a very good opportunity to refresh my Fabric skill and implement something new in my company.

I am sure you will find lot of documentation about what is fabric and how does it work, I will brief about fabric and directly jump onto the script which you make use  to create users.

Fabric is Python based automation tool  which works on SSH for remote execution used by system admins like me :). You have lot of open source automation tool in market like Puppet, Chef, SaltStack and many more.

Fabric is simple, needs only SSH enabled on remote server. You need to first install fabric  on the system from which script will be executed. Fabric by default execute sequentially, it can be made parallel also.  Stay tuned for more updates on fabric.

Let’s Start

I am using a Debian system hence all my command will be Debian based commands, for other Linux flavors make required changes.

apt-get install fabric

That’s all, fabric ready to automate your Linux world :).  Open a text editor using vi or vim and paste below script,

vi fabfile.py

#!  /usr/bin/env python
from fabric.api import env run
env.passwords = {‘user@X.X.X.X:22′:’password’, ‘user@X.X.X.X:22’: ‘password’}
env.hosts = [‘user@′,’user@’]

def makeuser():
 run(“useradd  -m -s /bin/bash username; echo ‘username:password’ | chpasswd”)

Let me explain each line

  • #!  /usr/bin/env python  –  Indicating Python on my system
  • from fabric.api import env run –   Importing  “env and run” from Python library which is used by fabric
  • env.passwords  –  Mentioning  system password, make sure its in same order ‘username@IP_Address:Port’:’password’.
  •  env.hosts – List machine on which this script has to be executed,  this needs to be in same order as env.password.  And env.password  should be first line.
  • def makeuser():  –  Function  to create user on remote machine
    • run  – This indicates the command which needs to be executed and as you can its just a normal Linux command.

#fab makeuser

This is how you call/execute the script from Linux bash shell.

Note :
1) Fabric is sequential, only after it completes with first system it moves on to next.
2) If something goes wrong on first system script will stop executing, it will not move on to next system.

Stay tuned for more.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s