Using sudo in Fabric
I’m using Xubuntu 12.04.03 32bit
If you haven’t seen my previous tutorial you should see it:
Fabric hello world
In this tutorial I’m using sudo to copy logs.
Create folder and fabfile.py.
$ mkdir sudofabric && cd sudofabric
$ nano fabfile.py
Create two methods: super and superfolder.
from fabric.api import env, run, get, sudo
env.hosts=["soivite01@localhost", "soivite02@localhost",
"soivite03@localhost", "soivite04@localhost"]
#env.password="password"
env.parallel=True
def super():
sudo("cp /var/log/syslog $HOME")
me = run("whoami")
sudo("chown -R "+me+" /home/"+me)
get("syslog")
def superfolder():
sudo("cp -r /var/log/apt/ $HOME")
me = run("whoami")
sudo("chown -R "+me+" /home/"+me)
get("apt")
Modify sudoers file. (WARNING: modifying wrong or giving rights to wrong person could compromise your computer)
$ sudo visudo
Because I don’t want to give password everytime running fabfile I give rights to all 4 user to run sudo without password. I added these lines to end of sudoers file
soivite01 ALL=(ALL) NOPASSWD: ALL
soivite02 ALL=(ALL) NOPASSWD: ALL
soivite03 ALL=(ALL) NOPASSWD: ALL
soivite04 ALL=(ALL) NOPASSWD: ALL
Run super command.
$ fab super
[soivite01@localhost] Executing task 'super'
[soivite01@localhost] sudo: cp /var/log/syslog $HOME
[soivite01@localhost] run: whoami
[soivite01@localhost] out: soivite01
[soivite01@localhost] sudo: chown -R soivite01 /home/soivite01
[soivite01@localhost] download: /home/xubuntu/fabric/soivite01@localhost/syslog <- /home/soivite01/syslog
...
Run superfolder command.
$ fab superfolder
Your folder tree should look like this.
sudofabric/
├── fabfile.py
├── fabfile.pyc
├── soivite01@localhost
│ ├── apt
│ │ ├── history.log
│ │ └── term.log
│ └── syslog
├── soivite02@localhost
│ ├── apt
│ │ ├── history.log
│ │ └── term.log
│ └── syslog
├── soivite03@localhost
│ ├── apt
│ │ ├── history.log
│ │ └── term.log
│ └── syslog
└── soivite04@localhost
├── apt
│ ├── history.log
│ └── term.log
└── syslog