From 4b4e0926bb68556820dc5b8a7db75f234b9d1cf5 Mon Sep 17 00:00:00 2001 From: David Stygstra Date: Wed, 18 Sep 2013 19:28:41 -0400 Subject: [PATCH 01/18] Modules for managing Open vSwitch bridges and ports --- library/net_infrastructure/openvswitch_bridge | 129 +++++++++++++++++ library/net_infrastructure/openvswitch_port | 133 ++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 library/net_infrastructure/openvswitch_bridge create mode 100644 library/net_infrastructure/openvswitch_port diff --git a/library/net_infrastructure/openvswitch_bridge b/library/net_infrastructure/openvswitch_bridge new file mode 100644 index 0000000000..2c87aacd39 --- /dev/null +++ b/library/net_infrastructure/openvswitch_bridge @@ -0,0 +1,129 @@ +#!/usr/bin/env python2 +#coding: utf-8 -*- + +# This module is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this software. If not, see . + +DOCUMENTATION = ''' +--- +module: openvswitch_bridge +short_description: Manage Open vSwitch bridges +requirements: [ ovs-vsctl ] +description: + - Manage Open vSwitch bridges +options: + bridge: + required: true + description: + - Name of bridge to manage + state: + required: false + default: "present" + choices: [ present, absent ] + description: + - Whether the bridge should exist + timeout: + required: false + default: 5 + description: + - How long to wait for ovs-vswitchd to respond +''' + +EXAMPLES = ''' +# Create a bridge named br-int +- openvswitch_bridge: bridge=br-int state=present +''' + + +class OVSBridge(object): + def __init__(self, module): + self.module = module + self.bridge = module.params['bridge'] + self.state = module.params['state'] + self.timeout = module.params['timeout'] + + def _vsctl(self, command): + '''Run ovs-vsctl command''' + return self.module.run_command(['ovs-vsctl', '-t', str(self.timeout)] + command) + + def exists(self): + '''Check if the bridge already exists''' + rc, _, err = self._vsctl(['br-exists', self.bridge]) + if rc == 0: # See ovs-vsctl(8) for status codes + return True + if rc == 2: + return False + raise Exception(err) + + def add(self): + '''Create the bridge''' + rc, _, err = self._vsctl(['add-br', self.bridge]) + if rc != 0: + raise Exception(err) + + def delete(self): + '''Delete the bridge''' + rc, _, err = self._vsctl(['del-br', self.bridge]) + if rc != 0: + raise Exception(err) + + def check(self): + '''Run check mode''' + try: + if self.state == 'absent' and self.exists(): + changed = True + elif self.state == 'present' and not self.exists(): + changed = True + else: + changed = False + except Exception, e: + self.module.fail_json(msg=str(e)) + self.module.exit_json(changed=changed) + + def run(self): + '''Make the necessary changes''' + result = {'changed': False} + try: + if self.state == 'absent': + if self.exists(): + self.delete() + result['changed'] = True + elif self.state == 'present': + if not self.exists(): + self.add() + result['changed'] = True + except Exception, e: + self.module.fail_json(msg=str(e)) + self.module.exit_json(**result) + + +def main(): + module = AnsibleModule( + argument_spec={ + 'bridge': {'required': True}, + 'state': {'default': 'present', 'choices': ['present', 'absent']}, + 'timeout': {'default': 5, 'type': 'int'} + }, + supports_check_mode=True, + ) + + br = OVSBridge(module) + if module.check_mode: + br.check() + else: + br.run() + + +# this is magic, see lib/ansible/module_common.py +#<> +main() diff --git a/library/net_infrastructure/openvswitch_port b/library/net_infrastructure/openvswitch_port new file mode 100644 index 0000000000..3bde7f9a5c --- /dev/null +++ b/library/net_infrastructure/openvswitch_port @@ -0,0 +1,133 @@ +#!/usr/bin/env python2 +#coding: utf-8 -*- + +# This module is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this software. If not, see . + +DOCUMENTATION = ''' +--- +module: openvswitch_port +short_description: Manage Open vSwitch ports +requirements: [ ovs-vsctl ] +description: + - Manage Open vSwitch ports +options: + bridge: + required: true + description: + - Name of bridge to manage + port: + required: true + description: + - Name of port to manage on the bridge + state: + required: false + default: "present" + choices: [ present, absent ] + description: + - Whether the port should exist + timeout: + required: false + default: 5 + description: + - How long to wait for ovs-vswitchd to respond +''' + +EXAMPLES = ''' +# Creates port eth2 on bridge br-ex +- openvswitch_port: bridge=br-ex port=eth2 state=present +''' + + +class OVSPort(object): + def __init__(self, module): + self.module = module + self.bridge = module.params['bridge'] + self.port = module.params['port'] + self.state = module.params['state'] + self.timeout = module.params['timeout'] + + def _vsctl(self, command): + '''Run ovs-vsctl command''' + return self.module.run_command(['ovs-vsctl', '-t', str(self.timeout)] + command) + + def exists(self): + '''Check if the port already exists''' + rc, out, err = self._vsctl(['list-ports', self.bridge]) + if rc != 0: + raise Exception(err) + return any(port.rstrip() == self.port for port in out.split('\n')) + + def add(self): + '''Add the port''' + rc, _, err = self._vsctl(['add-port', self.bridge, self.port]) + if rc != 0: + raise Exception(err) + + def delete(self): + '''Remove the port''' + rc, _, err = self._vsctl(['del-port', self.bridge, self.port]) + if rc != 0: + raise Exception(err) + + def check(self): + '''Run check mode''' + try: + if self.state == 'absent' and self.exists(): + changed = True + elif self.state == 'present' and not self.exists(): + changed = True + else: + changed = False + except Exception, e: + self.module.fail_json(msg=str(e)) + self.module.exit_json(changed=changed) + + def run(self): + '''Make the necessary changes''' + result = {'changed': False} + try: + if self.state == 'absent': + if self.exists(): + self.delete() + result['changed'] = True + elif self.state == 'present': + if not self.exists(): + self.add() + result['changed'] = True + except Exception, e: + self.module.fail_json(msg=str(e)) + self.module.exit_json(**result) + + +def main(): + module = AnsibleModule( + argument_spec={ + 'bridge': {'required': True}, + 'port': {'required': True}, + 'state': {'default': 'present', 'choices': ['present', 'absent']}, + 'timeout': {'default': 5, 'type': 'int'} + }, + supports_check_mode=True, + ) + + port = OVSPort(module) + if module.check_mode: + port.check() + else: + port.run() + + +# this is magic, see lib/ansible/module_common.py +#<> +main() From b5ae76107041caa5289d2ce0500ca34cd2d0829c Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Fri, 27 Sep 2013 13:21:55 +0200 Subject: [PATCH 02/18] Add support for tags parameter to cloudformation module Expose boto.cloudformation.create_stack() tags parameter. Supplied tags will be applied to stack and all it's resources on stack creation. Cannot be updated later (not supported by UpdateStack CloudFormation API). --- library/cloud/cloudformation | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/library/cloud/cloudformation b/library/cloud/cloudformation index ae9f2e0f26..955278406b 100644 --- a/library/cloud/cloudformation +++ b/library/cloud/cloudformation @@ -60,6 +60,12 @@ options: required: true default: null aliases: [] + tags: + description: + - Dictionary of tags to associate with stack and it's resources during stack creation. Cannot be updated later. + required: false + default: null + aliases: [] requirements: [ "boto" ] author: James S. Martin @@ -79,6 +85,8 @@ tasks: DiskType: ephemeral InstanceType: m1.small ClusterSize: 3 + tags: + Stack: ansible-cloudformation ''' import json @@ -163,7 +171,8 @@ def main(): region=dict(aliases=['aws_region', 'ec2_region'], required=True, choices=AWS_REGIONS), state=dict(default='present', choices=['present', 'absent']), template=dict(default=None, required=True), - disable_rollback=dict(default=False) + disable_rollback=dict(default=False), + tags=dict(default=None) ) ) @@ -173,14 +182,15 @@ def main(): template_body = open(module.params['template'], 'r').read() disable_rollback = module.params['disable_rollback'] template_parameters = module.params['template_parameters'] + tags = module.params['tags'] if not r: if 'AWS_REGION' in os.environ: r = os.environ['AWS_REGION'] elif 'EC2_REGION' in os.environ: r = os.environ['EC2_REGION'] - - + + # convert the template parameters ansible passes into a tuple for boto template_parameters_tup = [(k, v) for k, v in template_parameters.items()] @@ -203,7 +213,8 @@ def main(): cfn.create_stack(stack_name, parameters=template_parameters_tup, template_body=template_body, disable_rollback=disable_rollback, - capabilities=['CAPABILITY_IAM']) + capabilities=['CAPABILITY_IAM'], + tags=tags) operation = 'CREATE' except Exception, err: error_msg = boto_exception(err) From d294669dec309a2a425e53221d0a314c617d4b3c Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Mon, 30 Sep 2013 09:25:20 +0200 Subject: [PATCH 03/18] Add Boto version check for tags parameter of cloudformation module Tags parameter requires at least version 2.6.0 of Boto module. When tags parameter is used with older version, error is raised. When tags parameter is unused, module works as before. --- library/cloud/cloudformation | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/library/cloud/cloudformation b/library/cloud/cloudformation index 955278406b..164f306854 100644 --- a/library/cloud/cloudformation +++ b/library/cloud/cloudformation @@ -63,6 +63,7 @@ options: tags: description: - Dictionary of tags to associate with stack and it's resources during stack creation. Cannot be updated later. + Requires at least Boto version 2.6.0. required: false default: null aliases: [] @@ -93,6 +94,7 @@ import json import time try: + import boto import boto.cloudformation.connection except ImportError: print "failed=True msg='boto required for this module'" @@ -126,6 +128,17 @@ def boto_exception(err): return error +def boto_version_required(version_tuple): + parts = boto.Version.split('.') + boto_version = [] + try: + for part in parts: + boto_version.append(int(part)) + except: + boto_version.append(-1) + return tuple(boto_version) >= tuple(version_tuple) + + def stack_operation(cfn, stack_name, operation): '''gets the status of a stack while it is created/updated/deleted''' existed = [] @@ -190,6 +203,11 @@ def main(): elif 'EC2_REGION' in os.environ: r = os.environ['EC2_REGION'] + kwargs = dict() + if tags is not None: + if not boto_version_required((2,6,0)): + module.fail_json(msg='Module parameter "tags" requires at least Boto version 2.6.0') + kwargs['tags'] = tags # convert the template parameters ansible passes into a tuple for boto @@ -214,7 +232,7 @@ def main(): template_body=template_body, disable_rollback=disable_rollback, capabilities=['CAPABILITY_IAM'], - tags=tags) + **kwargs) operation = 'CREATE' except Exception, err: error_msg = boto_exception(err) From 5e4fff98f2da76cf4499c76809976c3d06e6dc10 Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Mon, 30 Sep 2013 09:28:53 +0200 Subject: [PATCH 04/18] Add version_added to cloudformation tags parameter --- library/cloud/cloudformation | 1 + 1 file changed, 1 insertion(+) diff --git a/library/cloud/cloudformation b/library/cloud/cloudformation index 164f306854..8c80fa75c1 100644 --- a/library/cloud/cloudformation +++ b/library/cloud/cloudformation @@ -67,6 +67,7 @@ options: required: false default: null aliases: [] + version_added: "1.4" requirements: [ "boto" ] author: James S. Martin From aa496e36a22fb8054a2e678d609f3128237a4216 Mon Sep 17 00:00:00 2001 From: David Stygstra Date: Sun, 13 Oct 2013 12:35:53 -0400 Subject: [PATCH 05/18] Minor style change: removed unnecessary dictionary --- library/net_infrastructure/openvswitch_bridge | 8 ++++---- library/net_infrastructure/openvswitch_port | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/library/net_infrastructure/openvswitch_bridge b/library/net_infrastructure/openvswitch_bridge index 2c87aacd39..7d9824c54b 100644 --- a/library/net_infrastructure/openvswitch_bridge +++ b/library/net_infrastructure/openvswitch_bridge @@ -92,19 +92,19 @@ class OVSBridge(object): def run(self): '''Make the necessary changes''' - result = {'changed': False} + changed = False try: if self.state == 'absent': if self.exists(): self.delete() - result['changed'] = True + changed = True elif self.state == 'present': if not self.exists(): self.add() - result['changed'] = True + changed = True except Exception, e: self.module.fail_json(msg=str(e)) - self.module.exit_json(**result) + self.module.exit_json(changed=changed) def main(): diff --git a/library/net_infrastructure/openvswitch_port b/library/net_infrastructure/openvswitch_port index 3bde7f9a5c..bfa7a9caa4 100644 --- a/library/net_infrastructure/openvswitch_port +++ b/library/net_infrastructure/openvswitch_port @@ -95,19 +95,19 @@ class OVSPort(object): def run(self): '''Make the necessary changes''' - result = {'changed': False} + changed = False try: if self.state == 'absent': if self.exists(): self.delete() - result['changed'] = True + changed = True elif self.state == 'present': if not self.exists(): self.add() - result['changed'] = True + changed = True except Exception, e: self.module.fail_json(msg=str(e)) - self.module.exit_json(**result) + self.module.exit_json(changed=changed) def main(): From bd5cd8e652adcedcd235ff49ebfa9f7d49462901 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 24 Oct 2013 10:56:53 +0200 Subject: [PATCH 06/18] Lookup plugin for etcd with support for configurable etcd URL in ansible.cfg (and environment) --- examples/ansible.cfg | 3 + lib/ansible/constants.py | 3 + lib/ansible/runner/lookup_plugins/etcd.py | 73 +++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 lib/ansible/runner/lookup_plugins/etcd.py diff --git a/examples/ansible.cfg b/examples/ansible.cfg index a9306a6119..2a1e668712 100644 --- a/examples/ansible.cfg +++ b/examples/ansible.cfg @@ -102,6 +102,9 @@ filter_plugins = /usr/share/ansible_plugins/filter_plugins # set to 1 if you don't want colors, or export ANSIBLE_NOCOLOR=1 #nocolor = 1 +# default URL for `etcd' lookup plugin +etcd_url = 'http://127.0.0.1:4001' + [paramiko_connection] # uncomment this line to cause the paramiko connection plugin to not record new host diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index a6b77050ca..e8d50beeea 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -148,6 +148,9 @@ ACCELERATE_TIMEOUT = get_config(p, 'accelerate', 'accelerate_timeout ACCELERATE_CONNECT_TIMEOUT = get_config(p, 'accelerate', 'accelerate_connect_timeout', 'ACCELERATE_CONNECT_TIMEOUT', 1.0, floating=True) PARAMIKO_PTY = get_config(p, 'paramiko_connection', 'pty', 'ANSIBLE_PARAMIKO_PTY', True, boolean=True) +# LOOKUP PLUGIN RELATED +ANSIBLE_ETCD_URL = get_config(p, DEFAULTS, 'etcd_url', 'ANSIBLE_ETCD_URL', 'http://127.0.0.1:4001') + # non-configurable things DEFAULT_SUDO_PASS = None diff --git a/lib/ansible/runner/lookup_plugins/etcd.py b/lib/ansible/runner/lookup_plugins/etcd.py new file mode 100644 index 0000000000..63726cdd5f --- /dev/null +++ b/lib/ansible/runner/lookup_plugins/etcd.py @@ -0,0 +1,73 @@ +# (c) 2013, Jan-Piet Mens +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +from ansible import utils, errors, constants +import os +import urllib2 +try: + import json +except ImportError: + import simplejson as json + +class etcd(): + def __init__(self, url='http://127.0.0.1:4001'): + self.url = url + self.baseurl = '%s/v1/keys' % (self.url) + + def get(self, key): + url = "%s/%s" % (self.baseurl, key) + + data = None + value = "" + try: + r = urllib2.urlopen(url) + data = r.read() + except: + return value + + try: + # {"action":"get","key":"/name","value":"Jane Jolie","index":5} + item = json.loads(data) + if 'value' in item: + value = item['value'] + if 'errorCode' in item: + value = "ENOENT" + except: + raise + pass + + return value + +class LookupModule(object): + + def __init__(self, basedir=None, **kwargs): + self.basedir = basedir + self.etcd = etcd(constants.ANSIBLE_ETCD_URL) + + def run(self, terms, inject=None, **kwargs): + + terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) + + if isinstance(terms, basestring): + terms = [ terms ] + + ret = [] + for term in terms: + key = term.split()[0] + value = self.etcd.get(key) + ret.append(value) + return ret From ac40d1512017518044640cc4b8427668c9db914c Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 30 Oct 2013 21:39:43 -0400 Subject: [PATCH 07/18] now assemble module is also action plugin and can use local source for files Signed-off-by: Brian Coca --- lib/ansible/runner/action_plugins/assemble.py | 97 +++++++++++++++++++ library/files/assemble | 15 ++- 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 lib/ansible/runner/action_plugins/assemble.py diff --git a/lib/ansible/runner/action_plugins/assemble.py b/lib/ansible/runner/action_plugins/assemble.py new file mode 100644 index 0000000000..73cb86f953 --- /dev/null +++ b/lib/ansible/runner/action_plugins/assemble.py @@ -0,0 +1,97 @@ +# (c) 2013, Michael DeHaan +# Stephen Fromm +# Brian Coca +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License + +import os +import os.path +import pipes +import shutil +import tempfile +from ansible import utils + +class ActionModule(object): + + def __init__(self, runner): + self.runner = runner + + def _assemble_from_fragments(src_path, delimiter=None): + ''' assemble a file from a directory of fragments ''' + tmpfd, temp_path = tempfile.mkstemp() + tmp = os.fdopen(tmpfd,'w') + delimit_me = False + for f in sorted(os.listdir(src_path)): + fragment = "%s/%s" % (src_path, f) + if delimit_me and delimiter: + tmp.write(delimiter) + if os.path.isfile(fragment): + tmp.write(file(fragment).read()) + delimit_me = True + tmp.close() + return temp_path + + def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): + + # load up options + options = {} + if complex_args: + options.update(complex_args) + options.update(utils.parse_kv(module_args)) + + src = options.get('src', None) + dest = options.get('dest', None) + delimiter = options.get('delimiter', None) + remote_src = options.get('remote_src', True) + + if src is None or dest is None: + result = dict(failed=True, msg="src and dest are required") + return ReturnData(conn=conn, comm_ok=False, result=result) + + if remote_src: + return self.runner._execute_module(conn, tmp, 'assemble', module_args, inject=inject, complex_args=complex_args) + + # Does all work assembling the file + path = assemble_from_fragments(src, delimiter) + + pathmd5 = utils.md5s(path) + remote_md5 = self.runner._remote_md5(conn, tmp, dest) + + if pathmd5 != remote_md5: + if self.runner.diff: + dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, inject=inject, persist_files=True) + if 'content' in dest_result.result: + dest_contents = dest_result.result['content'] + if dest_result.result['encoding'] == 'base64': + dest_contents = base64.b64decode(dest_contents) + else: + raise Exception("unknown encoding, failed: %s" % dest_result.result) + xfered = self.runner._transfer_str(conn, tmp, 'src', resultant) + + # fix file permissions when the copy is done as a different user + if self.runner.sudo and self.runner.sudo_user != 'root': + self.runner._low_level_exec_command(conn, "chmod a+r %s" % xfered, tmp) + + # run the copy module + module_args = "%s src=%s dest=%s original_basename=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest), pipes.quote(os.path.basename(src))) + + if self.runner.noop_on_check(inject): + return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=src, before=dest_contents, after=resultant)) + else: + res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject, complex_args=complex_args) + res.diff = dict(before=dest_contents, after=resultant) + return res + else: + return self.runner._execute_module(conn, tmp, 'file', module_args, inject=inject, complex_args=complex_args) diff --git a/library/files/assemble b/library/files/assemble index 8811f5ca55..fd497bdb91 100644 --- a/library/files/assemble +++ b/library/files/assemble @@ -31,9 +31,9 @@ description: - Assembles a configuration file from fragments. Often a particular program will take a single configuration file and does not support a C(conf.d) style structure where it is easy to build up the configuration - from multiple sources. M(assemble) will take a directory of files that have - already been transferred to the system, and concatenate them together to - produce a destination file. Files are assembled in string sorting order. + from multiple sources. M(assemble) will take a directory of files that can be + local or have already been transferred to the system, and concatenate them + together to produce a destination file. Files are assembled in string sorting order. Puppet calls this idea I(fragments). version_added: "0.5" options: @@ -62,6 +62,14 @@ options: version_added: "1.4" required: false default: null + remote_src: + description: + - If False, it will search for src at originating/master machine, if True it will + go to the remote/target machine for the src. + choices: [ "True", "False" ] + required: false + default: "True" + version_added: "1.4" others: description: - all arguments accepted by the M(file) module also work here @@ -107,6 +115,7 @@ def main(): delimiter = dict(required=False), dest = dict(required=True), backup=dict(default=False, type='bool'), + remote_src=dict(default=False, type='bool'), ), add_file_common_args=True ) From 971976ae991c76fb30780709cca7235cb4bcb1ca Mon Sep 17 00:00:00 2001 From: Carson Gee Date: Tue, 17 Sep 2013 16:21:08 -0400 Subject: [PATCH 08/18] Added module for handling AWS Virtual Private Clouds This handles creating and editing VPCs and takes care of vpcs, subnets, Internet Gateways, and route tables. --- library/cloud/vpc | 552 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 552 insertions(+) create mode 100644 library/cloud/vpc diff --git a/library/cloud/vpc b/library/cloud/vpc new file mode 100644 index 0000000000..e93dbcfe7a --- /dev/null +++ b/library/cloud/vpc @@ -0,0 +1,552 @@ +#!/usr/bin/python +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +DOCUMENTATION = ''' +--- +module: vpc +short_description: configure AWS virtual private clouds +description: + - Create or terminates AWS virtual private clouds. This module has a dependency on python-boto. +version_added: "1.4" +options: + cidr_block: + description: + - The cidr block representing the VPC, e.g. 10.0.0.0/16 + required: false, unless state=present + dns_support: + description: + - toggles the "Enable DNS resolution" flag + required: false + default: "yes" + choices: [ "yes", "no" ] + dns_hostnames: + description: + - toggles the "Enable DNS hostname support for instances" flag + required: false + default: "yes" + choices: [ "yes", "no" ] + subnets: + description: + - A dictionary array of subnets to add of the form: { cidr: ..., az: ... }. Where az is the desired availability zone of the subnet, but it is not required. All VPC subnets not in this list will be removed. + required: false + default: null + aliases: [] + vpc_id: + description: + - A VPC id to terminate when state=absent + required: false + default: null + aliases: [] + internet_gateway: + description: + - Toggle whether there should be an Internet gateway attached to the VPC + required: false + default: "no" + choices: [ "yes", "no" ] + aliases: [] + route_tables: + description: + - A dictionary array of route tables to add of the form: { subnets: [172.22.2.0/24, 172.22.3.0/24,], routes: [{ dest: 0.0.0.0/0, gw: igw},] }. Where the subnets list is those subnets the route table should be associated with, and the routes list is a list of routes to be in the table. The special keyword for the gw of igw specifies that you should the route should go through the internet gateway attached to the VPC. gw also accepts instance-ids in addition igw. This module is currently unable to affect the 'main' route table due to some limitations in boto, so you must explicitly define the associated subnets or they will be attached to the main table implicitly. + required: false + default: null + aliases: [] + wait: + description: + - wait for the VPC to be in state 'available' before returning + required: false + default: "no" + choices: [ "yes", "no" ] + aliases: [] + wait_timeout: + description: + - how long before wait gives up, in seconds + default: 300 + aliases: [] + state: + description: + - Create or terminate the VPC + required: true + default: present + aliases: [] + region: + description: + - region in which the resource exists. + required: false + default: null + aliases: ['aws_region', 'ec2_region'] + aws_secret_key: + description: + - AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used. + required: false + default: None + aliases: ['ec2_secret_key', 'secret_key' ] + aws_access_key: + description: + - AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used. + required: false + default: None + aliases: ['ec2_access_key', 'access_key' ] +requirements: [ "boto" ] +author: Carson Gee +''' + +EXAMPLES = ''' +# Note: None of these examples set aws_access_key, aws_secret_key, or region. +# It is assumed that their matching environment variables are set. + +# Basic creation example: + local_action: + module: vpc + state: present + cidr_block: 172.23.0.0/16 + region: us-west-2 +# Full creation example with subnets and optional availability zones. +# The absence or presense of subnets deletes or creates them respectively. + local_action: + module: vpc + state: present + cidr_block: 172.22.0.0/16 + subnets: + - cidr: 172.22.1.0/24 + az: us-west-2c + - cidr: 172.22.2.0/24 + az: us-west-2b + - cidr: 172.22.3.0/24 + az: us-west-2a + internet_gateway: True + route_tables: + - subnets: + - 172.22.2.0/24 + - 172.22.3.0/24 + routes: + - dest: 0.0.0.0/0 + gw: igw + - subnets: + - 172.22.1.0/24 + routes: + - dest: 0.0.0.0/0 + gw: igw + region: us-west-2 + register: vpc + +# Removal of a VPC by id + local_action: + module: vpc + state: absent + vpc_id: vpc-aaaaaaa + region: us-west-2 +If you have added elements not managed by this module, e.g. instances, NATs, etc then +the delete will fail until those dependencies are removed. +''' + + +import sys +import time + +try: + import boto.ec2 + import boto.vpc + from boto.exception import EC2ResponseError +except ImportError: + print "failed=True msg='boto required for this module'" + sys.exit(1) + +AWS_REGIONS = ['ap-northeast-1', + 'ap-southeast-1', + 'ap-southeast-2', + 'eu-west-1', + 'sa-east-1', + 'us-east-1', + 'us-west-1', + 'us-west-2'] + +def get_vpc_info(vpc): + """ + Retrieves vpc information from an instance + ID and returns it as a dictionary + """ + + return({ + 'id': vpc.id, + 'cidr_block': vpc.cidr_block, + 'dhcp_options_id': vpc.dhcp_options_id, + 'region': vpc.region.name, + 'state': vpc.state, + }) + +def create_vpc(module, vpc_conn): + """ + Creates a new VPC + + module : AnsibleModule object + vpc_conn: authenticated VPCConnection connection object + + Returns: + A dictionary with information + about the VPC and subnets that were launched + """ + + id = module.params.get('id') + cidr_block = module.params.get('cidr_block') + dns_support = module.params.get('dns_support') + dns_hostnames = module.params.get('dns_hostnames') + subnets = module.params.get('subnets') + internet_gateway = module.params.get('internet_gateway') + route_tables = module.params.get('route_tables') + wait = module.params.get('wait') + wait_timeout = int(module.params.get('wait_timeout')) + changed = False + + # Check for existing VPC by cidr_block or id + if id != None: + filter_dict = {'vpc-id':id, 'state': 'available',} + previous_vpcs = vpc_conn.get_all_vpcs(None, filter_dict) + else: + filter_dict = {'cidr': cidr_block, 'state': 'available'} + previous_vpcs = vpc_conn.get_all_vpcs(None, filter_dict) + + if len(previous_vpcs) > 1: + module.fail_json(msg='EC2 returned more than one VPC, aborting') + + if len(previous_vpcs) == 1: + changed = False + vpc = previous_vpcs[0] + else: + changed = True + try: + vpc = vpc_conn.create_vpc(cidr_block) + # wait here until the vpc is available + pending = True + wait_timeout = time.time() + wait_timeout + while wait and wait_timeout > time.time() and pending: + pvpc = vpc_conn.get_all_vpcs(vpc.id) + if pvpc.state == "available": + pending = False + time.sleep(5) + if wait and wait_timeout <= time.time(): + # waiting took too long + module.fail_json(msg = "wait for vpc availability timeout on %s" % time.asctime()) + + except boto.exception.BotoServerError, e: + module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message)) + + # Done with base VPC, now change to attributes and features. + + + # boto doesn't appear to have a way to determine the existing + # value of the dns attributes, so we just set them. + # It also must be done one at a time. + vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_support=dns_support) + vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_hostnames=dns_hostnames) + + + # Process all subnet properties + if not isinstance(subnets, list): + module.fail_json(msg='subnets needs to be a list of cidr blocks') + + current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id }) + # First add all new subnets + for subnet in subnets: + add_subnet = True + for csn in current_subnets: + if subnet['cidr'] == csn.cidr_block: + add_subnet = False + if add_subnet: + try: + vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None)) + changed = True + except EC2ResponseError as e: + module.fail_json(msg='Unable to create subnet {0}, error: {1}'.format(subnet['cidr'], e)) + # Now delete all absent subnets + for csubnet in current_subnets: + delete_subnet = True + for subnet in subnets: + if csubnet.cidr_block == subnet['cidr']: + delete_subnet = False + if delete_subnet: + try: + vpc_conn.delete_subnet(csubnet.id) + changed = True + except EC2ResponseError as e: + module.fail_json(msg='Unable to delete subnet {0}, error: {1}'.format(csubnet.cidr_block, e)) + + # Handle Internet gateway (create/delete igw) + igw = None + igws = vpc_conn.get_all_internet_gateways(filters={'attachment.vpc-id': vpc.id}) + if len(igws) > 1: + module.fail_json(msg='EC2 returned more than one Internet Gateway for id %s, aborting' % vpc.id) + if internet_gateway: + if len(igws) != 1: + try: + igw = vpc_conn.create_internet_gateway() + vpc_conn.attach_internet_gateway(igw.id, vpc.id) + changed = True + except EC2ResponseError as e: + module.fail_json(msg='Unable to create Internet Gateway, error: {0}'.format(e)) + else: + # Set igw variable to the current igw instance for use in route tables. + igw = igws[0] + else: + if len(igws) > 0: + try: + vpc_conn.detach_internet_gateway(igws[0].id, vpc.id) + vpc_conn.delete_internet_gateway(igws[0].id) + changed = True + except EC2ResponseError as e: + module.fail_json(msg='Unable to delete Internet Gateway, error: {0}'.format(e)) + + # Handle route tables - this may be worth splitting into a + # different module but should work fine here. The strategy to stay + # indempotent is to basically build all the route tables as + # defined, track the route table ids, and then run through the + # remote list of route tables and delete any that we didn't + # create. This shouldn't interupt traffic in theory, but is the + # only way to really work with route tables over time that I can + # think of without using painful aws ids. Hopefully boto will add + # the replace-route-table API to make this smoother and + # allow control of the 'main' routing table. + if not isinstance(route_tables, list): + module.fail_json(msg='route tables need to be a list of dictionaries') + + # Work through each route table and update/create to match dictionary array + all_route_tables = [] + for rt in route_tables: + try: + new_rt = vpc_conn.create_route_table(vpc.id) + for route in rt['routes']: + r_gateway = route['gw'] + if r_gateway == 'igw': + if not internet_gateway: + module.fail_json( + msg='You asked for an Internet Gateway ' \ + '(igw) route, but you have no Internet Gateway' + ) + r_gateway = igw.id + vpc_conn.create_route(new_rt.id, route['dest'], r_gateway) + + # Associate with subnets + for sn in rt['subnets']: + rsn = vpc_conn.get_all_subnets(filters={'cidr': sn }) + if len(rsn) != 1: + module.fail_json( + msg='The subnet {0} to associate with route_table {1} ' \ + 'does not exist, aborting'.format(sn, rt) + ) + rsn = rsn[0] + + # Disassociate then associate since we don't have replace + old_rt = vpc_conn.get_all_route_tables( + filters={'association.subnet_id': rsn.id} + ) + if len(old_rt) == 1: + old_rt = old_rt[0] + association_id = None + for a in old_rt.associations: + if a.subnet_id == rsn.id: + association_id = a.id + vpc_conn.disassociate_route_table(association_id) + + vpc_conn.associate_route_table(new_rt.id, rsn.id) + + all_route_tables.append(new_rt) + except EC2ResponseError as e: + module.fail_json( + msg='Unable to create and associate route table {0}, error: ' \ + '{1}'.format(rt, e) + ) + + + # Now that we are good to go on our new route tables, delete the + # old ones except the 'main' route table as boto can't set the main + # table yet. + all_rts = vpc_conn.get_all_route_tables(filters={'vpc-id': vpc.id}) + for rt in all_rts: + delete_rt = True + for newrt in all_route_tables: + if newrt.id == rt.id: + delete_rt = False + if delete_rt: + rta = rt.associations + is_main = False + for a in rta: + if a.main: + is_main = True + try: + if not is_main: + vpc_conn.delete_route_table(rt.id) + except EC2ResponseError as e: + module.fail_json(msg='Unable to delete old route table {0}, error: {1}'.format(rt.id, e)) + + vpc_dict = get_vpc_info(vpc) + created_vpc_id = vpc.id + returned_subnets = [] + current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id }) + for sn in current_subnets: + returned_subnets.append({ + 'cidr': sn.cidr_block, + 'az': sn.availability_zone, + 'id': sn.id, + }) + + + return (vpc_dict, created_vpc_id, returned_subnets, changed) + +def terminate_vpc(module, vpc_conn, vpc_id=None, cidr=None): + """ + Terminates a VPC + + module: Ansible module object + vpc_conn: authenticated VPCConnection connection object + vpc_id: a vpc id to terminate + cidr: The cidr block of the VPC - can be used in lieu of an ID + + Returns a dictionary of VPC information + about the VPC terminated. + + If the VPC to be terminated is available + "changed" will be set to True. + + """ + vpc_dict = {} + terminated_vpc_id = '' + changed = False + + if vpc_id == None and cidr == None: + module.fail_json( + msg='You must either specify a vpc id or a cidr '\ + 'block to terminate a VPC, aborting' + ) + if vpc_id is not None: + vpc_rs = vpc_conn.get_all_vpcs(vpc_id) + else: + vpc_rs = vpc_conn.get_all_vpcs(filters={'cidr': cidr}) + if len(vpc_rs) > 1: + module.fail_json( + msg='EC2 returned more than one VPC for id {0} ' \ + 'or cidr {1}, aborting'.format(vpc_id,vidr) + ) + if len(vpc_rs) == 1: + vpc = vpc_rs[0] + if vpc.state == 'available': + terminated_vpc_id=vpc.id + vpc_dict=get_vpc_info(vpc) + try: + subnets = vpc_conn.get_all_subnets(filters={'vpc_id': vpc.id}) + for sn in subnets: + vpc_conn.delete_subnet(sn.id) + + igws = vpc_conn.get_all_internet_gateways( + filters={'attachment.vpc-id': vpc.id} + ) + for igw in igws: + vpc_conn.detach_internet_gateway(igw.id, vpc.id) + vpc_conn.delete_internet_gateway(igw.id) + + rts = vpc_conn.get_all_route_tables(filters={'vpc_id': vpc.id}) + for rt in rts: + rta = rt.associations + is_main = False + for a in rta: + if a.main: + is_main = True + if not is_main: + vpc_conn.delete_route_table(rt.id) + + vpc_conn.delete_vpc(vpc.id) + except EC2ResponseError as e: + module.fail_json( + msg='Unable to delete VPC {0}, error: {1}'.format(vpc.id, e) + ) + changed = True + + return (changed, vpc_dict, terminated_vpc_id) + + +def main(): + module = AnsibleModule( + argument_spec = dict( + cidr_block = dict(), + wait = dict(choices=BOOLEANS, default=False), + wait_timeout = dict(default=300), + dns_support = dict(choices=BOOLEANS, default=True), + dns_hostnames = dict(choices=BOOLEANS, default=True), + subnets = dict(type='list'), + vpc_id = dict(), + internet_gateway = dict(choices=BOOLEANS, default=False), + route_tables = dict(type='list'), + region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS), + state = dict(choices=['present', 'absent'], default='present'), + aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True), + aws_access_key = dict(aliases=['ec2_access_key', 'access_key']), + ) + ) + + region = module.params.get('region') + state = module.params.get('state') + aws_secret_key = module.params.get('aws_secret_key') + aws_access_key = module.params.get('aws_access_key') + + if not aws_secret_key: + if 'AWS_SECRET_KEY' in os.environ: + aws_secret_key = os.environ['AWS_SECRET_KEY'] + elif 'EC2_SECRET_KEY' in os.environ: + aws_secret_key = os.environ['EC2_SECRET_KEY'] + + if not aws_access_key: + if 'AWS_ACCESS_KEY' in os.environ: + aws_access_key = os.environ['AWS_ACCESS_KEY'] + elif 'EC2_ACCESS_KEY' in os.environ: + aws_access_key = os.environ['EC2_ACCESS_KEY'] + + if not region: + if 'AWS_REGION' in os.environ: + region = os.environ['AWS_REGION'] + elif 'EC2_REGION' in os.environ: + region = os.environ['EC2_REGION'] + + # If we have a region specified, connect to its endpoint. + if region: + try: + vpc_conn = boto.vpc.connect_to_region( + region, + aws_access_key_id=aws_access_key, + aws_secret_access_key=aws_secret_key + ) + except boto.exception.NoAuthHandlerFound, e: + module.fail_json(msg = str(e)) + else: + module.fail_json(msg="region must be specified") + + if module.params.get('state') == 'absent': + vpc_id = module.params.get('vpc_id') + cidr = module.params.get('cidr_block') + if vpc_id == None and cidr == None: + module.fail_json( + msg='You must either specify a vpc id or a cidr '\ + 'block to terminate a VPC, aborting' + ) + (changed, vpc_dict, new_vpc_id) = terminate_vpc(module, vpc_conn, vpc_id, cidr) + subnets_changed = None + elif module.params.get('state') == 'present': + # Changed is always set to true when provisioning a new VPC + (vpc_dict, new_vpc_id, subnets_changed, changed) = create_vpc(module, vpc_conn) + + module.exit_json(changed=changed, vpc_id=new_vpc_id, vpc=vpc_dict, subnets=subnets_changed) + +# this is magic, see lib/ansible/module_common.py +#<> + +main() From 9d2f9bf93c389307b019be6549d7518ba717f263 Mon Sep 17 00:00:00 2001 From: Alexander Kuznecov Date: Sat, 9 Nov 2013 00:14:55 +0700 Subject: [PATCH 09/18] resolves #4855 --- lib/ansible/runner/action_plugins/synchronize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ansible/runner/action_plugins/synchronize.py b/lib/ansible/runner/action_plugins/synchronize.py index a7e9873096..865fcbb2b2 100644 --- a/lib/ansible/runner/action_plugins/synchronize.py +++ b/lib/ansible/runner/action_plugins/synchronize.py @@ -98,8 +98,8 @@ class ActionModule(object): if rsync_path: options['rsync_path'] = '"' + rsync_path + '"' - self.runner.module_args = ' '.join(['%s=%s' % (k, v) for (k, + module_items = ' '.join(['%s=%s' % (k, v) for (k, v) in options.items()]) return self.runner._execute_module(conn, tmp, 'synchronize', - self.runner.module_args, inject=inject) + module_items, inject=inject) From 710117e4da666c9d6d77528158b8b1f609025d2a Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 10:37:27 -0500 Subject: [PATCH 10/18] Addresses #4628 evaluate package check return properly and exit failure if not present --- library/packaging/pkgng | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/packaging/pkgng b/library/packaging/pkgng index dbae01bbb1..b8221a999d 100644 --- a/library/packaging/pkgng +++ b/library/packaging/pkgng @@ -123,8 +123,8 @@ def install_packages(module, pkgin_path, packages, cached, pkgsite): if not module.check_mode: rc, out, err = module.run_command("%s %s install -U -y %s" % (pkgsite, pkgin_path, package)) - if not module.check_mode and query_package(module, pkgin_path, package): - module.fail_json(msg="failed to install %s: %s" % (package, out)) + if not module.check_mode and not query_package(module, pkgin_path, package): + module.fail_json(msg="failed to install %s: %s" % (package, out), stderr=err) install_c += 1 From e4c2517e89b21ef21f981e7c020045b7c39e0d84 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 13:21:49 -0500 Subject: [PATCH 11/18] Add default for remote_src to assemble dostrings to avoid confusion --- library/files/assemble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/files/assemble b/library/files/assemble index 9e9886bd61..d1d375bd2a 100644 --- a/library/files/assemble +++ b/library/files/assemble @@ -65,7 +65,7 @@ options: remote_src: description: - If False, it will search for src at originating/master machine, if True it will - go to the remote/target machine for the src. + go to the remote/target machine for the src. Default is True. choices: [ "True", "False" ] required: false default: "True" From b37a8b90a607c64d8dfb5c06ac327c83238324e1 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 14:02:50 -0500 Subject: [PATCH 12/18] Fix docstring quoting in vpc module --- library/cloud/vpc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/cloud/vpc b/library/cloud/vpc index e93dbcfe7a..8ecab435c1 100644 --- a/library/cloud/vpc +++ b/library/cloud/vpc @@ -24,7 +24,7 @@ version_added: "1.4" options: cidr_block: description: - - The cidr block representing the VPC, e.g. 10.0.0.0/16 + - "The cidr block representing the VPC, e.g. 10.0.0.0/16" required: false, unless state=present dns_support: description: @@ -40,7 +40,7 @@ options: choices: [ "yes", "no" ] subnets: description: - - A dictionary array of subnets to add of the form: { cidr: ..., az: ... }. Where az is the desired availability zone of the subnet, but it is not required. All VPC subnets not in this list will be removed. + - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... }. Where az is the desired availability zone of the subnet, but it is not required. All VPC subnets not in this list will be removed." required: false default: null aliases: [] @@ -59,7 +59,7 @@ options: aliases: [] route_tables: description: - - A dictionary array of route tables to add of the form: { subnets: [172.22.2.0/24, 172.22.3.0/24,], routes: [{ dest: 0.0.0.0/0, gw: igw},] }. Where the subnets list is those subnets the route table should be associated with, and the routes list is a list of routes to be in the table. The special keyword for the gw of igw specifies that you should the route should go through the internet gateway attached to the VPC. gw also accepts instance-ids in addition igw. This module is currently unable to affect the 'main' route table due to some limitations in boto, so you must explicitly define the associated subnets or they will be attached to the main table implicitly. + - "A dictionary array of route tables to add of the form: { subnets: [172.22.2.0/24, 172.22.3.0/24,], routes: [{ dest: 0.0.0.0/0, gw: igw},] }. Where the subnets list is those subnets the route table should be associated with, and the routes list is a list of routes to be in the table. The special keyword for the gw of igw specifies that you should the route should go through the internet gateway attached to the VPC. gw also accepts instance-ids in addition igw. This module is currently unable to affect the 'main' route table due to some limitations in boto, so you must explicitly define the associated subnets or they will be attached to the main table implicitly." required: false default: null aliases: [] From 8332a0b75ec765b7fdd34bb8e52f779da0170cbe Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 14:09:06 -0500 Subject: [PATCH 13/18] Change vpc module to use shared ec2 authorization moudle snippet --- library/cloud/vpc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/cloud/vpc b/library/cloud/vpc index 8ecab435c1..4d218136a0 100644 --- a/library/cloud/vpc +++ b/library/cloud/vpc @@ -494,10 +494,9 @@ def main(): ) ) - region = module.params.get('region') state = module.params.get('state') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') + + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) if not aws_secret_key: if 'AWS_SECRET_KEY' in os.environ: @@ -546,7 +545,8 @@ def main(): module.exit_json(changed=changed, vpc_id=new_vpc_id, vpc=vpc_dict, subnets=subnets_changed) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() From 06eb7357fd4459d8cf2f39ee5a6ba608bfcdbbfa Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 14:10:38 -0500 Subject: [PATCH 14/18] Remove redundant credential code in vpc module --- library/cloud/vpc | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/library/cloud/vpc b/library/cloud/vpc index 4d218136a0..c7d708f92a 100644 --- a/library/cloud/vpc +++ b/library/cloud/vpc @@ -497,25 +497,7 @@ def main(): state = module.params.get('state') ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) - - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - - if not region: - if 'AWS_REGION' in os.environ: - region = os.environ['AWS_REGION'] - elif 'EC2_REGION' in os.environ: - region = os.environ['EC2_REGION'] - + # If we have a region specified, connect to its endpoint. if region: try: From fb903c5317d26d199233a5e5a1177375d0472819 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 14:13:05 -0500 Subject: [PATCH 15/18] Change s3 module to use shared ec2 authorization module snippet --- library/cloud/s3 | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/library/cloud/s3 b/library/cloud/s3 index 480f4b1b83..fcacaadeb2 100644 --- a/library/cloud/s3 +++ b/library/cloud/s3 @@ -272,9 +272,9 @@ def main(): mode = module.params.get('mode') expiry = int(module.params['expiry']) s3_url = module.params.get('s3_url') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') overwrite = module.params.get('overwrite') + + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) if module.params.get('object'): obj = os.path.expanduser(module.params['object']) @@ -283,18 +283,6 @@ def main(): if not s3_url and 'S3_URL' in os.environ: s3_url = os.environ['S3_URL'] - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - # If we have an S3_URL env var set, this is likely to be Walrus, so change connection method if is_walrus(s3_url): try: @@ -463,8 +451,8 @@ def main(): module.exit_json(failed=False) - -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() From 0ecc83fe9808fa292a65cbac1aeb035f4a1fbed2 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 14:16:23 -0500 Subject: [PATCH 16/18] Change route53 module to use shared ec2 authorization module snippet --- library/cloud/route53 | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/library/cloud/route53 b/library/cloud/route53 index f279a1390b..4e3051feea 100644 --- a/library/cloud/route53 +++ b/library/cloud/route53 @@ -165,8 +165,8 @@ def main(): record_in = module.params.get('record') type_in = module.params.get('type') value_in = module.params.get('value') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') + + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) value_list = () @@ -186,19 +186,6 @@ def main(): if not value_in: module.fail_json(msg = "parameter 'value' required for create/delete") - # allow environment variables to be used if ansible vars aren't set - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - # connect to the route53 endpoint try: conn = boto.route53.connection.Route53Connection(aws_access_key, aws_secret_key) @@ -263,7 +250,8 @@ def main(): module.exit_json(changed=True) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() From dc41bb8085317a257305461e5ba6c85a470eb45f Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 14:36:46 -0500 Subject: [PATCH 17/18] Merge pull request #4207 from ashorin/ansible Fail playbook when serial is set and hadlers fail on set. --- lib/ansible/playbook/__init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index 0d82deb0ef..36788f2409 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -544,12 +544,23 @@ class PlayBook(object): for handler in play.handlers(): if len(handler.notified_by) > 0: self.inventory.restrict_to(handler.notified_by) + # Resolve the variables first handler_name = template(play.basedir, handler.name, handler.module_vars) if handler_name not in fired_names: self._run_task(play, handler, True) # prevent duplicate handler includes from running more than once fired_names[handler_name] = 1 + + host_list = self._list_available_hosts(play.hosts) + if handler.any_errors_fatal and len(host_list) < hosts_count: + play.max_fail_pct = 0 + if (hosts_count - len(host_list)) > int((play.max_fail_pct)/100.0 * hosts_count): + host_list = None + if not host_list: + self.callbacks.on_no_hosts_remaining() + return False + self.inventory.lift_restriction() new_list = handler.notified_by[:] for host in handler.notified_by: From 5a3032a9504f6868c06c0ae9a57580c54a3ab94d Mon Sep 17 00:00:00 2001 From: James Tanner Date: Tue, 12 Nov 2013 18:17:20 -0500 Subject: [PATCH 18/18] Fixes #4884 Do not prematurely exit from file module if src not defined --- library/files/file | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/library/files/file b/library/files/file index 13a675c20c..01772a7e71 100644 --- a/library/files/file +++ b/library/files/file @@ -186,12 +186,8 @@ def main(): prev_state = 'file' if prev_state is not None and state is None: - if not params.get('src', None): - # idempotent exit if state is not specified - module.exit_json(path=path, changed=False) - else: - # src is defined, need to process other operations - state = prev_state + # set state to current type of file + state = prev_state elif state is None: # set default state to file state = 'file'