Changes

Jump to: navigation, search

OPS445 Online Lab8

32 bytes added, 08:29, 20 March 2022
m
INVESTIGATION 3: Running the fab command in script mode
= INVESTIGATION 3: Running the fab command in script mode =
: From investigation Investigation 2, we can see that running '''fab''' in ad-hoc mode is quick, straight forwardstraightforward, 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 executed on the remote machines, 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 under ops445 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' optinooption):
== PART 1: Non-privileged task example ==
===Create non-privileged tasks: Getting the hostname of remote machines===
: # Add the following contents to the default fabric script called "fabfile.py" in your lab8 directory:<source lang="python">
from fabric.api import *
# set Set the name of the user login to the remote host
env.user = 'student'
</source>
: # To check for syntax error in the fabric script, run the following command in the lab8 directory where it contains the fabric script named "fabfile.py":<source lang="bash">
fab -l
</source>
: you # You should get a list of tasks defined in your fabfile.py:<source lang="bash">[rchanraymond.chan@centos7 mtrx-node05pd lab8]$ fab -l
Available commands:
getHostname
</source>
: # To perform the task of getHostname on your VM (replace with the actual port # for connecting to your VM), run the fab command on matrix:<source lang="bash">
[raymond.chan@mtrx-node05pd lab8]$ fab --hosts=myvmlab.senecacollege.ca --port=7200 getHostname
[myvmlab.senecacollege.ca] Executing task 'getHostname'
[raymond.chan@mtrx-node05pd lab8]$
</source>
: #Notice that there is no need to specify the user name at the '''fab''' command line since we defined it in the fabric script file (env.user = 'student'). Also notice that we can capture the host name hostname returned from the "hostname" command and print it out together with an a descriptive text in a line. :#In the above executed '''fab''' command, the fab program imports the fabric script named "fabfile.py" and execute the getHostname function on the VM connect at port 7200 on myvmlab.senecacollege.ca. Note that the port number for your VM will likely has have a different value. : <br> If you did all the setup right and you got a password prompt when execute executing the above command, read the prompt carefully and see who's password it was prompting you for. If it is not for the user student, verify that you have the following line in your fabfile.py and you can ssh to your VM as the user student without password:
:<source lang="python">env.user = 'student'</source>
:* Output generated on the controller workstation from your fab file (the print statement)
:You should get used to the above messages from the '''fab''' command. It's a lot of output but it's important to understand where each piece of information is coming from, so you are able to debug problems when they happen.
== PART 2: Privileged Tasks Examples ==
===Creat Create privileged tasks: install and remove rpm package on remote machines===: # Add the following two new functions to the end of the fabric script "fabfile.py" in your lab8 directory:<source lang='bash'>
def installPackage(pkg='dummy'):
print(status)
def removePackage(pkg=''):
if pkg == '':
cmd = 'yum remove dummy -y'
print(status)
</source>
: # Note that both functions take one function argument in different ways. However, if no function argument is passed when calling the function, both will default to a string value of "dummy". Both functions call the sudo() from the fabric.api to execute the command contained in the "cmd" object on the remote machine via sudo.: # To check for any syntax error in your updated fabric script, run the following command in the same directory as the fabfile.py:<source lang='bash'>
fab -l
</source>
: # You should get a list of tasks defined similar to the following:<source lang='bash'>
[raymond.chan@mtrx-node05pd lab8]$ fab -l
Available commands:
[raymond.chan@mtrx-node05pd lab8]$
</source>
: # If you only need to connect to the same remote machine, you can specify the host and port number in the fabfile.py to save some typing when executing the fab command. Add the following two lines after the env.user line in your fabfile.py:<source lang='bash'>
env.port = '7200' # <-- please replace with the actual value of your VM's port number
env.hosts =['myvmlab.senecacollege.ca']
</source>
: # You can also store the user's password in this file so that it will respond to the "sudo password" prompt for sudo() call. It is not safe to do so as you can configure the sudo module on the remote machine not to ask for sudo password.: # Now you can run the fab command without the "--host" and "--port" optionoptions.: # Run the following two fab commands, note the results and compare their difference:<source lang='bash'>
fab installPackage
fab installPackage:tree
</source>
: # Run the following two fab commands, note the results and compare their difference:<source lang='bash'>
fab removePackage
572
edits

Navigation menu