Category Archives: ubuntu

Step by Step procedure to install and Configure ANSIBLE on Ubuntu

Step by step procedure to install and configure ansible on ubuntu

Before installing ANSIBLE, let us understand,

What is ANSIBLE ?

1.Ansible Introduction :

Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero
downtime rolling updates.
Ansible’s goals are foremost those of simplicity and maximum ease of use. It also has a strong focus on security and reliability, featuring a minimum of moving parts, usage of OpenSSH for transport (with an accelerated socket mode and pull modes as alternatives),
and a language that is designed around auditability by humans – even those not familiar with the program.

While there are many popular configuration management systems available for Linux systems, such as Chef and Puppet, these are often more complex than many people want or need.Ansible is a great alternative to these options because it has a much smaller overhead to get started.

for more information visit, Ansible docs.
http://docs.ansible.com/

Assumptions
● You have a ubuntu 12.04 and you are logged-in as root.

2.How Does Ansible Work :
Ansible works by configuring client machines from an computer with Ansible components
installed and configured.
It communicates over normal SSH channels in order to retrieve information from remote
machines, issue commands, and copy files. Because of this, an Ansible system does not require any additional software to be installed on the client computers.
This is one way that Ansible simplifies the administration of servers.
Configuration files are mainly written in the YAML data  format due to its
expressive nature and its similarity to popular markup languages.Ansible can interact with clients through either command line tools or through its configuration scripts called Playbooks.

3. Install Ansible on an Ubuntu 12.04 :
To begin exploring Ansible , we need to install the Ansible package on at least one machine. I have used ubuntu 12.04

git clone git@github.com:ansible/ansible.git
cd ansible
source ./hacking/env-setup
sudo pip install paramiko PyYAML jinja2 --upgrade

We now have all of the software required to administer our servers through Ansible.
Set Up SSH Keys , Ansible primarily communicates with client computers through SSH.While it certainly has the ability to handle password-based SSH authentication, SSH keys help keep things simple.

4. Creating a SSH Pair :
@Server:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory ‘/home/a/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
Now use ssh to create a directory ~/.ssh as user b on Client. (The directory may already exist,
which is fine):
@Server:~> ssh root@123.123.123.123 mkdir -p .ssh
root@Client’s password:
Finally append a’s new public key to root@123.123.123.123:.ssh/authorized_keys and enter
client’s password one last time:
root@Server:~> cat .ssh/id_rsa.pub | ssh root@123.123.123.123 ‘cat >> .ssh/authorized_keys’
root@Client’s password:
From now on you can log into Client as root from A as a without password:
@Server:~> ssh root@123.123.123.123

5. Configuring Ansible Host:
Ansible keeps record of all of the servers that it know through a “hosts” file. We need to configure this file first before we can begin to communicate with our other computers.
Open the file and add,

vim /etc/ansible/hosts
[hosts]
devops ansible_ssh_host=123.123.123.123

Where hosts is list of nodes
devops = host name of one of the client.
123.123.123.123 is hosts ip.
To do this, we will create a directory in the Ansible configuration structure called group_vars.
Within this folder, we can create YAML-formatted files for each group we want to configure:

sudo mkdir /etc/ansible/group_vars
sudo nano /etc/ansible/group_vars/hosts

We can put our configuration in here. YAML files start with “—“, so make sure you don’t forget that part.

ansible_ssh_user: root
Save and close this file when you are finished.

6. Ansible test Examples:

Using Simple Ansible Commands
Now that we have our hosts set up and enough configuration details to allow us to
successfully connect to our hosts, we can try out our very first command.
Ping all of the servers you configured by typing:
root@server:/etc/ansible# ansible -m ping hosts
devops | success >> {
“changed”: false,
“ping”: “pong”
}
This is a basic test to make sure that Ansible has a connection to all of its hosts.

ansible -m shell -a ‘ifconfig’ devops

devops | success | rc=0 >>
eth0 Link encap:Ethernet HWaddr 00:0c:29:a2:42:c4
inet addr:123.123.123.123 Bcast:10.27.111.255 Mask:255.255.255.0

ansible -m shell -a ‘free -m’ devops
devops | success | rc=0 >>
total used free shared buffers cached
Mem: 487 440 47 0 108 173
-/+ buffers/cache: 158 329
Swap: 507 66 441

Lets install sendmail on client from Ansible server, to do that create playbook.yml file in
/etc/ansible 100 Jul 5 02:48 playbook.yml

root@server:/etc/ansible# cat playbook.yml

– hosts: devops
tasks:
– name: 1. install sendmail
apt: name=sendmail state=present

Now execute below command to install on remote server.
ansible-playbook –inventory-file=hosts playbook.yml –sudo –verbose
Below is the result,

ansible 1

Before executing above command, sendmail was not installed. ansible 2

Later after execution, sendmail installed on the remote host.

Advantages of Automation,

,Automation-Advantages

Hence we have Installed and Configured ANSIBLE, Enjoy !!!

shell script to replace a string in a file

shell script to replace a string in a file

Some times during automating any task, we may require to replace a string in a file.
Example:- I want to change “Linux” string with “Waseem” in a file called string.txt

cat replacestring.sh
#!/bin/bash
# Shell Script to replace string in a file
/bin/sed -i “s/Linux/Waseem/g” /PATH/TO/FILE/string.txt
exit 0;

Before:-
—————–
cat string.txt
Linux

Run the script:-
——————-
sh replacestring.sh

After:-
——————
cat string.txt
Waseem

Another way we can replace string by passing parameter,so that the script will be used dynamically.
cat replacestring.sh
#!/bin/bash
# Shell Script to replace string in a file
/bin/sed -i “s/$1/$2/g” /PATH/TO/FILE/string.txt
exit 0;

