Ansible – exclude host from playbook execution

By using –limit argument with ansible-playbook command we can exclude a host from playbook execution.
If hostname starts with “!” it will excluded from host execution.

Lets say if we want to exclude host1 and host2 from ansible-playbook execution use following command:

$ ansible-playbook --limit '!hoost1:!host2' yourPlaybook.yml

To exclude only host1 from execution use following command:

$ ansible-playbook --limit '!hoost1' yourPlaybook.yml

To execute only in host1 and host2 from execution use following command:

$ ansible-playbook --limit 'hoost1:host2' yourPlaybook.yml

To execute only in host1 use following command:

$ ansible-playbook --limit 'hoost1' yourPlaybook.yml

To exclude host1 and host2 from execution and allow execution only in host3:

$ ansible-playbook --limit '!hoost1:!host2:host3' yourPlaybook.yml

Python – check if ip is public or private


netaddr is a Python library for representing and manipulating network addresses.

Installing netaddr library:

Use any of following command to install netaddr library

$ sudo pip install netaddr
$ easy_install netaddr

Observe following example for more details:

>>> from netaddr import *
>>> IPAddress('').is_private()
>>> IPAddress('').is_private()
>>> IPAddress('').is_loopback()

IPAddress(‘input ip’).is_private() will return true if the input ip address private, else it will return false.

Reference: Netaddr

Kibana connection failed elasticsearch 1.4

While trying to setup ELK(Elastic Search, Logstash and Kibana) I got an error saying “Connection Failed”

If you are suing elastic search version 1.4 or above this issue will occur.

To resolve it append following lines in elasticsearch.yml file(which is in config directory of elastic search):

http.cors.allow-origin: "/.*/"
http.cors.enabled: true

After adding above lines restart elasticsearch.

This should fix “Connection Failed” issue.

Ganglia – rrd remove negative spikes


By using rrdtool tune we can remove negative spikes from any rrd(round robin database).


$ rrdtool tune <RRD_FileName> --minimum ds-name:min

where, ds-name is the name you will use to reference this particular data source from an RRD.

min is minimum value, if we set minimum value of zero, it prevents negative rates.

In this example I am using test.rrd as my input rrd file.

To get ds-name first extract rrd file and with following command:

$ rrdtool dump test.rrd

Now look for following block in extracted data:

<name> sum </name>
<type> GAUGE </type>

<!– PDP Status –>
<unknown_sec> 0 </unknown_sec>

In above output value between <name> and </name> is ds-name, so in above example sum is ds-name.

To remove negative run following commands:

#Set minimum value
$ rrdtool tune test.rrd --minimum sum:0
#Save new rrd
$ rrdtool dump test.rrd | rrdtool restore --range-check - test.rrd-fix
#Replace old rrd with new rrd
$ mv test.rrd-fix test.rrd

After running above 3 commands you wont see any negative values test.rrd

If the rrd is ganglia’s rrd you need to change permissions of rrd otherwise due to permissions new data won’t get updated to this rrd.

Use following command to change rrd permission:

$ chown nobody:root test.rrd

Python – find files in directory with extension

The glob module finds all the pathnames matching a specific pattern according to the rules used by the Unix Shell. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.


To list all files ending with *.txt in /home/guest directory:

>>> import glob
>>> glob.glob("/home/guest/*.txt")

[‘/home/guest/syslog.txt’, ‘/home/guest/mysql.txt’, ‘/home/guest/topics.txt’, ‘/home/guest/bus.txt’]

Similarly you can try others.

As mentioned above No tilde expansion will work, so following example will return empty list.

>>> glob.glob("~/guest/*.txt")


Username and password with wget/curl


To download a url with basic authentication we need to pass username & password.

To acheive this functionality with wget or curl observer following examples:

With curl:

$ curl -u 'username:password' ''

With wget:

$ wget --user='username' --password='password' ''

Wget with cookie:

Login to server & save cookie to a file:
$ wget --save-cookies cookiesFile.txt --post-data 'user=username&password=password'

Download a page by using saved cookie file:
$ wget --load-cookies cookiesFile.txt -p

Python – get system hostname


By using socket.gethostname() we can get hostname of system:

>>> import socket
>>> socket.gethostname()

There is one more way to get hostname is by using platform.node():

>>> import platform
>>> platform.node()

Ubuntu – apt-get auto complete not working

To fix apt-get auto complete issue we need to edit /etc/bash.bashrc file.

Open /etc/bash.bashrc file with any text editor, here I am using vim.

$ vim /etc/bash.bashrc

Now search for below lines in /etc/bash.bashrc file:

# enable bash completion in interactive shells
# if ! shopt -oq posix; then
#  if [ -f /usr/share/bash-completion/bash_completion ]; then
#    . /usr/share/bash-completion/bash_completion
#  elif [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion
#  fi
# fi

Uncomment external if condition(uncomment all line except first line), after uncomment it look like below:

# enable bash completion in interactive shells
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion

Next save and exit /etc/bash.bashrc file and reopen terminal.

I got this issue after installing Ubuntu 14.04 64 bit desktop edition & and resolved it by following above mentioned procedure.