1,760
edits
Changes
→Getting the hostname of the remote machine
== PART 1: Simplest example ==
===Getting the hostname of the remote machine===
: Add the following contents to the file called "fabfile.py" in your lab8 directory:<source lang="python">
from fabric.api import *
# set the name of the user on login to the remote host
env.user = 'student'
print("The host name is:",name)
</source>
: To check for syntax errorin 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 should get a list of tasks 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 your VM (replace with the actual port # for connecting to your VM), we run it under your account the fab command on matrix:<source lang="bash">[rchanraymond.chan@centos7 mtrx-node05pd lab8]$ fab -f fabfile.py -H 192hosts=myvmlab.168senecacollege.122.169 ca --port=7200 getHostname[192.168myvmlab.122senecacollege.169ca] Executing task 'getHostname'[192.168myvmlab.122senecacollege.169ca] run: hostname[192.168myvmlab.122senecacollege.169ca] out: c7-rchancentos7[192.168myvmlab.122senecacollege.169ca] out:
Done.
Disconnecting from 192myvmlab.168senecacollege.122ca:7200.169..done. done[raymond.chan@mtrx-node05pd lab8]$
</source>
:All this has done Notice that there is get 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 returned from the "hostname of the worker " command and print it (on the controller)out together with an descriptive text in a line. :In the command above weexecuted '''fab'''re using command, the fab program to import imports the file fabric script named "fabfile.py " and execute the getHostname function on the worker 192.168VM connect at port 7200 on myvmlab.122senecacollege.169ca. Note that the IP address of port number for your first worker will likely be of a differentvalue.
: If you did all the setup right and you get got a password prompt when execute the above command, read the prompt carefully and see who's password it prompted was prompting you for. If it is not for the same as your [seneca_id]user student, verify that you have the following line in your fabfile .py and you can ssh to your worker vm VM as the user student without password:
:<source lang="python">env.user = '[seneca_id]student'</source>
: In the above output from the '''fab''' command, you have::* Lines with an IP address telling the FQDN of the remote machine you which worker the output is for/fromare working on.:* Messages from the controller workstation (e.g. "Executing task...", and "run: ...").:* Output from the worker remote machine ("out: ..."):* Output generated on the controller workstation from your fab file ("worker1" which came from the "print()" callstatement)
:You should get used to the abovemessages from the '''fab''' command. It's a lot of output but it's important to understand where every part is coming from, so you are able to debug problems when they happen.
== Part 2: Set up more administrative tasks==