Created
October 18, 2012 19:34
-
-
Save rubiojr/3914299 to your computer and use it in GitHub Desktop.
Run an arbitrary command on every puppet node registered to the puppet master
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'excon' | |
require 'json' | |
require 'net/dns' | |
include Net::DNS | |
# | |
# Run an arbitrary command using parallel-ssh against all the nodes | |
# registered in the puppet master | |
# Needs pssh (parallel-ssh) installed. | |
# | |
# Usage: puppet-node-run <command> | |
# | |
# i.e. puppet-node-run puppet agent -t | |
# | |
# Replace with the URL of your puppet master | |
MASTER_URL = "http://puppet:8080/nodes" | |
DNS_SERVER = "my-dns-server-address" | |
if ARGV.empty? | |
$stderr.puts "Invalid command." | |
$stderr.puts | |
$stderr.puts "Usage: puppet-node-run <command> <args>" | |
exit 1 | |
end | |
out = Excon.get MASTER_URL | |
nodes = JSON.parse out.body | |
# | |
# Optionally resolve names using specific DNS server | |
# | |
# res = Resolver.new | |
# res.nameservers = DNS_SERVER | |
# | |
# Generate the list of nodes for parallel-ssh | |
puts "Generating hostlist..." | |
File.open '/tmp/hostlist', 'w' do |f| | |
nodes.each do |i| | |
address = i | |
# try to resolve before writing the list | |
# address = res.query(i).answer.first.address rescue next | |
f.puts "#{address} root" | |
end | |
end | |
# | |
# Run the command, ignoring bad host keys (useful while in development) | |
# The output for every node is saved in /tmp/root@XXXXXX | |
# | |
$stdout.sync = true | |
command = ARGV.join(' ') | |
puts "Running command '#{command}' with parallel-ssh..." | |
system 'parallel-ssh -o /tmp -t 120 -h /tmp/hostlist -x "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" ' + "'#{command}'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment