Saturday, February 5, 2022

Ansible - Run a task on multiple linux servers simultaneously

If you want to install a package or run a command on multiple servers, Ansible is a smart way to do the thing.

Suppose there are 100 Ubuntu servers and you want to perform a task on all the servers simultaneously, suppose you want to install java 11 on all the servers or run a shell script on all the servers, here is the way.

 After installing Ansible on your linux server, update inventory file of your linux server i.e. /etc/ansible/hosts 

Now add entries of your hundred servers i.e. IP, server login username and ssh key file path.

host1 ansible_ssh_host=23.56.xx.xx ansible_ssh_private_key_file=/path/of/ssh_key.pem ansible_ssh_user=ubuntu

host2 ansible_ssh_host=65.xx.xx.xx ansible_ssh_private_key_file=~/.ssh/ssh_key.pem ansible_ssh_user=ubuntu

host3 ansible_ssh_host=65.xx.xx.xx ansible_ssh_private_key_file=~/.ssh/ssh_key.pem ansible_ssh_user=ubuntu

 Suppose this is the shell script which should be run all 100 Ubuntu servers.

    #!/bin/bash

sudo apt-get update

sudo apt-get install -y openjdk-11-jdk

Now run the command

ansible all -i /etc/ansible/hosts -a "bash ~/Documents/script.sh"
It will install the java 11 on all 100 servers.

This time is surely less than the time taking log into the each server and run the command.

B) Ansible - Run a command on all servers

ansible all -i /etc/ansible/hosts -m command -a 'sudo apt-get update' 

Sunday, January 16, 2022

Ubuntu - Switch between multiple php versions on web and on command line

Multiple php versions are installed on ubuntu. You can find the config files of all the versions in /etc/php.

If you check the version on command line using

php --version

This version may be or may not be same as php version on browser (using apache2). You use phpinfo(); function to find the phpversion. You might have installed multiple php versions using apt-get install.

To shift different version on web using apache2

First disable the current enabled version using command

sudo a2dismod php7.0

then enable the other version which you want to use on web

sudo a2enmod php8.0

sudo service apache2 restart

To shift different version on terminal

sudo update-alternatives --set php /usr/bin/php8.0

Friday, December 10, 2021

Upload Files on Google Drive using CURL

Initiate an automated backup on Google Drive using shell script.

Steps :

a) Generate client id and secret key.

b) Add Google logged in user as a Test user in consent screen.

c) Enable Google drive API

d) Generate Authorization Code (One Time)

https://accounts.google.com/o/oauth2/auth?client_id=xxxxxxxx&redirect_uri=http://localhost&response_type=code&scope=https://www.googleapis.com/auth/drive&access_type=offline
e) Generate Refresh Token (One Time)
curl --request POST --data "code=xxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&redirect_uri=http://localhost&grant_type=authorization_code" https://oauth2.googleapis.com/token
f) Generate Access Token (Always)
curl --request POST --data "client_id=xxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx&refresh_token=xxxxxxxxxxxxx&grant_type=refresh_token" https://oauth2.googleapis.com/token
g) Upload File on Google Drive (Always)
curl -X POST -L \
    -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx" \
    -F "metadata={name : 'imagename.png'};type=application/json;charset=UTF-8" \
    -F "file=@imagename.png;type=image/png" \
   "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

Note : Refresh Tokens expire in 1 week if your app is not set as production. Change publishing status of your app from testing to production to use your refresh token always.

The Publishing Status option can be found in 'Oauth Consent Screen' which is under API & Services.

See complete instructions in this video.