Fabric's official website:
http://www.fabfile.org
Help documents:
https://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html
- Fabric installation
yum install -y make gcc gcc++ python-devel python-pip $ pip install fabric==1.14.0 //perhaps $ pip3 install fabric2 (No, fabric.api Module)
- Verify installation
python >>> import fabric //perhaps python3 >>> import fabric
- Command line entry fab
fab [options] -- [shell command]
Core API of Fabric
Core API | category |
---|---|
Output class with color | color output |
Context management class | context managers |
Decoration class | decorators |
Network class | network |
Operation class | operations |
Task class | tasks |
Tool class | utils |
fabric.api command set
- Usage method
$ cat fabfile.py ---------------------------------- #!/usr/bin/python # -*- coding: utf-8 -*- from fabric.api import * from fabric.colors import * from fabric.context_managers import * env.user = 'beeworkshop' env.hosts = ['192.168.30.66'] env.password = '123456' @task def run_remote_cmds(): print yellow("I'm going to execute the order") with settings(warn_only=True): <---Errors continue local("hostname") local("uname -a") run("w") run("hostname") run("ifconfig") run("ls -l") --------------------------------- $ fab -l Available commands: run_remote_cmds $ fab run_remote_cmds [192.168.30.66] Executing task 'run_remote_cmds' //I'm going to execute the order [localhost] local: hostname bee-a [localhost] local: uname -a 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 [192.168.30.66] run: w [192.168.30.66] out: 01:04:50 up 1:35, 1 user, load average: 0.00, 0.00, 0.00 [192.168.30.66] out: USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT [192.168.30.66] out: beeworks pts/0 192.168.30.6 01:04 0.00s 0.02s 0.02s w [192.168.30.66] out: [192.168.30.66] run: hostname [192.168.30.66] out: sdn-testbed [192.168.30.66] out: [192.168.30.66] run: ifconfig [192.168.30.66] out: ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 [192.168.30.66] out: inet 192.168.74.128 netmask 255.255.255.0 broadcast 192.168.74.255 [192.168.30.66] out: inet6 fe80::4dec:63ce:223b:a7cf prefixlen 64 scopeid 0x20<link> [192.168.30.66] out: ether 00:0c:29:68:44:26 txqueuelen 1000 (Ethernet) [192.168.30.66] out: RX packets 89251 bytes 115992608 (115.9 MB) [192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0 [192.168.30.66] out: TX packets 27312 bytes 1844389 (1.8 MB) [192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [192.168.30.66] out: [192.168.30.66] out: ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 [192.168.30.66] out: inet 192.168.30.66 netmask 255.255.255.0 broadcast 192.168.30.255 [192.168.30.66] out: inet6 fe80::6094:b573:8d2f:dd5 prefixlen 64 scopeid 0x20<link> [192.168.30.66] out: ether 00:0c:29:68:44:30 txqueuelen 1000 (Ethernet) [192.168.30.66] out: RX packets 305 bytes 39864 (39.8 KB) [192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0 [192.168.30.66] out: TX packets 230 bytes 38470 (38.4 KB) [192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [192.168.30.66] out: [192.168.30.66] out: lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 [192.168.30.66] out: inet 127.0.0.1 netmask 255.0.0.0 [192.168.30.66] out: inet6 ::1 prefixlen 128 scopeid 0x10<host> [192.168.30.66] out: loop txqueuelen 1000 (Local Loopback) [192.168.30.66] out: RX packets 373 bytes 28853 (28.8 KB) [192.168.30.66] out: RX errors 0 dropped 0 overruns 0 frame 0 [192.168.30.66] out: TX packets 373 bytes 28853 (28.8 KB) [192.168.30.66] out: TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [192.168.30.66] out: [192.168.30.66] out: [192.168.30.66] run: ls -l [192.168.30.66] out: total 48 [192.168.30.66] out: drwxrwxr-x 2 beeworkshop beeworkshop 4096 4 Month 2421:20 bak [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Desktop [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Documents [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Downloads [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Music [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Pictures [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Public [192.168.30.66] out: drwxrwxr-x 5 beeworkshop beeworkshop 4096 4 Month 2501:09 sdnsoftware [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Templates [192.168.30.66] out: drwxrwxr-t 2 beeworkshop beeworkshop 4096 4 Month 2518:23 thinclient_drives [192.168.30.66] out: drwxrwxr-x 3 beeworkshop beeworkshop 4096 4 Month 2423:33 tools [192.168.30.66] out: drwxr-xr-x 2 beeworkshop beeworkshop 4096 4 Month 2419:40 Videos [192.168.30.66] out: Done. Disconnecting from 192.168.30.66... done.
If it is not the default file, failfile.py, you must indicate with the parameter - f:
fab -f ~/test.py run_remote_cmds
Indicates the default task. In this case, it is not necessary to give the function name after the Fab, but it can be directly fab.
@task(default=True)
Note: cmd can disable aliases.
Example 1
#!/usr/bin/python # -*- coding: utf-8 -*- from fabric.api import * from fabric.colors import * from fabric.context_managers import * env.user = 'beeworkshop' env.hosts = ['192.168.30.66','192.168.30.67','192.168.30.68','192.168.30.69'] env.password = '123456' @task def put_hosts_files(): print yellow("rsync /etc/host file") with settings(warn_only=true): put("/etc/hosts","/etc/hosts") print green("rsync file success") for host in env.hosts env.host_string = host put_hosts_files()
Here, there are multiple functions decorated by @ task, and all of them are put in the for loop. fab will run the functions one by one based on the current value of env.host'string.