1,760
edits
Changes
→INVESTIGATION 3: Running fab in script mode
= INVESTIGATION 3: Running fab in script mode =
: From investigation 2, we can see that run running '''fab''' in ad-hoc more mode is quick, straight forward, and easy. However, the rich output generated can not be easily captured and processed. If you have a need to capture and process the output generated by the commands execute executed on the remote machinemachines, the solution is to run the '''fab ''' command in script mode.: The first step in running the '''fab ''' command in script mode, is to create a fabric script file.
: Let's start with a simple fabric script file to demonstrate some basic concepts that use the API from the Fabric python library.
: On matrix, cd to your lab8 directory and create a simple fabric script file named '''fabfile.py''' (this is the default filename used by the fab command when you invoke it without the '-f' optino):
== PART 1: Simplest example ==
===Getting the hostname on the remote worker===
: Add the following to the file called "fabfile.py" in your lab8 directory: <source lang="python">from fabric.api import *
# set the name of the user on the remote host
env.user = '[seneca_id]student'
# Will Define task to get the hostname of this workerthe remote machine:
def getHostname():
name = run("hostname")
print("The host name is:",name)
</source>
: To check for syntax error, run the following command in the same lab8 directory as your where it contains the fabric script named "fabfile.py":<source lang="bash">
fab -l
</source>
: you should get a list of tasks stored defined in your fabfile.py:<source lang="bash">
[rchan@centos7 lab8]$ fab -f fabfile.py -l
Available commands:
getHostname
</source>
: To perform the task of getHostname on the worker machine 192.168.122.169 your VM (replace with the actual IP of port # for connecting to your worker VM), we run it under your account on the controller machine like thismatrix:<source lang="bash">
[rchan@centos7 lab8]$ fab -f fabfile.py -H 192.168.122.169 getHostname
[192.168.122.169] Executing task 'getHostname'