martes, 30 de abril de 2013

Installing and executing Ansible on Ubuntu

This time is shown how to install and deploy and Ansible installation, also how to configure myhost to set a group of hosts to issue Ansible commands.

It is easy and simple. http://ansible.cc/


Ansible is useful if you want sto perform multiple aoperation on remote hosts; for example to install a big data database or to configure a multinode hadoop cluster. You can also send requests to find particular information and work with the result data; perhaps you need maintenance to cleanup your temporal file or create a cron job for all hosts, or some, etc.


Here you will see how to issue simple ansible commands and basic ansible-playbooks with an ease java-6-oracle example.


To install it on Ubuntu:


  # sudo add-apt-repository ppa:rquillo/ansible
  # sudo apt-get update
  # sudo apt-get install ansible -y


It is IMPORTANT to add to authorized_keys the uder (id_rsa,pub) that will issue Ansible Commands, in this case it is the same host. IF YOU DON'T have an id_rsa, then create it.

To create id_rsa:   (Make sure you don't have one already)

  # ssh-keygen            
               
Then cat it to copy it pubkey content and add it to authorized keys, remember in this examplo it is in the same authorized keys of the same host.

  # cat .ssh/id_rsa.pub 
  # vi .ssh/authorized_keys 




Now you can add localhost to myhosts to do a quick test:

   # echo localhost > myhosts
   # export  ANSIBLE_HOSTS=$(pwd)/myhosts
   # echo $ANSIBLE_HOSTS
   # ansible all -m ping -u ubuntu


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


There you go, Ansible works, what have we done so far? Just test it, you can ccreat at myhosts a set of remote instances by adding to my hosts a tag like:

     # vi myhosts

 
     [my_remote_servers]
     10.40.207.50
     10.40.207.82

In this case you can issue Ansible commands for a particular set of my_remote_servers like:

   # ansible my_remote_servers -m ping -u ubuntu

You can see now that only the servers listed in my_remote_servers will respond to ping.


What can we do? all that the specified user can do, for example:


     # ansible gazzang  -a "ls /home/ubuntu" -u ubuntu
     10.40.207.50 | success | rc=0 >>
     dir_test
     file.txt

   10.40.207.82 | success | rc=0 >>
     my_directory
     this_is_a_file.doc




Remember that for 10.40.207.50 and 10.40.207.82 the id_rsa_pub should've been added to their corresponding autrorized keys; if not the Ansible command will fail with:


     # ansible gazzang  -a "ls /home" -u ubuntu
     10.40.207.50 | FAILED => FAILED: Authentication failed.

IF YOU ARE PLANNING TO ISSUE SUDO COMMANDS, then remember to add to /root/.ssh/authorized keys the id_rsa.pub.  Or add to the non-root user root permissions, just be careful.



Wanna go further? of course yo do!

Follow are two YAML files, these are ansible-playbooks, you can see that we are installing java-6-oracle from PPA and we acre including one YAML into another.


# This playbook just includes a second playbook
- hosts: ec2
  vars:
    reponame: stable
    distro_release: precise
  user: ubuntu
  sudo: yes
  tasks:
  - include: install_java6oracle_on_ubuntu.yml


  #First operations
  - name: Describe first task
    shell: echo "This is the issued command"


  - name: Describing second task
    shell: sudo gpg --keyserver pgpkeys.mit.edu --recv-key  D2B6F0B7FADF302F



 install_java6oracle_on_ubuntu.yml:



You can do many operations on the different ansible-platbook tasks, this is just a small set of them.

  - name: Changing the cluster name at /etc/cassandra/cassandra.yaml and the listen_address
    shell: sudo cat $cassandra_orig  | sed "s/Test Cluster/Cassandra Production Cluster/" | sed "s/listen_address:\ localhost/listen_address:\ \"$( ifconfig  | grep Bcast | awk '{print
substr($2,6)}')\"/" | sed "s/seeds:\ \"127.0.0.1\"/seeds:\ \"$seed_ip\"/" | sed "s/rpc_address:\ localhost/rpc_address:\ 0.0.0.0/"    > $cassandra_tmp


You can save stdout at a ansible variable from prompt commands by using the operation register:

  - name: Get Cassandra PID
    shell: ps aux | grep cassandra | grep oracle | grep root | grep -v ansible | awk '{ print($2) }'
    register: cassandra_pid

  - name: Creating cassandra profile
    shell: sudo zncrypt-profile  --pid=${cassandra_pid.stdout} > $cassandra_profile_file




Have Fun!
.Alex

P.D. Flag has no reverse meaning (useless)

jueves, 11 de abril de 2013

Install Java6-sun on scripts with non-interactive (unattended) mode

This has been tested on ubuntu 10.04 and ubuntu 12.04

Follow is a python function that does that, you can see the commands:

For ubuntu 10.04:

def install_java_6(status):
        output = ""
    status, output = execute('sudo add-apt-repository ppa:sun-java-community-team/sun-java6')
    if status == 0:
        status, output = execute('sudo apt-get update')
    if status == 0:
        status, output = execute('sudo sh -c \'echo sun-java6-jre shared/accepted-sun-dlj-v1-1 select true | /usr/bin/debconf-set-selections\' ')
    if status == 0:
        status, output = execute('sudo apt-get install -y sun-java6-jdk')
    if status == 0:
        status, output = execute('sudo update-java-alternatives -s java-6-sun')
        status = 0;
    return status,output
exit




For Ubuntu 12.04:

def install_java_6(status):
        output = ""
        #2.4 status, output = execute('sudo add-apt-repository ppa:sun-java-community-team/sun-java6')
        status, output = execute('sudo add-apt-repository ppa:webupd8team/java -y')
        if status == 0:
            status, output = execute('sudo apt-get update')
        if status == 0:   
            status, output = execute(' echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections ')
            status, output = execute(' echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections ')
        if status == 0:
            #2.4 status, output = execute('sudo apt-get install -y sun-java6-jdk')i
            status, output = execute('sudo apt-get install oracle-java6-installer')
        if status == 0:
            #status, output = execute('sudo update-java-alternatives -s java-6-sun')
            status = 0;
        return status,output
exit


IMPORTANT, debconf is receiving select and seen, If you miss selelect you will receive an error like:

error: Cannot find a question for shared/accepted-oracle-license-v1-1


For both 10.04 and 12.04 the execute function is:


def execute( cmd ):
    print "\n\n Executing :" + cmd
    status, output = commands.getstatusoutput(cmd)
    print "status: " + str(status)
    print "output: " + output
    return status, output
exit




If you want to uninstall java-6-oracle:

# ubuntu@ip-10-136-0-17:~$    sudo apt-get purge oracle-java6-installer


Enjoy!

P.D. Should exist a Cuernavaca's flag!


Add and prepare a Volume to an EC2 Instance

Well this is simple and will document it just for fun! =)


You can attach a volume to an EC2 instance by:

1. creating the volume at EC2 - > Volumes -> Create volume

Select the size and create it. It is IMPORTANT that the volume is created in the same Geography and lab, i.e. If an instance is un us-east-1a and your volume is in us-east-1b you will not be able to attach the volume; use us-east-1a for both for example.


Once it is created

2. Right click on the volume -> Attach Volume

Then select the instance you want to attach to.

See that volume state will be attaching for a while; in my case I did it when the instance was running, I guess this also works for stopped instances.

Ok, so now we have to format, mount and att to fstab our new device:


ssh your EC2 instance.

If our Volume is named: /dev/xvdf and we want to use an ext4 filesystem:

sudo mkfs.ext4 /dev/xvdf

Then mount to a particular directory

mkdir /mnt_large

sudo mount /dev/xvdf /mnt_large

Finally add to fstab:


Follow is the original content:

LABEL=cloudimg-rootfs   /        ext4   defaults        0 0
/dev/xvdb       /mnt    auto    defaults,nobootwait,comment=cloudconfig 0       2


Add your device line and final result will be:

LABEL=cloudimg-rootfs   /        ext4   defaults        0 0
/dev/xvdb       /mnt    auto    defaults,nobootwait,comment=cloudconfig 0       2
/dev/xvdf       /mnt_large    auto    defaults,nobootwait,comment=cloudconfig 0       2


For details on fstab see: https://help.ubuntu.com/community/Fstab


Why do we add it to fstab? well if you reboot you can make sure that your device will be automatically mounted.

That's it!

P.D. I should say that there are not ugly flags.