├── .gitignore ├── LICENCE ├── README.md ├── bashcompletion.sh ├── bashcompletion.zsh.sh └── bin └── ansishell /.gitignore: -------------------------------------------------------------------------------- 1 | #IDE 2 | .idea 3 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 SkaveRat 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansishell 2 | 3 | A small tool to quickly ssh into an ansible host group 4 | 5 | ## Dependencies 6 | 7 | * Ansible 2.2 8 | * Python 3 9 | * pyyaml 10 | * jinja2 11 | 12 | ## INI file 13 | 14 | create an `.ansishell` file in your home directory: 15 | 16 | ```ini 17 | [inventory] 18 | ; "default" is required 19 | default=/home/foobar/myproject/dev_inventory 20 | dev=/home/foobar/myproject/dev_inventory 21 | production=/home/foobar/myproject/production_inventory 22 | 23 | [config] 24 | ; the command to open multiple shells. Needs to support syntax "$command $host1 $host2 ..." 25 | command=mssh 26 | ``` 27 | 28 | ## Installation 29 | 30 | Add the `bin` dir or symlink the executable somewhere in your `$PATH` 31 | 32 | ## Bash completion 33 | 34 | If you want bash completion, source the `bashcompletion.sh` or `bashcompletion.zsh.sh` 35 | 36 | ## Usage 37 | 38 | For default environment, you can just use the group name as a parameter: 39 | 40 | `ansishell web` 41 | 42 | For a specific environment, use it the first parameter: 43 | 44 | `ansishell dev web` 45 | 46 | Unsure which groups are configured? Use group listing of an environment: 47 | 48 | `ansishell dev -l` 49 | 50 | Need a list of environments? 51 | 52 | `ansishell -e` 53 | -------------------------------------------------------------------------------- /bashcompletion.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | _script() 3 | { 4 | local envs env group 5 | COMPREPLY=() 6 | env="${COMP_WORDS[1]}" #first parameter 7 | 8 | #get list of environments 9 | envs=$(ansishell -e| tail -n +2 | tr "\n" " ") 10 | 11 | # given autocomplete is not an environment 12 | # autocomplete with default environment instead 13 | if ! [[ "${envs[@]}" =~ "${env}" ]]; then 14 | env="default" 15 | fi; 16 | 17 | _script_commands=$(ansishell ${env} -l| tail -n +2 | tr "\n" " ") 18 | COMPREPLY=( $(compgen -W "${_script_commands}" ) ) 19 | 20 | return 0 21 | } 22 | complete -o nospace -F _script ansishell -------------------------------------------------------------------------------- /bashcompletion.zsh.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env zsh 2 | autoload bashcompinit 3 | bashcompinit 4 | 5 | source $(dirname $0)/bashcompletion.sh -------------------------------------------------------------------------------- /bin/ansishell: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | from ansible import inventory 3 | import subprocess 4 | import configparser 5 | import argparse 6 | from os.path import expanduser, isfile 7 | from ansible.parsing.dataloader import DataLoader 8 | from ansible.vars import VariableManager 9 | 10 | 11 | loader = DataLoader() 12 | variable_manager = VariableManager() 13 | dotfile = configparser.ConfigParser() 14 | home = expanduser("~") 15 | dotfile_path = home + "/.ansishell" 16 | 17 | if not isfile(dotfile_path): 18 | print("~/.ansishell config not found") 19 | exit(1) 20 | 21 | dotfile.read(dotfile_path) 22 | 23 | argparser = argparse.ArgumentParser() 24 | 25 | environments = dotfile.options("inventory") 26 | config = dotfile.options("config") 27 | 28 | # TODO check for missing defaults 29 | 30 | argparser.add_argument("env", help="name of environment", default="default", nargs="?") 31 | argparser.add_argument("group", help="ansible group", nargs="?") 32 | argparser.add_argument("-l", help="list groups", action="store_true") 33 | argparser.add_argument("-e", help="list environments", action="store_true") 34 | args = argparser.parse_args() 35 | 36 | first_argument = args.env 37 | second_argument = args.group 38 | 39 | if second_argument is None and not args.l: 40 | # We only get one argument. Use default env and set given parameter as group 41 | env = "default" 42 | group = first_argument 43 | else: 44 | env = first_argument 45 | group = second_argument 46 | 47 | inventory_path = dotfile.get("inventory", env) 48 | shell_command = dotfile.get("config", "command") 49 | 50 | inventory_manager = inventory.Inventory(loader, variable_manager, inventory_path) 51 | 52 | if args.e: 53 | print("Configured groups:") 54 | for environment in environments: 55 | print(environment) 56 | exit(0) 57 | 58 | if args.l: 59 | inventory_manager = inventory.Inventory(loader, variable_manager, inventory_path) 60 | print("Groups in %s:" % inventory_path) 61 | for group_item in inventory_manager.list_groups(): 62 | print(group_item) 63 | exit(0) 64 | 65 | hosts = inventory_manager.list_hosts(group) 66 | if len(hosts) == 0: 67 | print("No hosts found") 68 | exit(1) 69 | 70 | cmd_args = [shell_command] + [str(x) for x in hosts] # convert hostlist to strings explicitly 71 | 72 | subprocess.call(cmd_args) --------------------------------------------------------------------------------