fabric批量操做遠程操做主機的練習

fabric是python的一個基於命令行的自動化部署框架,用docker開了兩個容器來學習fabric.python

#!/usr/bin/env python
#-*- coding=utf-8 -*-
from fabric.api import *
env.hosts=[
  'root@114.215.86.228:22',
  'root@114.215.86.228:32',
]
env.passwords={
  'root@114.215.86.228:22':'*****',
  'root@114.215.86.228:32':'***',
}
@task
def task1():
  run('ps')
@task
def task2():
  run('uname -a')
@task
def dotask():
  execute(task1)
  execute(task2)

執行結果linux

root@0fe14b5400a8:/home# fab dotask
[root@114.215.86.228:22] Executing task 'dotask'
[root@114.215.86.228:22] Executing task 'task1'
[root@114.215.86.228:22] run: ps
[root@114.215.86.228:22] out:   PID TTY          TIME CMD
[root@114.215.86.228:22] out: 26424 pts/2    00:00:00 ps
[root@114.215.86.228:22] out:

[root@114.215.86.228:32] Executing task 'task1'
[root@114.215.86.228:32] run: ps
[root@114.215.86.228:32] out:   PID TTY          TIME CMD
[root@114.215.86.228:32] out:    59 pts/0    00:00:00 ps
[root@114.215.86.228:32] out:

[root@114.215.86.228:22] Executing task 'task2'
[root@114.215.86.228:22] run: uname -a
[root@114.215.86.228:22] out: Linux iZ2893wjzgyZ 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@114.215.86.228:22] out:

[root@114.215.86.228:32] Executing task 'task2'
[root@114.215.86.228:32] run: uname -a
[root@114.215.86.228:32] out: Linux c368a2768e6b 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@114.215.86.228:32] out:

[root@114.215.86.228:32] Executing task 'dotask'
[root@114.215.86.228:22] Executing task 'task1'
[root@114.215.86.228:22] run: ps
[root@114.215.86.228:22] out:   PID TTY          TIME CMD
[root@114.215.86.228:22] out: 26447 pts/2    00:00:00 ps
[root@114.215.86.228:22] out:

[root@114.215.86.228:32] Executing task 'task1'
[root@114.215.86.228:32] run: ps
[root@114.215.86.228:32] out:   PID TTY          TIME CMD
[root@114.215.86.228:32] out:    63 pts/0    00:00:00 ps
[root@114.215.86.228:32] out:

[root@114.215.86.228:22] Executing task 'task2'
[root@114.215.86.228:22] run: uname -a
[root@114.215.86.228:22] out: Linux iZ2893wjzgyZ 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@114.215.86.228:22] out:

[root@114.215.86.228:32] Executing task 'task2'
[root@114.215.86.228:32] run: uname -a
[root@114.215.86.228:32] out: Linux c368a2768e6b 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@114.215.86.228:32] out:


Done.
Disconnecting from 114.215.86.228... done.
Disconnecting from 114.215.86.228:32... done.

儘管如此,密碼是明文,不安全,所以仍然推薦用加key的方式管理服務器docker

相關文章
相關標籤/搜索