From fdc4b816d52e1abce24781ed0b07ac8f391a5641 Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Fri, 24 May 2013 21:02:18 +0000 Subject: [PATCH 1/2] add which_playbook script --- scripts/which_playbook | 107 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100755 scripts/which_playbook diff --git a/scripts/which_playbook b/scripts/which_playbook new file mode 100755 index 0000000000..4665837cf4 --- /dev/null +++ b/scripts/which_playbook @@ -0,0 +1,107 @@ +#!/usr/bin/python -tt +# skvidal +# gplv2 + +# takes hostnames as arguments +# returns the group or host playbook which will setup/configure this host +# or outputs an error message if there is no playbook for that host + +#NB - this does not read the actual hosts entries from the plays in the +# playbooks b/c that captures lots of issues with delegated hosts and VM and +# silliness. It just uses the filename :) + +# takes 2 dirs: +# - host-specific-playbooks +# - group-specific-playbooks + +# looks for a host playbook matching the hostname first +# looks up groups for that host specified +# looks if any of the groups have playbooks +# if more than one playbook exists - emits an error (either a group AND a +# hostname playbook or a 2 group playbooks) +# output the command to run +# if it is a group playbook include the --limit option + + + +import os +import sys +import ansible.inventory +from optparse import OptionParser + +host_path = '/srv/web/infra/ansible/playbooks/hosts' +group_path = '/srv/web/infra/ansible/playbooks/groups' +pb_extension = '.yml' + + + +def main(): + group_playbooks = [] + for (d, dirs, files) in os.walk(group_path): + for fn in files: + if fn.endswith(pb_extension): + group_playbooks.append(d + '/' + fn) + + + + host_playbooks = [] + for (d, dirs, files) in os.walk(host_path): + for fn in files: + if fn.endswith(pb_extension): + host_playbooks.append(d + '/' + fn) + + + if not host_playbooks and not group_playbooks: + print "No Playbooks found - that seems unlikely" + return 1 + + + parser = OptionParser(version = "1.0") + parser.add_option('-i', dest='inventory', default=None, + help="Path to inventory file/dir") + opts,hosts = parser.parse_args(sys.argv[1:]) + + if opts.inventory: + inv = ansible.inventory.Inventory(host_list=opts.inventory) + else: + inv = ansible.inventory.Inventory() + + + for host in hosts: + matched_host = None + matched_group = None + for h_pb in host_playbooks: + if matched_host: + break + if host == os.path.basename(h_pb).replace(pb_extension, ''): + matched_host = h_pb + for group in inv.groups_for_host(host): + if matched_group: + break + for g_pb in group_playbooks: + if group.name == os.path.basename(g_pb).replace(pb_extension, ''): + matched_group = g_pb + + if matched_host and matched_group: + print "\nError: Found a group playbook and a host playbook for %s" % host + continue + + if not matched_host and not matched_group: + print "\nNo playbook found for %s" % host + continue + + if matched_group: + print "\nplaybook is %s" % matched_group + print "Run with: \n ansible-playbook %s --limit=%s\n" % (matched_group, host) + + if matched_host: + print "\nplaybook is %s" % matched_host + print "Run with: \n ansible-playbook %s" % (matched_host) + + + + print '' + return 0 + +if __name__ == "__main__": + sys.exit(main()) From 2b9e169a90456356df9333316be1657bcd6d0851 Mon Sep 17 00:00:00 2001 From: Seth Vidal Date: Fri, 24 May 2013 21:05:40 +0000 Subject: [PATCH 2/2] update readme! --- scripts/README | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts/README b/scripts/README index 4f0e34a080..b93dfceef4 100644 --- a/scripts/README +++ b/scripts/README @@ -1,18 +1,22 @@ -executable scripts to use ansible on the system +ans-needs-reboot - check which hosts need to be rebooted due to kernel or + other critical updates -describe-instances is to get a simple list of all the instances under an -account in the eucalyptus cloudlet. +ans-vhost-freemem - display freememory per vhost -normal invocation: +auth-keys-from-fas - run to pull ssh auth keys from fas for users to push + onto systems +freezelist - run to see if a host is included in the change-freeze or not -sudo -i watch /srv/web/infra/ansible/scripts/describe-instances +ok-nagios - tells nagios to start notifying again -so you can see it update/change +shutup-nagios - tells nagios to shut the hell up + +which_playbook - lets you look up which playbook (and how to run it) for + running the deployment/maintenance playbook for any given + host + ex: which_playbook kernel01.qa.fedoraproject.org -terminate-instances -To shutdown an instance just run: -sudo -i /srv/web/infra/ansible/scripts/terminate-instances ip-of-instance