Xargs

So I have used xargs for years but never real thought much about it as when I wanted to do something more advanced … like reorder output I would simple use awk.

Normally you run xargs as:

jmiller@srv-101-03:~$ echo "sample" | xargs ls -l
-rw-r--r-- 1 jmiller jmiller 0 2010-04-29 15:18 sample
jmiller@srv-101-03:~$

but what if you want to do something more advanced like

echo “sample” | xargs -i echo {} two

work calls will have to finish later

chef, knife, and ssh – loving it!

Opscode added a ssh call to the knife utility which when used with the search syntax can be very nice. A few minor examples below.

Opscode added a ssh call to the knife utility which when used with the search syntax can be very nice. A few minor examples below.

jmiller@srv-101-03: $ knife ssh role:APACHE_ROLE uptime
srv-101-18.example.com  02:07:24 up 140 days, 23:23,  1 user,  load average: 0.00, 0.00, 0.00
srv-101-17.example.com  02:07:24 up 125 days, 10:53,  1 user,  load average: 0.03, 0.06, 0.02

j

miller@srv-101-03:~/operations/chef/roles$ knife ssh “role:BASE_ROLE” ‘ grep paranoia /etc/nscd.conf ‘
srv-101-01.example.com # paranoia
srv-101-01.example.com paranoia no
srv-101-14.example.com # paranoia
srv-101-14.example.com paranoia yes
srv-201-22.example.com # paranoia
srv-201-22.example.com paranoia yes
srv-201-01.example.com # paranoia
srv-201-01.example.com paranoia yes
srv-201-26.example.com # paranoia
srv-201-26.example.com paranoia yes
srv-101-04.example.com # paranoia
….

Backup chef roles

I like to keep my chef roles in git so I do a dump of them and check them when I make changes. Very nice if you remove something and can not recall what it is.

I like to keep my chef roles in git so I do a dump of them and check them when I make changes. Very nice if you remove something and can not recall what it was as you jump around.

#!/bin/bash

####
#
# Must be run from a server that has knife and your key i.e. chef.int.rdio
#
###

# List of all roles:

knife role list | sed s/\”//g | sed s/,// | egrep -v ‘\]|\[‘ > ./rolelist.txt

# Generate a file for each role containing the servers in that role

for i in `cat rolelist.txt`; do echo $i; knife role show $i > $i.json; done

Quick and dirty server list from chef

So I have always used a simple bash look to do quick task on lots of servers:

Example:

for i in `cat server.list`; do ssh $i ‘hostname;uptime’;done

We can use chef to build list of servers by role, and a list all servers in a our farm if managed by chef 🙂

#!/bin/bash

####
#
# Must be run from a server that has knife and your key i.e. chef.server.com
#
###

# I think I am going to make this a recipe
# but for now…

#Generate a list of all chef controlled servers

knife node list | sed s/\”//g | sed s/,// | grep -v \] > /home/operations/servers/all.txt

# List of all roles:

knife role list | sed s/\”//g | sed s/,// | egrep -v ‘\]|\[‘ > /home/operations/servers/roles.txt

# Generate a file for each role containing the servers in that role
# Tetsu likes the files lower case … works for me 🙂

for i in `cat roles.txt`; do echo $i; z=`echo $i | tr ‘[:upper:]’ ‘[:lower:]’`; knife search node role:$i -i > $z.txt; done