cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Cloud



Etc


Reference














.

middleware:iac:fabric:best.html



Fabric

Fabric Best Practices



Fabric


Directory structure

fabric/
    fabfile.py
    host.sh
    host.list


    test.py
    check.py
    user.py
    middleware.py
    os.py
    tmp.py


Example of execution

$ echo |fab -l
$ host.sh host.list  -t XXXX

$ host.sh host.list  hostlist.XXXX |fab test.hostname

$ host.sh host.list  XXXX |fab -- COMMAND
$ host.sh host.list  XXXX |fab -- uptime
$ host.sh host.list  XXXX |fab -- uptime  |grep load
$ host.sh host.list  XXXX |fab -- uptime  |grep load > tmp.txt
$ host.sh host.list  XXXX |fab -- uptime  |grep load tee  tmp.txt
$ host.sh host.list  XXXX |fab -- uptime  |grep load 2>&1 | tee   tmp.txt

$ echo  xx.xx.xx.xx |fab -- uptime


Files

fabfile.py

import sys
from fabric.api import *
from fabric.contrib import files

env.warn_only = True

@task
def production():
    env.user = 'USER01'
    env.key_filename = '~/.ssh/id_rsa_production'
    env.password = 'passphrase-for-key'

@task
def staging():
    env.port = 22
    env.user = 'USER01'
    env.password = 'PASSWORD'

@task
def develop():
    env.port = 22
    env.user = 'USER02'
    env.key_filename = '~/.ssh/id_rsa_develop'
    env.password = 'passphrase-for-key'


lines = sys.stdin.read().splitlines()
env.hosts = filter(bool, lines)
print "target hosts: %r" % env.hosts

import test
import user
import middleware
import os

host.sh

#!/bin/sh

LIST=$1
 
case "$2" in
   -t | --test )
    cat "${LIST}" |grep -Ev "^#|^$" |grep "$3"
    ;;
   -a | --all )
    cat "${LIST}" |grep -Ev "^#|^$" |awk '{print $2'}
    ;;
   * )
    cat "${LIST}" |grep -Ev "^#|^$" |grep "$2"  |awk '{print $2'}
    ;;
esac

host.list

#test
test-web1  test-web reboot1
test-web2  test-web reboot1
test-db1  test-db reboot2
test-db2  test-db reboot2
 
#Example
example-web2   example-web   reboot2
example-web2   example-web   reboot4



DNS or /etc/hosts

#test
192.168.0.1 test-web1
192.168.0.2 test-web2
192.168.0.3 test-db1
192.168.0.4 test-db2
 
#Example
192.168.1.1 example-web2
192.168.1.2 example-web2

ping.sh

#!/bin/sh
 
LIST=$1


for i in `cat $1 |grep -Ev "^#|^$"`
do
    echo $i
    ping -i 0.5 -c 2 $i
done

exit


test.py

import sys
from fabric.api import *
from fabric.contrib import files


@task 
def test1():
    '''hostname , whoami test3'''
    run('hostname')
    run('whoami')
    test3()

@task 
def test2():
    '''uptime'''
    run('uptime')

@task 
def test3():
    '''test3'''
    run('echo test3')

@task
def sudotest():
    '''sudo test'''
    sudo('cat /etc/passwd')  


check.py

import sys
from fabric.api import *
from fabric.contrib import files

env.warn_only = True

@task 
def check_ping():
    run('ping -i 0.1 -c 3 xx.xx.xx.xx')

@task 
def check_user():
    '''check user'''
    run('/usr/bin/id user01 > /dev/null 2>&1  || echo "ERR : there isn\'t user01. "')
    run('/usr/bin/id user02 > /dev/null 2>&1  || echo "ERR : there isn\'t user02. "')
    run('/usr/bin/id user03 > /dev/null 2>&1  || echo "ERR : there isn\'t user03. "')





Fabric





Fabric




middleware/iac/fabric/best.html.txt ยท Last modified: 2017/11/05 by admin

Page Tools