Wednesday, January 18, 2017

Python fabric Part1

Fabric is a Python library and command-line tool helping to simplify management tasks via automation of the tasks.
From personal experience:
Fabric is very good for Linux tasks automation but I tried a lot to automate Cisco IOS tasks and failed (it seems like Fabric reconnects to the target system every time when you use `run` command , so after issuing `run('enable', shell=False)`  when you send `run('configure terminal', shell=False)` you are not in `enable mode` and end with error. But if you want to auto-connect to the Cisco device and then enter commands manually - you can use `from fabric import open_shell` and then use `open_shell()` in your script you'll end up connected to the IOS command line where you can add commands manually).

[admin@localhost ~]$ pip install --upgrade pip
[admin@localhost ~]$ pip install fabric

In desired directory:
[admin@localhost ~]$ touch fabfile.py

task decorator makes command available to fab
run executes smth on remote host (local like run but executes only on local
host). If you want to execute command on non-Linux machine - use `shell=False`:
@task
@hosts('cisco@10.3.3.1:2200')
def test1():
run('sh ver', shell=False)
Otherwise you will experience an error like this:
Line has invalid autocommand "/bin/bash -l -c "sh ver""
hosts decorator makes command available for specified hosts
roles decorator makes command available for specified roledefs
runs_once decorator executes command only one time even if hosts list or role for this command is specified
[admin@localhost ~]$ vi fabfile.py
from fabric.api import env, task, run, roles, runs_once, local, hosts

env.hosts = ['127.0.0.1', 'localhost']

env.roledefs = {
 'switch'  : ['10.1.1.1', '10.1.1.2'],
 'asterisk' : ['root@10.2.2.1']
}

@task
@runs_once
@roles('switch')
def list_roles():
 for role in env.roledefs:
  print role

@task
@roles('switch')
def hello(name='World'):
 print("Hello %s!" % name)

def ldr():
 run('ls -l')

@task
def test_exec():
 print('executed')

@task
@hosts('root@10.2.2.1:2200')
def test1():
 run('ls -l')

@task
def test2():
 local('ls -l')

[admin@localhost ~]$ fab --list
Available commands:
    hello
    list_roles
    test_exec
    test1
    test2

[admin@localhost ~]$ fab hello
[10.1.1.1] Executing task 'hello'
Hello World!
[10.1.1.2] Executing task 'hello'
Hello World!

[admin@localhost ~]$ fab hello:name='Fellow'
[127.0.0.1] Executing task 'hello'
Hello Fellow!
[localhost] Executing task 'hello'
Hello Fellow!

Per-task, command-line host lists (fab mytask:host=host1 or fab mytask:role=role1) override absolutely everything else:
[admin@localhost ~]$ fab hello:role=asterisk
[10.2.2.1] Executing task 'hello'
Hello World!

If command have no hosts or roles specified with decorator and env.host is set, this command will be executed for all hosts in env.host:
[admin@localhost ~]$ fab test_exec
[127.0.0.1] Executing task 'test_exec'
executed
[localhost] Executing task 'test_exec'
executed

No comments:

Post a Comment