Search code examples
pythonfabricsalt-projectdevops

Can saltstack do something similar to Fabric?



I am new to saltstack. I have saltstack installed. I can run "sudo salt '*' test.ping" successfully. I am trying to use salt to replace Fabric. I have some functions/procedures in Fabric, which I would like salt to do the same.

def commit():
    message = raw_input("Enter a git commit message: ")
    local("git add . && git commit -am '{}'".format(message))

def push():
    local("git push origin master")

def prepare():
    commit()
    push()
  1. Can Salt be used to replace Fabric?
  2. How should I start? Which file should I modify or create in order to define those executions?
  3. Where do I place this definition file?
  4. How do I get the minion to run the defined function, e.g. "commit"?

Thanks for your help!


Solution

  • You can use Salt both for configuration management and ad-hoc orchestration tasks. What you're looking for are execution modules. Those are simple Python files that you can place the in the <file_root>/_modules directory on your master (typically something like /srv/salt/_modules). They will be picked up by the master automatically and distributed to the minions on-demand. You can then invoke them on a minion (either using salt '<minion-id>' on the master or using salt-call on the minion).

    In your case, you could create a file deploy.py in your modules directory with the following content:

    #!/usr/bin/python
    
    def commit(message, cwd):
        __salt__['cmd.run']("git add . && git commit -am '{}'".format(message),
                            cwd=cwd)
    
    def push(cwd):
        __salt__['cmd.run']("git push origin master",
                            cwd=cwd)
    
    def prepare(message, cwd):
        commit(message=message, cwd=cwd)
        push(cwd=cwd)
    

    The __salt__['cmd.run'] function that I'm using in my example is one of many already-built-in executions modules provided by Salt.

    The execution module needs to be placed on the master, but will be run on the minion. Besides using existing execution modules like cmd.run, you can unleash the full Python power in them.

    You could then call this script from the Salt master:

    $ salt 'minion-id' deploy.prepare message='Some commit message' cwd=/path/to/git
    

    Alternatively, you can also invoke it directly on the minion:

    $ salt-call deploy.prepare message='Some commit message' cwd=/path/to/git