ansible with docker dynamic inventory

So, I have a few dockers. Every now and then I want to run some command on all of them. Doing ‘docker exec’ is tiresome. I found this neat solution with ansible that I thought I should share with you.

To get started, you need to have the “docker.py” script. This script will be used as python script inventory for ansible. So, use the following command and get the script:

curl 'https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/docker.py' -o docker.p

Once done, check that all is well, with :

python docker.py

You should not get any errors or warnings and see a list of dockers running on your host. If you get error for docker-py then you can install the same with :

pip install docker-py

And now is the good time to open the docker.py script to check the documentation. It adds all the docker hosts as entry in itself. But cool  thing I liked is the fact that it creates a group for all running dockers – which is very very useful.

 

Now, if I want to set the Timezone (TZ) on all the running dockers in one go, I can do this:

ansible -i docker.py -m shell -a 'rm -f /etc/localtime; ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime' running

And just in case, the above does not work for you and you have a common username/password on all the machines, then another thing you would love is following:

# Export the docker host. If that is localhost, then you do not need this.
export DOCKER_HOST=tcp://192.168.122.1:4243
# This is GOOD to set option. This is the default IP address for docker
# and setting this to first IP address of the docker network is good idea
# that would ensure that ansible can login to each docker.
# And note - you need to have ssh running on all the dockers for the commands to work
export DOCKER_DEFAULT_IP=192.168.122.1
ansible -i docker.py -m setup -u root  -a 'filter=ansible_eth[0-2]'  all

Executing commands on multiple hosts

If you have to execute the same command in multiple hosts, then you can use mussh:

Description : Mussh is a shell script that allows you to execute a command or script
: over ssh on multiple hosts with one command. When possible mussh will use
: ssh-agent and RSA/DSA keys to minimize the need to enter your password
: more than once.

First install mussh with the following command

dnf install mussh

Now to run this for multiple hosts, you can run like this

mussh -h vm{1..2} -c "hostname"

This will run the commands on hosts – vm1 and vm3
Command that will be run is “hostname”
Similarly you can run other commands as well on multiple machines.

script to get hard disk health in fedora/ubuntu

First, put this in a script.

#!/bin/bash

#Change as appropriate
HDD=sda

export sub="SmartCtl data for HDD"
echo 'To: <Your Email>
Sub: [Cron] $sub
MIME-Version: 1.0
Content-Type: text/html
Content-Disposition: inline

<html><pre>'


echo '<style>

hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid #ccc;
    margin: 1em 0;
    padding: 0;
}
</style>
'

echo '<h2>Errors on HDD:</h2>'
echo '<hr>'
sudo smartctl -l error  /dev/$HDD
echo '<h2>Health of HDD:</h2>'
echo '<hr>'
sudo smartctl -H  /dev/$HDD

echo '<h2>Detailed info</h2>'
echo '<hr>'
sudo smartctl -a -d ata /dev/$HDD

echo '<h2>Journalctl output for smartd</h2>'
echo '<hr>'

if [[ -f /etc/fedora-release ]]
then
journalctl -x --show-cursor -u smartd --since=yesterday
else
#This is for Ubuntu.. still using dmesg

dmesg -T -l err,crit,alert,emerg

fi


echo '<h2>All Details</h2>'
echo '<hr>'

sudo smartctl --xall /dev/$HDD


echo "Thanks for using Amit Agarwal's script"
echo '</pre></html>'

and then put this in cron:

30 15 * * 5 <script name> |/usr/sbin/sendmail -t

You need to have mail configured to receive the mails.