[PYTHON] Idempotent guarantee with deployment tool fabric + cuisine


Python's deployment tool fabric makes shell scripts convenient to use in Python, and can easily perform Chef-Solo-like configuration management. In addition, idempotency, which is one of the major features of Chef, can be guaranteed by using cuisine, which is a wrapper for fabric.

It is very simple, the learning cost is low compared to Chef etc., and it can be installed easily because it only needs to be installed on the target machine. This time I wrote a little script to place dotfiles in vagrant using fabric.


It can be installed with one command below.

$ pip install fabric cuisine

How to use

Specify the host of the target machine in env.hosts, enclose the command in run (), and execute it. If you want to execute it with sudo, enclose it in sudo (). This is the only basic.


# -*- encoding:utf-8 -*-

from fabric.api import env
from fabric.decorators import task
from cuisine import run

env.hosts = ['']

def A():
  run('echo A')

def B():
  sudo('echo B')

def main():

If you execute> main, you can see that it can be executed on the target machine as follows.

$ fab main
[] run: echo A
[] out: A
[] run: echo B
[] out: B

Disconnecting from done.

Idempotency can be guaranteed by using the _ensure () method such as package_ensure () and the _exisits () method such as dir_exists ().

package_ensure('vim') #Install only if vim is not installed

vagrant integration

To link with vagrant, execute the following command.

$ vagrant plugin install vagrant-fabric 

Then, describe the following settings in the Vagrantfile.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.provision :fabric do |fabric|
    fabric.fabfile_path = './fabfile.py'
    fabric.tasks = ['set_env'] #Task you want to perform

This time, I wrote a file to place the following dotfile.


# -*- encoding:utf-8 -*-

from fabric.api import env, settings
from fabric.decorators import task
from cuisine import mode_sudo, select_package, run, package_ensure, dir_exists, cd


def set_env():

def setup_packages():
  with settings(mode_sudo()):

def fetch_dotfiles():
  if not dir_exists('dotfiles'):
    run('git clone --recursive [email protected]:pika-shi/dotfiles.git')

def set_symlinks():
  path_pair_list = [
    ('dotfiles/_zshrc', '.zshrc'), ('dotfiles/_tmux.conf', '.tmux.conf'),
    ('dotfiles/_vimrc', '.vimrc'), ('dotfiles/_vim', '.vim')
  with cd('~/'):
    for path_set in path_pair_list:
      run('ln -s {} {}'.format(*path_set))

If you execute the following command with vagrant up, the task will be executed and the appropriate symbolic link will be attached to the dotfile.

$ vagrant provision

in conclusion

In this way, a little configuration management can be done very easily, so please try it and realize its convenience.

Recommended Posts

Idempotent guarantee with deployment tool fabric + cuisine
First Fabric (Python deployment tool)
Giving idempotence to fabric with cuisine
Easy deployment environment with gaffer + fabric
Why we chose Fabric as our deployment tool
Create a deploy script with fabric and cuisine and reuse it
[Linux] Write a deployment tool using rsync with a shell script