Skip to content

Instantly share code, notes, and snippets.

@a4180p
Created August 8, 2016 08:53
Show Gist options
  • Select an option

  • Save a4180p/52a4d5a9223e60b9ffb03eacfdb54dc0 to your computer and use it in GitHub Desktop.

Select an option

Save a4180p/52a4d5a9223e60b9ffb03eacfdb54dc0 to your computer and use it in GitHub Desktop.
create tmux window with ssh sessions to all hosts in service and syncronized input
#!/usr/bin/env python
# requirements: ansible, libtmux
# you must set env var INVENTORIES_PATH
import argparse
from ansible.inventory import Inventory
import libtmux as tmux
import os
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--environ")
parser.add_argument("-s", "--system")
args = parser.parse_args()
if args.system is None:
sys.exit("you must specify system")
environ = args.environ or "test"
inv_path = "/".join([os.getenv("INVENTORIES_PATH"), environ, "inventory"])
inventory = Inventory(inv_path)
tmx = tmux.Server()
sessions = tmx.list_sessions()
if not sessions:
session = tmx.new_session()
else:
session = sessions[0]
w = session.new_window()
hosts = inventory.get_group(args.system).hosts
h = int(session.height) // len(hosts)
while len(w.panes) < len(hosts):
p = w.split_window()
p.set_height(h)
w.select_layout(layout="even-vertical")
for p,h in zip(w.panes, hosts):
p.send_keys("ssh " + h.name)
w.set_window_option("synchronize-panes", True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment