Search This Blog

Monday, February 10, 2014

Howto deploy cowsay to all your cloud servers using ansible configuration management

There are many devops tools that you can use. An example tool chain list can be found http://www.rackspace.com/devops/ and if you are more interested in devops in general make a habit to regularly checking our blog at http://developer.rackspace.com/blog.

Demonstration

In this post we are going to create number of fresh cloud servers: mybastion and 7 serverX
We will install ansible on the mybastion.
Once ansible is installed we will configure our remaining serversX.

Howto and results description
  • Spin up cloud servers
We are going to use our helper program http://rtomaszewski.blogspot.co.uk/2013/12/how-to-automatically-deploy-your-public.html to spin up the cloud servers first.
 
auxnova --image 80fbcb55-b206-41f9-9bc2-2dd7aac6c061 --flavor 2 mybastion &> mybastion.log
for i in $(seq 1 7); do echo $i; auxnova --image 80fbcb55-b206-41f9-9bc2-2dd7aac6c061 --flavor 2 server$i &> server${i}.log; done
  • Install ansible on mybastion host
auxssh bastion-ip  
auxnova --image 80fbcb55-b206-41f9-9bc2-2dd7aac6c061 --flavor 2 mybastion &> mybastion.log
for i in $(seq 1 7); do echo $i; auxnova --image 80fbcb55-b206-41f9-9bc2-2dd7aac6c061 --flavor 2 server$i &> server${i}.log; done

aptitude update
aptitude upgrade

aptitude install libc6-dev  

aptitude install python-dev
aptitude install python-pip

# http://docs.ansible.com/intro_installation.html
git clone git://github.com/ansible/ansible.git
pip install paramiko PyYAML jinja2 httplib2

cd ansible 
make 
make install

export PATH=$PATH:/usr/local/bin/
  • Configure ansible on mybastion
cp -vb /root/ansible/examples/ansible.cfg ~/
sed 's/#host_key_checking = False/host_key_checking = False/g' ansible.cfg > ansible.cfg.old && mv ansible.cfg.old ansible.cfg
sed 's/\(transport *= \)\(.*\)/\1 paramiko/' ansible.cfg  > ansible.cfg.new && mv -v ansible.cfg{.new,}

root@mybastion:~# egrep 'host_key_checking|transport' ansible.cfg | column -t
transport          =  paramiko
host_key_checking  =  False
  • Update the host file for static DNS names
for i in $(seq 1 7); do nova show server$i | egrep 'accessIPv4| name' | cut -d '|' -f3 | xargs -n2 | awk '{ print $2, $1}'; done
162.13.9.170 server1
162.13.9.102 server2
162.13.11.232 server3
162.13.9.215 server4
162.13.9.59 server5
162.13.11.227 server6
162.13.11.222 server7

cat >> /etc/hosts <<END
162.13.9.170 server1
162.13.9.102 server2
162.13.11.232 server3
162.13.9.215 server4
162.13.9.59 server5
162.13.11.227 server6
162.13.11.222 server7
END
  • Configure ansible inventory file
cp -b ansible_hosts{,.old}
rm ansible_hosts
for i in $(seq 1 7); do 
  cat server${i}.log | egrep ' name|adminPass' | cut -d '|' -f 3 | xargs -n2 | sed 's/\([^ ]*\) *\(.*\)/\1 ansible_ssh_user=root ansible_ssh_pass=\2/' >> ansible_hosts;
done

root@mybastion:~# cat  ansible_hosts
server1 ansible_ssh_user=root ansible_ssh_pass=JaD2KJYswZ92
server2 ansible_ssh_user=root ansible_ssh_pass=keic343S3uJt
server3 ansible_ssh_user=root ansible_ssh_pass=2SNvD4cLN36m
server4 ansible_ssh_user=root ansible_ssh_pass=T7VKkm3GLjKm
server5 ansible_ssh_user=root ansible_ssh_pass=ghdWJeKRRt2f
server6 ansible_ssh_user=root ansible_ssh_pass=N2qVsxt93Lfb
server7 ansible_ssh_user=root ansible_ssh_pass=yMgS5Jx4zexM
  • Run a test to verify ansible and inventory is setup correctly
root@mybastion:~# ansible all -m ping -i ansible_hosts
server1 | success >> {
    "changed": false,
    "ping": "pong"
}

server3 | success >> {
    "changed": false,
    "ping": "pong"
}

server4 | success >> {
    "changed": false,
    "ping": "pong"
}

server2 | success >> {
    "changed": false,
    "ping": "pong"
}

server5 | success >> {
    "changed": false,
    "ping": "pong"
}

server7 | success >> {
    "changed": false,
    "ping": "pong"
}

server6 | success >> {
    "changed": false,
    "ping": "pong"
}
  • Run a simple command
ansible all -i ansible_hosts -a date
ansible all -i ansible_hosts -m command -a /bin/date

server1 | success | rc=0 >>
Mon Feb 10 01:43:49 UTC 2014

server3 | success | rc=0 >>
Mon Feb 10 01:43:49 UTC 2014

server2 | success | rc=0 >>
Mon Feb 10 01:43:49 UTC 2014

server4 | success | rc=0 >>
Mon Feb 10 01:43:49 UTC 2014

server5 | success | rc=0 >>
Mon Feb 10 01:43:49 UTC 2014

server7 | success | rc=0 >>
Mon Feb 10 01:43:50 UTC 2014

server6 | success | rc=0 >>
Mon Feb 10 01:43:50 UTC 2014
  • Use ansible modules to install the packages on the serverX
ansible all -i ansible_hosts -m apt -a 'name=cowsay state=installed'
server1 | success >> {
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (166 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 50438 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}

server4 | success >> { 
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (861 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 49847 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}

server3 | success >> {
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (1075 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 49847 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}

server5 | success >> {
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (1116 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 49847 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}

server2 | success >> {
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (108 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 49847 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}

server6 | success >> {
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (1292 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 49847 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}

server7 | success >> {
    "changed": true,
    "stderr": "",
    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSuggested packages:\n  filters\nThe following NEW packages will be installed:\n  cowsay\n0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nNeed to get 19.9 kB of archives.\nAfter this operation, 287 kB of additional disk space will be used.\nGet:1 http://mirror.rackspace.com/ubuntu/ precise/universe cowsay all 3.03+dfsg1-3 [19.9 kB]\nFetched 19.9 kB in 0s (1056 kB/s)\nSelecting previously unselected package cowsay.\n(Reading database ... 49847 files and directories currently installed.)\nUnpacking cowsay (from .../cowsay_3.03+dfsg1-3_all.deb) ...\nProcessing triggers for man-db ...\nSetting up cowsay (3.03+dfsg1-3) ...\n"
}
  • Run the cow program on all hosts and collect results
ansible all -i ansible_hosts -m shell -a 'executable=/bin/bash cowsay lucky number on $HOSTNAME is  $RANDOM'
server1 | success | rc=0 >>
 _________________________________
< lucky number on server1 is 9420 >
 ---------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

server2 | success | rc=0 >>
 __________________________________
< lucky number on server2 is 12466 >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

server4 | success | rc=0 >>
 __________________________________
< lucky number on server4 is 27268 >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

server5 | success | rc=0 >>
 __________________________________
< lucky number on server5 is 26818 >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

server3 | success | rc=0 >>
 __________________________________
< lucky number on server3 is 17164 >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

server6 | success | rc=0 >>
 __________________________________
< lucky number on server6 is 28732 >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

server7 | success | rc=0 >>
 _________________________________
< lucky number on server7 is 6655 >
 ---------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

References

http://www.ansible.com/home
http://docs.ansible.com/intro_getting_started.html
http://docs.ansible.com/intro_adhoc.html
http://docs.ansible.com/list_of_packaging_modules.html

https://github.com/rtomaszewski/dotfiles/blob/master/.bashrc_rado_aux


No comments:

Post a Comment