Run the script:-
—————–
sh replacestring.sh Linux Waseem

Shell Script : Basic mathematics

#!/bin/bash
# SCRIPT: calc.sh
# PURPOSE: Addition, Subtraction, Division and Multiplication of
# two numbers.
#
#####################################################################
# Variable Declaration #
#####################################################################

clear #Clears Screen

Bold=”\033[1m” #Storing escape sequences in a variable.
Normal=”\033[0m”

echo “$Bold Basic mathematics using bash script $Normal\n”

items=”1. ADDITTION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT”

choice=

#####################################################################
# Functions Starts from here #
#####################################################################
exit_function()
{
clear
exit
}

#Function enter is used to go back to menu and clears screen

enter()
{
unset num1 num2
ans=
echo “”
echo -e “Do you want to continue(y/n):\c”
stty -icanon min 0 time 0
while [ -z “$ans” ]
do
read ans
done

#The while loop ensures that so long as at least one character is

if [ “$ans” = “y” -o “$ans” = “Y” ]
then
stty sane # Restoring terminal settings
clear
else
stty sane
exit_function
fi
}

#####################################################################
# CALC Script MENU #
#####################################################################

while true
do
echo “$Bold \tWelcome to Waseem’s Tutorial $Normal\n”
echo “\t$items \n”
echo -n “Enter your choice : ”
read choice

case $choice in
1) clear
echo “Enter two numbers for Addition : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
echo “$num1 + $num2 = `expr $num1 + $num2`”
;;
2) clear
echo “Enter two numbers for Subtraction : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
echo “$num1 – $num2 = $((num1-num2))”
enter ;;
3) clear
echo “Enter two numbers for Multiplication : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
echo “$num1 * $num2 = `echo “$num1*$num2″|bc`”
enter ;;
4) clear
echo “Enter two numbers for Division : ”
echo -n “Number1: ”
read num1
echo -n “Number2: ”
read num2
let div=num1/num2
echo “$num1 / $num2 = $div”
enter ;;
5) exit_function ;;
*) echo “You entered wrong option, Please enter 1,2,3,4 or 5″
echo “Press enter to continue”
read
clear
esac

done

Output:-

Capture

How To Install MediaWiki on Ubuntu 12.04

About MediaWiki

MediaWiki is a free open source wiki program that allows users to create their own personal wiki sites. Originally built for Wikpedia, MediaWiki is written in PHP and uses a backend database, like MySQL, to store its data.

Setup


The steps in this tutorial require the user to have root privileges.  
Before working with MediaWiki, you need to have LAMP installed on your server. If you don’t have the Linux, Apache, MySQL, PHP stack on your server, you can find the tutorial for setting it up http://mohammedwaseem.in/install-lamp-ubuntu/  .

Once you have the user and required software on the virtual private server, you can start installing MediaWiki!

Install Media Wiki

Because the apt-get package of MediaWiki is very out of date, MediaWiki should be installed from source. We can start off by downloading the latest version:

curl -O http://releases.wikimedia.org/mediawiki//1.19/mediawiki-1.19.9.tar.gz
After downloading tar, untar it.
tar -xvzf mediawiki-1.19.9.tar.gz

Move the contents of the MediaWiki directory to a a more convenient location:
sudo mv mediawiki-1.19.9 /etc/mediawiki
Create a symbolic link between the MediaWiki directory and Apache’s document root:
sudo ln -s /etc/mediawiki/ /var/www
And finish up by restarting apache.
sudo service apache2 restart

Create a MySQL User and Database


Although this step is not required for a successful installation of MediaWiki, it is definitely a recommended measure for added security. 

During the installation process, you will be asked to supply either a database and associated user for a MediaWiki database or your MySQL root password. MediaWiki uses this information to create the required wiki database and tables.

Setting up a dedicated database can be done very quickly and removes the need for sharing the MySQL root information and the associated security risks. We can go ahead and set up the database now:

Log into MySQL on your server:

mysql -u root -p


Once you are logged in, create your MediaWiki database. The default name that MediaWiki has in their configuration form is my_wiki, but you can name it whatever you prefer:

create database my_wiki;


Additionally, you will need to provide a user for the new database with the permissions that MediaWiki requires (replace the username and password below with details of your choice):

grant index, create, select, insert, update, delete, alter, lock tables on my_wiki.* to 'wikiuser'@'localhost' identified by 'password';


Finish up by implementing the changes and quitting MySQL:

FLUSH PRIVILEGES;
exit;


The next part of the installation needs to be completed through the onscreen instructions located at [domain]/mediawiki/index.php

Set Up MediaWiki


Go through and fill out the Media Wiki Site configuration in the browser. 

When you reach the “MySQL settings” section of the setup page, leave the Database Host as localhost and then enter in the MySQL database name and username and password, if you configured them in the previous step.

If you did not set up a new user for MediaWiki, write in root for the username and the root password in the password field.

You can leave the “MySQL specific options” section as is.

Once you have finished walking through the the configuration, keep pressing continue until you reach the page that says, “Complete!” 

Complete!

The LocalSettings.php file should start downloading automatically. Make sure that you have downloaded the file before closing out of the page. LocalSettings.php is auto-generated—if you do not have the Local Settings file, you will have to generate it once again by going through all of the MediaWiki setup steps.

Once the file downloads, you should upload it to /etc/mediawiki, or whichever directory contains MediaWiki’s “index.php” file on your virtual server. 

You can copy the LocalSettings.php file from your computer to the server, with SCP (Secure Copy), an SSH-based way of transferring files:

cp /path/to/LocalSettings.php /etc/mediawiki


After the file is uploaded, you will be able to access your personal wiki at[domain]/mediawiki.