A summary of the survey on the introduction of Fabric in the system currently being viewed. Described with reference to Official Document. There seems to be a lot of things you can do, but you can't take over unless you keep it simple, so it's about the following. The version I tried is as follows.
Simple execution is executed in the following format
fab -f file name-H host name-u username execution method
Example: fab-f fab_test.py -H 192.168.1.10 -u ubuntu task_name
fab_test.py
from fabric.api import *
def task_name():
run("whoami")
sudo("whoami")
with cd("/tmp"):
run("pwd")
get Copy the file locally from the target server. Check the return value .succeeded to see if it was successful. .failed seems to store a list of file paths in case of failure.
get("Remote server path","Local path")
r = get('/home/foo/hoge', './')
print(r.succeeded) # =>If True, it succeeds. Fail if False
print(r.failed)
put Copy the file from local to the target server.
put('Local server path','Remote server path')
r = put('./hoge',"/home/foo")
print(r.succeeded) # =>If True, it succeeds. Fail if False
local Execute the command on the local server.
local("pwd")
run Execute the command on the remote server. The user who executes is the user who accessed.
run("whoami")
sudo sudo and execute the command. It will fail if the accessing user does not have sudo privileges.
sudo("whoami")
It is also possible to specify the execution user by describing as follows.
with settings(sudo_user='mysql'):
sudo("whoami") # prints 'mysql'
open_shell Migrate to the remote server shell and execute the command. Move to interactive as it is.
open_shell("ls")
prompt Accepts input from the user prompt. It seems that validation is also possible. When I entered a character string for the int type, an Exception occurred, so it seems better to validate it with a regular expression.
r = prompt("run command ?")
run(r)
# With validation, i.e. requiring integer input:
prompt('Please specify process nice level: ', key='nice', validate=int)
# With validation against a regular expression:
release = prompt('Please supply a release name',
validate=r'^\w+-\d+(\.\d+)?$')
Prefix all commands with cd XX &&
or YY &&. I couldn't put su-in front of the prefix, so use the settings mentioned above.
with cd("/etc"):
run("cat hosts")
with prefix("cd /etc"):
run("cat hosts")
Colors can be added by describing as follows. It seems easy to understand if you color success or failure. It is necessary to verify whether it will be colored with Teraterm.
from fabric.colors import *
print(red("This sentence is red, except for " +
green("these words, which are green") + "."))
The colors are as follows.
Execute the method only on the server of the target role.
from fabric.api import env
env.roledefs = {
'web': ['192.168.1.10', '192.168.1.11', '192.168.1.12']
}
@roles('web')
def restart_nginx():
sudo("service nginx restart ")
The execution is as follows. In the above, @roles ('web') is described before the method, so -R web can be omitted at runtime.
fab -f Executable file[-R roll] task
fab -f fab_test.py -u ubuntu restart_nginx
There was a procedure called "File is missing" in the procedure, and when I made a script like I did manually, it stopped there.
It can be avoided by inserting warn_only = True
as shown below.
with settings(warn_only=True):
result = sudo("ls -l /not/found/path")
[Other sites that may be helpful] https://blog.masu-mi.me/2015/04/11/fabric_tips.html
http://perezvon.hatenablog.com/entry/20091026/1256552181
Recommended Posts