当前位置:   article > 正文

Fabric管理组件的使用

import fabric

Fabric的官方网站:
http://www.fabfile.org
帮助文档:
https://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html

  1. Fabric的安装
  1. yum install -y make gcc gcc++ python-devel python-pip
  2. $ pip install fabric==1.14.0
  3. 或者
  4. $ pip3 install fabric2 (没有fabric.api模块)
  1. 验证安装
  1. python
  2. >>> import fabric
  3. 或者
  4. python3
  5. >>> import fabric
  1. 命令行入口fab
fab [options]  --  [shell command]

基本用法

Fabric的核心API

核心API类别
带颜色的输出类color output
上下文管理类context managers
装饰类decorators
网络类network
操作类operations
任务类tasks
工具类utils

fabric.api命令集
1720799-20190701235854246-1570029464.jpg

  1. 使用方法
  1. $ cat fabfile.py
  2. ----------------------------------
  3. #!/usr/bin/python
  4. # -*- coding: utf-8 -*-
  5. from fabric.api import *
  6. from fabric.colors import *
  7. from fabric.context_managers import *
  8. env.user = 'beeworkshop'
  9. env.hosts = ['192.168.30.66']
  10. env.password = '123456'
  11. @task
  12. def run_remote_cmds():
  13. print yellow("我要执行命令啦")
  14. with settings(warn_only=True): <---错误也继续执行
  15. local("hostname")
  16. local("uname -a")
  17. run("w")
  18. run("hostname")
  19. run("ifconfig")
  20. run("ls -l")
  21. ---------------------------------
  22. $ fab -l
  23. Available commands:
  24. run_remote_cmds
  25. $ fab run_remote_cmds
  26. [192.168.30.66] Executing task 'run_remote_cmds'
  27. 我要执行命令啦
  28. [localhost] local: hostname
  29. bee-a
  30. [localhost] local: uname -a
  31. Linux bee-a 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  32. [192.168.30.66] run: w
  33. [192.168.30.66] out: 01:04:50 up 1:35, 1 user, load average: 0.00, 0.00, 0.00
  34. [192.168.30.66] out: USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  35. [192.168.30.66] out: beeworks pts/0 192.168.30.6 01:04 0.00s 0.02s 0.02s w
  36. [192.168.30.66] out:
  37. [192.168.30.66] run: hostname
  38. [192.168.30.66] out: sdn-testbed
  39. [192.168.30.66] out:
  40. [192.168.30.66] run: ifconfig
  41. [192.168.30.66] out: ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  42. [192.168.30.66] out: inet 192.168.74.128 netmask 255.255.255.0 broadcast 192.168.74.255
  43. [192.168.30.66] out: inet6 fe80::4dec:63ce:223b:a7cf prefixlen 64 scopeid 0x20<link>
  44. [192.168.30.66] out: ether 00:0c:29:68:44:26 txqueuelen 1000 (Ethernet)
  45. [192.168.30.66] out: RX packets 89251 bytes 115992608 (115.9 MB)
  46. [192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0
  47. [192.168.30.66] out: TX packets 27312 bytes 1844389 (1.8 MB)
  48. [192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  49. [192.168.30.66] out:
  50. [192.168.30.66] out: ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  51. [192.168.30.66] out: inet 192.168.30.66 netmask 255.255.255.0 broadcast 192.168.30.255
  52. [192.168.30.66] out: inet6 fe80::6094:b573:8d2f:dd5 prefixlen 64 scopeid 0x20<link>
  53. [192.168.30.66] out: ether 00:0c:29:68:44:30 txqueuelen 1000 (Ethernet)
  54. [192.168.30.66] out: RX packets 305 bytes 39864 (39.8 KB)
  55. [192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0
  56. [192.168.30.66] out: TX packets 230 bytes 38470 (38.4 KB)
  57. [192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  58. [192.168.30.66] out:
  59. [192.168.30.66] out: lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  60. [192.168.30.66] out: inet 127.0.0.1 netmask 255.0.0.0
  61. [192.168.30.66] out: inet6 ::1 prefixlen 128 scopeid 0x10<host>
  62. [192.168.30.66] out: loop txqueuelen 1000 (Local Loopback)
  63. [192.168.30.66] out: RX packets 373 bytes 28853 (28.8 KB)
  64. [192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0
  65. [192.168.30.66] out: TX packets 373 bytes 28853 (28.8 KB)
  66. [192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  67. [192.168.30.66] out:
  68. [192.168.30.66] out:
  69. [192.168.30.66] run: ls -l
  70. [192.168.30.66] out: total 48
  71. [192.168.30.66] out: drwxrwxr-x 2 beeworkshop beeworkshop 4096 424 21:20 bak
  72. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Desktop
  73. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Documents
  74. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Downloads
  75. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Music
  76. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Pictures
  77. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Public
  78. [192.168.30.66] out: drwxrwxr-x 5 beeworkshop beeworkshop 4096 425 01:09 sdnsoftware
  79. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Templates
  80. [192.168.30.66] out: drwxrwxr-t 2 beeworkshop beeworkshop 4096 425 18:23 thinclient_drives
  81. [192.168.30.66] out: drwxrwxr-x 3 beeworkshop beeworkshop 4096 424 23:33 tools
  82. [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 424 19:40 Videos
  83. [192.168.30.66] out:
  84. Done.
  85. Disconnecting from 192.168.30.66... done.

如果不是默认的文件fabfile.py,则必须用参数-f指出:

fab -f ~/test.py run_remote_cmds

指出默认的task。此时fab后边可以不必给出函数名,直接fab即可。

@task(default=True)

注意:\cmd 可以取消别名的作用。

例子1

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from fabric.api import *
  4. from fabric.colors import *
  5. from fabric.context_managers import *
  6. env.user = 'beeworkshop'
  7. env.hosts = ['192.168.30.66','192.168.30.67','192.168.30.68','192.168.30.69']
  8. env.password = '123456'
  9. @task
  10. def put_hosts_files():
  11. print yellow("rsync /etc/host file")
  12. with settings(warn_only=true):
  13. put("/etc/hosts","/etc/hosts")
  14. print green("rsync file success")
  15. for host in env.hosts
  16. env.host_string = host
  17. put_hosts_files()

这里@task装饰的函数可以多个,然后全部放在for循环中。fab会根据env.host_string的当前值逐个运行函数。

转载于:https://www.cnblogs.com/cerana/p/11117692.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/447462
推荐阅读
相关标签
  

闽ICP备14008679号