Saturday, 5 September 2020

Terraform Basic Commands Guide

 

Terraform:

      Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

      Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.


Terraform Installation:

  curl -O  https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip

  sudo unzip terraform_0.13.2_linux_amd64.zip -d /usr/local/bin/

  rm terraform_0.13.2_linux_amd64.zip

You select the latest version of using above link terraform link, right now the latest version is 13.2

Version:

 $ terraform --version

       Terraform v0.13.2 

Terraform Prerequisites:

You need to set Cloud credentials depends on the cloud you are using.

export ALI_ACCESS_KEY_ID="ali_access_key"

$ export ALI_SECRET_ACCESS_KEY="ali_secret_key"

$ export ALI_DEFAULT_REGION="eu-west-1"

Terraform Commands:

After writing the terraform below is the first command to execute to initialize a working directory containing terraform configuration files. It is safe to run multiple times.

  $ terraform init

Plan:

 $ terraform plan -out plan.out

 or

$ terraform plan

It's an important feature of terraforming that allows  a user  to see  which actions Terraform will perform prior  to making any changes,

$ terraform apply plan.out

or

$ terraform apply

or

$ terraform apply  -out-approve

It will apply the changes to the instances.

Define variable while applying the changes

$ terraform apply  -out-approve -var tags-repository_url=${git_url}

Destroy:

$ terraform destroy 

Which will delete all the resources  that have been created

If I want to destroy one resource 

$ terraform destroy -target bucket_name

Validate:

  The validate command is used to validate/check the syntax of the terraform files.

$ terraform validate

Providers:

   The terraform providers command prints information  about the providers  used in the current configuration 

$ terraform providers

You and add plugins using terraform.tf file as shown below.

A provider configuration is created using a provider block: 

provider "alicloud"{

  version: "1.58.0"

}

In this example, I have updated the alicould provider version to 1.58.0.

Friday, 4 September 2020

Ansible Basic Commands Guide

 

Ansible:

 Is an open-source IT configuration Management and automation platform provided by Redhat.

  It uses no agents and no additional custom security infrastructure, so it's easy to deploy and most importantly  it uses a simple language (YAML, in the form of playbook)


Useful Commands

Install ansible

 Install the latest version ansible 

  •  pip install ansible 

Install a specific version of ansible

  • pip install ansible==2.7

Note: pip command depends on the python version which will vary if python3 is installed you can use pip3 install ansible 


Check the Ansible version:

  •  ansible --version


Testing connectivity of nodes:

  • ansible all -m ping
     To check uptime for one machine.  
  •    ansible all -i <IP address>, -m command -a "uptime"
  •    ansible all -a "uptime"
     To check the list of nodes from the inventory
  •   ansible all -i inventory -m command -a "uptime"
Dry run:
  • ansible <ipaddress> -m yum -a "name=vim" --check
Before making any changes you use the dry run to predict how the server would be affected by your command.
     

Connecting as different users:
 
  By default, Ansible connects the system using the current user using the corresponding ssh key pair.
  To connect with different users use  "-u " option to connect new users.

  • ansible  all -m ping -u ram
   You can run the playbook using the same method.

  • anisble-playbook newplay,yml -u ram -i inventory
Note: Make sure that user public key is added to .ssh/authorized_key for authentication.


You want to prompt a password for each activity, you can use the below command.

  •    ansible -i <ipaddress>,  -m command -a "uptime" --ask-pass
  •    ansible -i  <ipaddress>,  -m command -a "uptime" --ask-become-pass

The same applies to run the playbook as well.

  • ansible-playbook myplay.yml --ask-pass -i inventory
  • ansible-playbook myplay.yml --ask-become-pass -i inventory  (works as sudo)

Get information about Play:

  • ansible-playbook myplay.yml --list-tasks

This list all the tasks before making any changes or remote servers

Similarly, you can list all the hosts which gone effect with these changes without making any changes on the remote servers.

  • ansible-playbook myplay.yml --list-hosts

Yaml file Basic Syntax:

---
- hosts: apache/all  --> Checks inventory file tag matches installs the package."All" installs on all Server
   become: yes   ---> run play with sudo user
   vars:                ---> optional
       apache: 80
    tasks:                
    - name: Install apache web server   ---> Installs latest package of apache
       yum:
          name: apache
          state: latest
    
Inventory file Sample:

[apache]
10.0.0.5
10.0.0.6
[data]
10.0.0.4
10.0.0.3


This guide covers some of the most common Ansible commands. To get an overview of  all available  options you can use the help command:

  • ansible --help

If you want to get more insight into Ansible you can visit Ansible official website.

$ -bash: /dev/null permission denied


Unable to switch the user:  /dev/null permission denied


virtualmachine:~ # su - test
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied

Check the /dev permission and change the permission according to below...

virtualmachine:~ # ls -ld /dev
      drwxr-xr-x 18 root root 5620 Oct 15 07:31 /dev

If still issue persists then the problem would with /dev/null.

virtualmachine:~ # ls -l /dev/null
     crw-rw-rw- 1 root root 1, 3 Oct 15 07:28 /dev/null

Try to recreate the /dev/null file...

     rm /dev/null && mknod -m 0666 /dev/null c 1 3


The issue will be Solved...
My title



Unable to switch the user:  /dev/null permission denied


virtualmachine:~ # su - test
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied
     -bash: /dev/null: Permission denied

Check the /dev permission  and change the permission according to below...

virtualmachine:~ # ls -ld /dev
      drwxr-xr-x 18 root root 5620 Oct 15 07:31 /dev

If still issue persists then problem would with /dev/null.

virtualmachine:~ # ls -l /dev/null
     crw-rw-rw- 1 root root 1, 3 Oct 15 07:28 /dev/null

Try to recreate the /dev/null file..

     rm /dev/null && mknod -m 0666 /dev/null c 1 3


Issue will be Solved...



Unable to switch user permission denied

Unable to Switch User:

server1:~# su - test1
Too many logins for 'test1'.
Last login: Wed Jan 18 02:28:05 UTC 2017 on pts/49
su: cannot open session: Permission denied

The issue will be with setting limits for the user profile.

In file /etc/security/limits.conf the max logins field refer to what?

@users          -       maxlogins       6

Note: 6 logins at the same time. 
So you might have crossed the limit for a user session.

extend the limit or logout the user profile.

If you logged in as a test user and just wanted to exit a login shell type the following command 

:~# logout

or hit CTRL+D

else you don't know the session where you have logged in.

To see the list logged in user type who or w command.
:~# who
OR

:~# w

To logout a user called test, enter:

:~# pkill -KILL -u test1
OR

:~# Sudo pkill -KILL -u test1

Terraform Basic Commands Guide

  Terraform:       Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage ex...