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

The Governor
  • 153
  • 7

2 Answers2

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
Dan Garthwaite
  • 2,922
  • 18
  • 29
0

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

f01
  • 406
  • 4
  • 8