Added file copy support w/ readme updates

This commit is contained in:
Michael DeHaan 2012-02-23 16:07:10 -05:00
parent 83d15afc6f
commit 3807824c6d
4 changed files with 54 additions and 21 deletions

View file

@ -41,8 +41,15 @@ The default inventory file (-H) is ~/.ansible_hosts and is a list
of all hostnames to target with ansible, one per line. These
can be hostnames or IPs
Example:
abc.example.com
def.example.com
192.168.10.50
192.168.10.51
This list is further filtered by the pattern wildcard (-P) to target
specific hosts.
specific hosts. This is covered below.
Comamnd line usage example
==========================
@ -51,36 +58,51 @@ Run a module by name with arguments
* ssh-agent bash
* ssh-add ~/.ssh/id_rsa.pub
* ansible -p "*.example.com" -m modName -a "arg1 arg2"
* ansible -p "*.example.com" -n modName -a "arg1 arg2"
API Example
===========
The API is simple and returns basic datastructures.
import ansible
runner = ansible.Runner(command='inventory', host_list=['xyz.example.com', '...'])
data = runner.run()
import ansible
runner = ansible.Runner(command='inventory', host_list=['xyz.example.com', '...'])
data = runner.run()
{
'xyz.example.com' : [ 'any kind of datastructure is returnable' ],
'foo.example.com' : None, # failed to connect,
...
}
{
'xyz.example.com' : [ 'any kind of datastructure is returnable' ],
'foo.example.com' : None, # failed to connect,
...
}
Additional options to runner include the number of forks, hostname
exclusion pattern, library path, and so on. Read the source, it's not
complicated.
Patterns
========
To target only hosts starting with "rtp", for example:
* ansible "rtp*" -n command -a "yum update apache"
Parallelism
===========
Specify the number of forks to use, to run things in greater parallelism.
* ansible -f 10 "*.example.com" -m modName -a "arg1 arg2"
* ansible -f 10 "*.example.com" -n command -a "yum update apache"
10 forks. The default is 3. 5 is right out.
File Transfer
=============
Yeah, it does that too.
* ansible -n copy -a "/etc/hosts /tmp/hosts"
Bundled Modules
===============
@ -119,8 +141,8 @@ Future plans
Author
======
Michael DeHaan <michael.dehaan@gmail.com>
Michael DeHaan <michael.dehaan@gmail.com>
http://michaeldehaan.net/
http://michaeldehaan.net/

View file

@ -35,10 +35,13 @@ class Cli(object):
options, args = parser.parse_args()
host_list = self._host_list(options.host_list)
# TODO: more shell like splitting on module_args would
# be a good idea
return ansible.Runner(
module_name=options.module_name,
module_path=options.module_path,
module_args=options.module_args,
module_args=options.module_args.split(' '),
host_list=host_list,
forks=options.forks,
pattern=options.pattern,

View file

@ -39,7 +39,7 @@ class Pooler(object):
class Runner(object):
def __init__(self, host_list=[], module_path=None,
module_name=None, module_args='',
module_name=None, module_args=[],
forks=3, timeout=60, pattern='*'):
self.host_list = host_list
@ -73,15 +73,22 @@ class Runner(object):
conn = self._connect(host)
if not conn:
return [ host, None ]
outpath = self._copy_module(conn)
self._exec_command(conn, "chmod +x %s" % outpath)
cmd = self._command(outpath)
result = self._exec_command(conn, cmd)
result = json.loads(result)
if self.module_name != "copy":
outpath = self._copy_module(conn)
self._exec_command(conn, "chmod +x %s" % outpath)
cmd = self._command(outpath)
result = self._exec_command(conn, cmd)
result = json.loads(result)
else:
ftp = conn.open_sftp()
ftp.put(self.module_args[0], self.module_args[1])
ftp.close()
return [ host, 1 ]
return [ host, result ]
def _command(self, outpath):
cmd = "%s %s" % (outpath, self.module_args)
cmd = "%s %s" % (outpath, " ".join(self.module_args))
return cmd
def _exec_command(self, conn, cmd):

1
library/copy Normal file
View file

@ -0,0 +1 @@
# copy is built-in to ansible's core, so the module here is just a placeholder