Other useful features in Fabric
Fabric has other useful features, such as roles and context managers.
Fabric roles
Fabric can define roles for hosts, and run only the tasks to role members. For example, we might have a bunch of database servers on which we need to validate whether the MySql service is up, and other web servers on which we need to validate whether the Apache service is up. We can group these hosts into roles, and execute functions based on those roles:
#!/usr/bin/python __author__ = "Bassim Aly" __EMAIL__ = "[email protected]" from fabric.api import * env.hosts = [ '10.10.10.140', # ubuntu machine '10.10.10.193', # CentOS machine '10.10.10.130', ] env.roledefs = { 'webapps': ['10.10.10.140','10.10.10.193'], 'databases': ['10.10.10.130'], } env.user = "root" env.password = "access123" @roles('databases') def validate_mysql(): output = run("systemctl status mariadb") @roles('webapps') def validate_apache(): output = run("systemctl status httpd")
In the preceding...