From 3807824c6d0dae63b9f36dbafe8e100b0a3beaa6 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Thu, 23 Feb 2012 16:07:10 -0500 Subject: [PATCH] Added file copy support w/ readme updates --- README.md | 48 ++++++++++++++++++++++++++++++----------- bin/ansible | 5 ++++- lib/ansible/__init__.py | 21 ++++++++++++------ library/copy | 1 + 4 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 library/copy diff --git a/README.md b/README.md index 6989e12ff8c..37fcb5faf9a 100644 --- a/README.md +++ b/README.md @@ -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 -http://michaeldehaan.net/ + http://michaeldehaan.net/ diff --git a/bin/ansible b/bin/ansible index 537a60e78a3..386b19b0a3c 100755 --- a/bin/ansible +++ b/bin/ansible @@ -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, diff --git a/lib/ansible/__init__.py b/lib/ansible/__init__.py index 26f3af508bc..09332da8cfe 100755 --- a/lib/ansible/__init__.py +++ b/lib/ansible/__init__.py @@ -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): diff --git a/library/copy b/library/copy new file mode 100644 index 00000000000..38ea7d7cedd --- /dev/null +++ b/library/copy @@ -0,0 +1 @@ +# copy is built-in to ansible's core, so the module here is just a placeholder