From d431e23f086921fa90b191b8acc0cf846611aa6e Mon Sep 17 00:00:00 2001 From: John Jarvis <jarv@edx.org> Date: Fri, 26 Apr 2013 11:57:02 -0400 Subject: [PATCH] change module->library, update the example playbook By default ansible will load libs in library/ which means you do not need to specify a module path if you are invoking ansible from the playbooks/ dir. Added some comments to the rolling example --- playbooks/edxapp_rolling_example.yml | 11 ++++++++++- playbooks/library/ec2_elb_facts | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ playbooks/modules/ec2_elb_facts | 71 ----------------------------------------------------------------------- 3 files changed, 81 insertions(+), 72 deletions(-) create mode 100644 playbooks/library/ec2_elb_facts delete mode 100644 playbooks/modules/ec2_elb_facts diff --git a/playbooks/edxapp_rolling_example.yml b/playbooks/edxapp_rolling_example.yml index 84196f7..9c0b12e 100644 --- a/playbooks/edxapp_rolling_example.yml +++ b/playbooks/edxapp_rolling_example.yml @@ -1,4 +1,5 @@ -# ansible-playbook -v --user=ubuntu edxapp_rolling_example.yml -i ./ec2.py --private-key=/path/to/deployment.pem --module-path /path/to/repo/modules +# ansible-playbook -v --user=ubuntu edxapp_rolling_example.yml -i ./ec2.py --private-key=/path/to/deployment.pem + - hosts: tag_Group_anothermulti serial: 1 vars_files: @@ -10,10 +11,18 @@ ec2_facts: - name: Gathering ELB facts local_action: ec2_elb_facts +# These two modules "ec2_facts" and "ec2_elb_facts" are invoked in the +# pre_tasks and give us the $elbs and $ansible_ec2_isntance_id facts +# which are variables that can be used in the playbook - local_action: command util/elb_reg.py -e {{ ",".join(elbs[ansible_ec2_instance_id]) }} -i {{ ansible_ec2_instance_id }} deregister +# -e is the list of elbs that the current instances belong to and -i is +# the instance "active_elbs" are the elbs that are passed in. roles: - common - nginx - lms post_tasks: - local_action: command util/elb_reg.py -e {{ ",".join(elbs[ansible_ec2_instance_id]) }} -i {{ ansible_ec2_instance_id }} register +# Register will pass in the same elb list and the same instance id +# to add it back to the pool + diff --git a/playbooks/library/ec2_elb_facts b/playbooks/library/ec2_elb_facts new file mode 100644 index 0000000..bf45053 --- /dev/null +++ b/playbooks/library/ec2_elb_facts @@ -0,0 +1,71 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# 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 <http://www.gnu.org/licenses/>. + +DOCUMENTATION=""" +--- +module: ec2_elb_facts +short_description: Creates a fact with instance to ELB mappings +version_added: "1.0" +options: {} +description: + - This module fetches data from AWS using the boto api and returns + a hash as a fact that maps instances to the ELB(s) that they belong to. +examples: + - code: ansible all -m ec2_elb_facts + description: Obtain ELB info using boto +requirements: [ "boto" ] +author: "John Jarvis <john@jarv.org>" +""" + +try: + import boto +except ImportError: + print "failed=True msg='boto required for this module'" + sys.exit(1) + + +from collections import defaultdict + +def get_elb_info(module): + + try: + elb = boto.connect_elb() + except boto.exception.NoAuthHandlerFound, e: + module.fail_json(msg = str(e)) + + elbs = elb.get_all_load_balancers() + elb_info = defaultdict(set) + for lb in elbs: + for info in lb.instances: + elb_info[info.id].add(lb.name) + elb_info = {k: list(v) for k,v in elb_info.iteritems()} + return elb_info + + +def main(): + + + module = AnsibleModule(argument_spec = dict()) + elb_info = get_elb_info(module) + ec2_facts_result = dict(changed=False, ansible_facts={'elbs': elb_info}) + module.exit_json(**ec2_facts_result) + +# this is magic, see lib/ansible/module_common.py +#<<INCLUDE_ANSIBLE_MODULE_COMMON>> + +main() diff --git a/playbooks/modules/ec2_elb_facts b/playbooks/modules/ec2_elb_facts deleted file mode 100644 index bf45053..0000000 --- a/playbooks/modules/ec2_elb_facts +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# 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 <http://www.gnu.org/licenses/>. - -DOCUMENTATION=""" ---- -module: ec2_elb_facts -short_description: Creates a fact with instance to ELB mappings -version_added: "1.0" -options: {} -description: - - This module fetches data from AWS using the boto api and returns - a hash as a fact that maps instances to the ELB(s) that they belong to. -examples: - - code: ansible all -m ec2_elb_facts - description: Obtain ELB info using boto -requirements: [ "boto" ] -author: "John Jarvis <john@jarv.org>" -""" - -try: - import boto -except ImportError: - print "failed=True msg='boto required for this module'" - sys.exit(1) - - -from collections import defaultdict - -def get_elb_info(module): - - try: - elb = boto.connect_elb() - except boto.exception.NoAuthHandlerFound, e: - module.fail_json(msg = str(e)) - - elbs = elb.get_all_load_balancers() - elb_info = defaultdict(set) - for lb in elbs: - for info in lb.instances: - elb_info[info.id].add(lb.name) - elb_info = {k: list(v) for k,v in elb_info.iteritems()} - return elb_info - - -def main(): - - - module = AnsibleModule(argument_spec = dict()) - elb_info = get_elb_info(module) - ec2_facts_result = dict(changed=False, ansible_facts={'elbs': elb_info}) - module.exit_json(**ec2_facts_result) - -# this is magic, see lib/ansible/module_common.py -#<<INCLUDE_ANSIBLE_MODULE_COMMON>> - -main() -- libgit2 0.26.0