0

I am using CentOS6.5. I can successfully copy a file from master to a minion with id=minionId by running the following command on the master:

salt minionId cp.get_file salt://fileInMaster.txt /home/johnDoe/fileNowInMinion.txt ##run on master 

If I have to accomplish the exact same thing using python how do I do it? Following this link all I find is a way to copy a file from master to minion by running a script on the minion. I want to run a python script on master that copies a file from master to a specific minion with given Id

1

Use the Python client API, or install and run salt-api which exposes a REST API. I'll detail the Python client API: http://salt.readthedocs.org/en/v2014.1.13/ref/clients/index.html

Script totin.py, copies /srv/salt/vim/vimrc.local to tin:/tmp/vimrc.local

#!/usr/bin/env python
import json
import salt.client

minion = 'tin'
source = 'salt://vim/vimrc.local'
target = '/tmp/vimrc.local'

local = salt.client.LocalClient()
ret = local.cmd('tin', 'cp.get_file', [source, target])
print json.dumps(ret, indent=2)

Test:

$ sudo python totin.py 
{
  "tin": "/tmp/vimrc.local"
}

Verify:

$ sudo salt tin cmd.run 'ls -l /tmp/vimrc.local'
tin:
    -rw-r--r-- 1 root root 652 Dec  4 20:45 /tmp/vimrc.local
  • Thanks a lot. Looks like what I wanted. I am in the process of trying this. Will let you know what happens. – The Governor Dec 11 '14 at 16:49
  • How goes the battle @TheGovernor? – Dan Garthwaite Dec 12 '14 at 0:49
  • Finally got to trying it. Works like charm thanks! – The Governor Jan 30 '15 at 1:06
0

You can wrap your salt command in python using pexpect or some shell invocation module (os.system, subprocess.call, etc.)

  • I don't want to shell out to another process. Thanks though. – The Governor Nov 5 '14 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.