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

2 Answers 2

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
3
  • Thanks a lot. Looks like what I wanted. I am in the process of trying this. Will let you know what happens. Dec 11, 2014 at 16:49
  • How goes the battle @TheGovernor? Dec 12, 2014 at 0:49
  • Finally got to trying it. Works like charm thanks! Jan 30, 2015 at 1:06
0

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

1
  • I don't want to shell out to another process. Thanks though. Nov 5, 2014 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .