Search This Blog

Saturday 27 February 2021

Ansible playbook for apache installation using yum module

Step 1) Create a playbook

 [osboxes@master ansible-playbooks]$ cat>apache-install.yml
---
- hosts: all
  become: true
  become_user: root
  tasks:
       - name: Install httpd package
         yum:
           name: httpd
           state: present

       - name: Create a index.html
         copy:
           content: "Test page"
           dest: /var/www/html/index.html

       - name: Start httpd service
         service:
           name: httpd
           state: started
           enabled: true

- name: Test the webserver URL
  hosts: all
  become: no
  tasks:
    - name: Connect to http://<hostname>
      uri:
        url: http://localhost
        return_content: yes
        status_code: 200


Step 2) Verify the syntax

[osboxes@master ansible-playbooks]$ ansible-playbook apache-install.yml -i inventory.txt --syntax-check

playbook: apache-install.yml

Step 3) Execute the playbook with --step option to run to verify on the remote host after each step. 

[osboxes@master ansible-playbooks]$ ansible-playbook apache-install.yml -i inventory.txt -K --step
BECOME password:

PLAY [Apahce webserver installation] ************************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]
Perform task: TASK: Install httpd package (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Install httpd package (N)o/(y)es/(c)ontinue: ********************************************************************************************

TASK [Install httpd package] ********************************************************************************************************************************
changed: [linuxhost]
Perform task: TASK: Create a index.html (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Create a index.html (N)o/(y)es/(c)ontinue: **********************************************************************************************

TASK [Create a index.html] **********************************************************************************************************************************
changed: [linuxhost]
Perform task: TASK: Start httpd service (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Start httpd service (N)o/(y)es/(c)ontinue: **********************************************************************************************

TASK [Start httpd service] **********************************************************************************************************************************
changed: [linuxhost]

PLAY [Test the webserver URL] *******************************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]
Perform task: TASK: Connect to http://<hostname> (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Connect to http://<hostname> (N)o/(y)es/(c)ontinue: *************************************************************************************

TASK [Connect to http://<hostname>] *************************************************************************************************************************
ok: [linuxhost]

PLAY RECAP **************************************************************************************************************************************************
linuxhost                  : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Ansible Playbook for Linux User Creation

Step 1) Create a simple playbook using user ansible module for user creation.

[osboxes@master ansible-playbooks]$ cat>linux-user.yml
---
- name: Create a Linux User
  hosts: all
  become: true
  become_user: root
  tasks:
      - name: User Account Creation
        user:
          name: pavan
          uid: 3405
          state: present

Step 2) Run the playbook using --syntax-check for syntax verification.

[osboxes@master ansible-playbooks]$ ansible-playbook linux-user.yml -i inventory.txt --syntax-check

playbook: linux-user.yml

Step 3) Run playbook using -C option for trial run. It will not perform actual task rather it will check if the task is going to be successful or failed. Here in the following example it says that task is going to be failed but it did not perform anything. Error is due to not providing -K option for asking password. 

[osboxes@master ansible-playbooks]$ ansible-playbook -C linux-user.yml -i inventory.txt

PLAY [Create a Linux User] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
fatal: [linuxhost]: FAILED! => {"msg": "Missing sudo password"}

PLAY RECAP **************************************************************************************************************************************************
linuxhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

I did the trail run again and it says task is going to be successful. 

[osboxes@master ansible-playbooks]$ ansible-playbook -C linux-user.yml -i inventory.txt -K
BECOME password:

PLAY [Create a Linux User] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]

TASK [User Account Creation] ********************************************************************************************************************************
changed: [linuxhost]

PLAY RECAP **************************************************************************************************************************************************
linuxhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Step 3) Now run the actual playbook task and verify if the user is created on remote host. 

[osboxes@master ansible-playbooks]$ ansible-playbook linux-user.yml -i inventory.txt -K
BECOME password:

PLAY [Create a Linux User] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]

TASK [User Account Creation] ********************************************************************************************************************************
changed: [linuxhost]

PLAY RECAP **************************************************************************************************************************************************
linuxhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Step 4) Verify if the linux user is created on remote host

[osboxes@linuxhost ~]$ id pavan
uid=3405(pavan) gid=1001(sftpusers) groups=1001(sftpusers)

Wednesday 24 February 2021

Ansible playbook for apache webserver installation and configuration from source

Step 1) Copy ssh keys to remote target server.

[osboxes@master ansible-projects]$ ssh-copy-id webserverhost02

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/osboxes/.ssh/id_rsa.pub"

The authenticity of host 'webserverhost02 (192.168.1.246)' can't be established.

ECDSA key fingerprint is SHA256:QYhfRimq4gvWwsjg+kul52yjv48WfbcRHaaKgSuUQeE.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

osboxes@webserverhost02's password:


Number of key(s) added: 1


Now try logging into the machine, with:   "ssh 'webserverhost02'"

and check to make sure that only the key(s) you wanted were added.

Step 2) Execute ansible ping module to check the remote server connectivity

[osboxes@master ansible-projects]$ ansible all -m ping -i webserver-inventory.txt

webserverhost02 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/libexec/platform-python"

    },

    "changed": false,

    "ping": "pong"

}

Step 2) Verify the syntax check before running actual playbook.

[osboxes@master ansible-projects]$ ansible-playbook install_configure_apache.yml -i webserver-inventory.txt --syntax-check

playbook: install_configure_apache.yml

Step 3) I have created a playbook for apache webserver installation and instance creation. You may download the source code from github repository.


[osboxes@master ansible-projects]$ cat install_configure_apache.yml
---
- hosts: all
  become: yes
  become_method: sudo

  roles:
    - role: "roles/apache24"


[osboxes@master ansible-projects]$ cat webserver-inventory.txt
webserverhost02




[osboxes@master roles]$ tree apache24/
apache24/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── boot.yml
│   ├── filesystem.yml
│   ├── install-dependencies.yml
│   ├── install.yml
│   ├── main.yml
│   └── validate.yml
├── templates
│   ├── apache.service.j2
│   └── httpd.conf.j2
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 15 files



Step 3) Run Ansible playbook for apache installation from source and instance creation. 


[osboxes@master ansible-projects]$ ansible-playbook install_configure_apache.yml -i webserver-inventory.txt

PLAY [all] **************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [webserverhost02]

TASK [roles/apache24 : check if the apache instance already present] ****************************************************************************************
ok: [webserverhost02]

TASK [roles/apache24 : fail] ********************************************************************************************************************************
skipping: [webserverhost02]

TASK [roles/apache24 : create a directory for apache binaries location] *************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : create a directory for apache instance location] *************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : create a directory for apache log location] ******************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Install all the packages] ************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Download apache binaries] ************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Download apr binaries] ***************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Download apr-util binaries] **********************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Install httpd package] ***************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Create a symlinks for apache products] ***********************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Install apr package] *****************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Copy apr package to srclib] **********************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Install apr-util package] ************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Copy apr-util package to srclib] *****************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Create a symlinks for python] ********************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Source code compilation] *************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Remove symlink] **********************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Configure Apache and change the desired installation location] ***********************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Execute the make command to prepare the files for the installation of Apache] ********************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Execute make install command to install apache] **************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : delete log directory] ****************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Create a log symlinks] ***************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : deploy httpd.conf.j2 to /home/osboxes/instances/apache24/poc-apache-instance/conf/] **************************************************
changed: [webserverhost02]

TASK [roles/apache24 : cleanup apache downloads] ************************************************************************************************************
changed: [webserverhost02] => (item=httpd-2.4.46.tar.gz)
changed: [webserverhost02] => (item=apr-1.7.0.tar.gz)
changed: [webserverhost02] => (item=apr-util-1.6.1.tar.gz)

TASK [roles/apache24 : Change the ownership for apache products directory] **********************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : Change the ownership for apache instances  directory] ********************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : deploy apache.service.j2 to /etc/systemd/system/apache.service] **********************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : load apache service] *****************************************************************************************************************
changed: [webserverhost02]

TASK [roles/apache24 : enable apache service] ***************************************************************************************************************
ok: [webserverhost02]

TASK [roles/apache24 : starting the apache instance] ********************************************************************************************************
changed: [webserverhost02]

PLAY RECAP **************************************************************************************************************************************************
webserverhost02            : ok=31   changed=28   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

[osboxes@master ansible-projects]$


Step 4) Verify apache instance status on remote host

[osboxes@webserverhost02 ~]$ ps -ef | grep httpd
osboxes    63796       1  0 08:37 ?        00:00:00 /home/osboxes/instances/apache24/poc-apache-instance/bin/httpd -k start
osboxes    63797   63796  0 08:37 ?        00:00:00 /home/osboxes/instances/apache24/poc-apache-instance/bin/httpd -k start
osboxes    63798   63796  0 08:37 ?        00:00:00 /home/osboxes/instances/apache24/poc-apache-instance/bin/httpd -k start
osboxes    63799   63796  0 08:37 ?        00:00:00 /home/osboxes/instances/apache24/poc-apache-instance/bin/httpd -k start

[osboxes@webserverhost02 poc-apache-instance]$ curl -k http://webserverhost02:10001
<html><body><h1>It works!</h1></body></html>
[osboxes@webserverhost02 poc-apache-instance]$


Atom IDE for developing Ansible playbooks

 Step 1) Download and Install Atom

https://atom.io/download/windows_x64

Step 2) Create a Project folder and Remote Sync to Linux machine where you have ansible-projects



Step 3) Enter remote machine details for connectivity




Step 4) Download or upload 





Step 5) Configurations will automatically be saved on remote machine when you save the file on local system.




Sunday 21 February 2021

Ansible playbook for tomcat installation and configuration

Step 1) I used ansible-galaxy to create a folder structure for tomcat playbook

[osboxes@master ansible-projects]$ ansible-galaxy init tomcat
- Role tomcat was created successfully

 [osboxes@master ansible-projects]$ tree tomcat/
tomcat/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files


Step 2) I have create playbook for tomcat installation and instance creation. You may download the source code from github repository.


[osboxes@master ansible-projects]$ cat install_configure_tomcat.yml
---
- hosts: all
  become: yes
  become_method: sudo

  roles:
    - role: "roles/tomcat90"


[osboxes@master ansible-projects]$ cat inventory.txt
tomcathost01



[osboxes@master ansible-projects]$ tree
.
├── install_configure_tomcat.yml
├── inventory.txt
└── roles
    └── tomcat90
        ├── defaults
        │   └── main.yml
        ├── files
        │   └── bash_profile
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   ├── boot.yml
        │   ├── filesystem.yml
        │   ├── install.yml
        │   ├── main.yml
        │   └── validate.yml
        ├── templates
        │   ├── setenv.sh.j2
        │   └── tomcat.service.j2
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

10 directories, 17 files

Step 3) Run Ansible playbook 

[osboxes@master ansible-projects]$ ansible-playbook install_configure_tomcat.yml -i inventory.txt

PLAY [all] **************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [tomcathost01]

TASK [roles/tomcat90 : check if the tomcat instance already present] ****************************************************************************************
ok: [tomcathost01]

TASK [roles/tomcat90 : fail] ********************************************************************************************************************************
skipping: [tomcathost01]

TASK [roles/tomcat90 : create a directory for tomcat jdk location] ******************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : create a directory for tomcat product location] **************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : create a directory for tomcat product location] **************************************************************************************
ok: [tomcathost01]

TASK [roles/tomcat90 : create a directory for tomcat instance location] *************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : create a directory for tomcat log location] ******************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Installing Java] *********************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Create a symlink for jdk] ************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Copy bash_profile to destination server for setting up JAVA_HOME] ********************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Source the .bash_profile] ************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Download tomcat binaries] ************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Install tomcat] **********************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Create a symlinks for tomcat products] ***********************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Create a new tomcat instance] ********************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : delete log directory] ****************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : Create a log symlinks] ***************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : deploy setenv.sh.j2 to /home/osboxes/instances/poc-tomcat-instance/bin] **************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : cleanup tomcat downloads] ************************************************************************************************************
ok: [tomcathost01]

TASK [roles/tomcat90 : deploy tomcat.service.j2 to /etc/systemd/system/tomcat.service] **********************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : load tomcat service] *****************************************************************************************************************
changed: [tomcathost01]

TASK [roles/tomcat90 : enable tomcat service] ***************************************************************************************************************
ok: [tomcathost01]

TASK [roles/tomcat90 : starting the tomcat instance] ********************************************************************************************************
changed: [tomcathost01]

PLAY RECAP **************************************************************************************************************************************************
tomcathost01               : ok=23   changed=18   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

Create a Linux user with expiration date

 Create a user named bandaru & Set expiry date to 2021-06-30

1) Create the user

[root@tomcathost01 ~]# useradd bandaru
[root@tomcathost01 ~]# passwd bandaru
Changing password for user bandaru.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

2) Check the expiry date for the user

[root@tomcathost01 ~]# chage -l bandaru
Last password change                                    : Feb 21, 2021
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

3) Set the expiration date

[root@tomcathost01 ~]# chage -E 2021-06-30 bandaru

4) Validate the expiration date

[root@tomcathost01 ~]# chage -l bandaru
Last password change                                    : Feb 21, 2021
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Jun 30, 2021
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

Saturday 20 February 2021

How to upload a file larger than 25mb into github

 
Step 1) Download and install git-lfs

https://github.com/git-lfs/git-lfs/releases/download/v2.13.2/git-lfs-windows-v2.13.2.exe

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/jdk (master)

$ ls -lart

total 140388

drwxr-xr-x 1 pavankumar bandaru 197121         0 Feb 20 16:08 ../

drwxr-xr-x 1 pavankumar bandaru 197121         0 Feb 20 16:09 .git/

drwxr-xr-x 1 pavankumar bandaru 197121         0 Feb 20 16:09 ./

-rw-r--r-- 1 pavankumar bandaru 197121 143722924 Feb 20 16:09 jdk-8u281-linux-x64.tar.gz

Step 2) Once downloaded and installed, set up Git LFS for your user account by running:

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/Downloads (main)

$ git lfs install

Updated git hooks.

Git LFS initialized.

Step 3) In each Git repository where you want to use Git LFS, select the file types you'd like Git LFS to manage (or directly edit your .gitattributes). You can configure additional file extensions at anytime.

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/jdk (master)

$ git lfs track "*.gz"

Tracking "*.gz"


Now make sure .gitattributes is tracked:

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/Downloads (main)

$ git add .gitattributes

Step 4) Just commit and push to GitHub as you normally would; for instance, if your current branch is named main:

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/Downloads (master)

$ git add jdk-8u281-linux-x64.tar.gz

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/Downloads (master)

$ git commit -m "jdk8"
[master (root-commit) fac4ee2] jdk8
 2 files changed, 4 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 jdk-8u281-linux-x64.tar.gz

$ git push origin master
Username for 'https://github.com': pavanbandaru
Password for 'https://pavanbandaru@github.com':
Uploading LFS objects: 100% (1/1), 144 MB | 0 B/s, done.
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 424 bytes | 424.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/pavanbandaru/tomcat/pull/new/master
remote:
To https://github.com/pavanbandaru/tomcat.git/
 * [new branch]      master -> master

pavankumar bandaru@DESKTOP-0V7CFI5 MINGW64 ~/jdk8 (master)



GIT and GITHUB






Linux System Admin


Basic Shell scripting for beginners

 What is a Shell ?

shell is a command line interpreter. It is an interface between user and the kernel. It takes commands from the user and executes them. 

There are different types of shells available in Linux. 

Shell

Developed By

Path

Default Prompt

Bourne (sh)

Stephen Bourne  

/bin/sh and sbin/sh

$

Korn (ksh)

David Korn

/bin/ksh

$

Cshell (csh)

Bill Joy

/bin/csh

%

Bash (sh/bsh)

Brain Fox

/bin/bash

$

Zsh (zsh)

Paul Falstad

/bin/zsh

$


Shell Variable types:

  1. Unix Defined or System Defined Variables
  2. User Defined Variables

1) Unix Defined Variables: These are stranded variable which are always accessible. The shell provide the values for these variables. These variables are usually used by the system we can change the variables as per over preference an customize the system environment.

Few system defined variables

[root@linuxhost ~]# set | grep ^HOME
HOME=/root
[root@linuxhost ~]# set | grep ^LOGNAME
LOGNAME=root
[root@linuxhost ~]# set | grep ^SHELL
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
[root@linuxhost ~]# set | grep ^MAIL
MAIL=/var/spool/mail/root
MAILCHECK=60

PS1=$primary shell prompt
Ps2= > -system prompt(default values)
SHELL=/bin/sh
TERM=vt100
TZ=ist-5:30
PATH:- Define the which shell  must search in order to command .
HOME:-Store the default working directory of the user.
LOGNAME:-Store the log name of the user.
MAIL:-Define the file where the mail of user stored.
IFS:-Define the internal felid separator which is space or tab or newline
SHELL:-Define the name of  the your  working shell.
TERM:-Define the name of the terminal which you are working.
TZ:-Define the name of the time zone in which you are working.

2) User Defined Variables: These are defined by the user and used most exclusively  in shell programming.

Rules for creating shell variables:

The first character should be alphabet or Underscore ( _ )
  • Eg:- -a= ,a=,b=,c=,d=
No commas or blanks are allowed
  • Eg:-a,b=10 is worng
Variable names should be case sensitive
  • Eg:-name=,Name=,nAme=
Variable names should not be a shell keyword.
  • Key words are words which meaning as already been explained to the shell.
  • Keywords are also called as reverse words
  • The list of key words are in bourn shell: echo for if untill read else case set wait fi esas unset eval while readonly break do shift exec continue done ulimit export exit umask return frap
echo statements: echo command is used to display the message on the screen and it is used to display the values stored in a shell variable.

Example:
[osboxes@linuxhost ~]$ echo "welcome"
welcome

Unix command should be in backquote/backtick ( ` ) in echo statement, otherwise it treats as a text

Examples:

[osboxes@linuxhost ~]$ echo "Today's date is : date"
Today's date is : date

[osboxes@linuxhost ~]$ echo "Today's date is : `date`"
Today's date is : Sun Feb 14 08:11:15 EST 2021

[osboxes@linuxhost ~]$ echo "Present working directory is : `pwd`"
Present working directory is : /home/osboxes

Shell variables/User defined Variables:

[osboxes@linuxhost ~]$ a=10
[osboxes@linuxhost ~]$ b=20
[osboxes@linuxhost ~]$ name="pavan"

[osboxes@linuxhost ~]$ echo $a
10
[osboxes@linuxhost ~]$ echo $b
20
[osboxes@linuxhost ~]$ echo $name
pavan

Null Variables: A variable which is defined but not assigned any value is known as a null variable.

[osboxes@linuxhost ~]$ d=""
[osboxes@linuxhost ~]$ echo $d

[osboxes@linuxhost ~]$ e=''

[osboxes@linuxhost ~]$ echo $e

Constant Variable: It is fixed value and doesn't change during the execution of the program. When the variable readonly the shell doesn't allow you to change their values.

[osboxes@linuxhost ~]$ b=14
[osboxes@linuxhost ~]$ readonly b
[osboxes@linuxhost ~]$ b=15
-bash: b: readonly variable

Unset: A variable and its value assigned to it are erased from shell memory.

[osboxes@linuxhost ~]$ c=30
[osboxes@linuxhost ~]$ echo $c
30
[osboxes@linuxhost ~]$ unset c
[osboxes@linuxhost ~]$ echo $c

[osboxes@linuxhost ~]$

Escape Sequences:

\n means newline
\r means return
\t means tab
\v means vertical tab
\b means backspace
\a means "alert" (beep or flash)
\0xx translates to the octal ASCII equivalent of 0xx

\”                 double quote                  
\’                 single quote
\\                 backslash     

Sample Program:
Write a program to display list of files and directories and present working directories and no of users logged into the system. You can execute the script in two ways (./sample.sh or sh sample.sh)

[osboxes@linuxhost ~]$ cat>sample.sh
#!/bin/bash
ls -l
pwd
who
[osboxes@linuxhost ~]$ chmod a+x sample.sh

[osboxes@linuxhost ~]$ ./sample.sh
total 4
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Desktop
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Documents
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Downloads
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Music
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Pictures
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Public
-rwxrwxr-x. 1 osboxes osboxes 26 Feb 14 08:33 sample.sh
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Templates
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Videos
/home/osboxes
osboxes  pts/0        2021-02-14 06:28 (192.168.1.5)

[osboxes@linuxhost ~]$ sh sample.sh
total 4
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Desktop
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Documents
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Downloads
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Music
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Pictures
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Public
-rwxrwxr-x. 1 osboxes osboxes 26 Feb 14 08:33 sample.sh
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Templates
drwxr-xr-x. 2 osboxes osboxes  6 Jul  4  2020 Videos
/home/osboxes
osboxes  pts/0        2021-02-14 06:28 (192.168.1.5)

Write a program to count no of users are currently logged into the system.

[osboxes@linuxhost ~]$ cat>numberofusers.sh
#!/bin/bash
echo "No of users logged into the system: `who |wc -l`"

[osboxes@linuxhost ~]$ sh numberofusers.sh
No of users logged into the system: 1

Write a program to accept the user input from the terminal. 
(Hint: read command reads value from the keyboard up to space or enter key)

[osboxes@linuxhost ~]$ cat>input.sh
#!/bin/bash
echo "Enter your name:"
read name
echo "Your Name is : $name"

[osboxes@linuxhost ~]$ sh input.sh
Enter your name:
pavan
Your Name is : pavan

Write a program to read two numbers and display.

[osboxes@linuxhost ~]$ cat>inputvalues.sh
#!/bin/bash
echo "Enter two values: a b"
read a b
echo "You have entered : $a $b "

[osboxes@linuxhost ~]$ sh inputvalues.sh
Enter two values: a b
2 3
You have entered : 2 3

Operators:

1) Arithmetic Operators
2) Relational Operators: 
    Numeric Comparison
    String Comparison
3) Logical Operators

1) Arithmetic Operators: Addition ( + ),  Subtraction ( - ), Multiplication ( * ), Division ( / ), Modulus Division ( % )
2) Relational Operators: -gt ( > ),  -ge ( >= ),  -le ( <= ),  -eq ( = ),  -ne ( != )
3) Logical Operators:
    And   -a
    Or     -o
    Nor   -!

Write a program to read two numbers and display sum diff product and division.
(Hint: expr is a command to evaluate arithmetic expressions. But expr is capable of carrying out only integer arithmetic.  Escape sequence ( "\"  is used for wild card characters  * , - , ? , [ , ] )

[osboxes@linuxhost ~]$ cat>operators.sh
#!/bin/bash
echo "Enter two values: a b "
read a b
c=`expr $a + $b`
echo " a and b addition is : $c "
c=`expr $a - $b`
echo " a and b subtraction is : $c "
c=`expr $a \* $b`
echo " a and b product is : $c "
c=`expr $a / $b`
echo " a and b division is : $c "

[osboxes@linuxhost ~]$ sh operators.sh
Enter two values: a b
4 2
 a and b addition is : 6
 a and b subtraction is : 2
 a and b product is : 8
 a and b division is : 2

If conditional statements:

if statement:

if condition
   then
   ---------
   ---------
   ---------
fi

if-else statement:

if condition
   then
   --------
   --------
   --------
else
   -------
   -------
   -------
fi

if-elif-else statement:

if condition
   then
   --------
   --------
   --------
elif condition
   then
   ----------
   ----------
   ----------
else
   ----------
   ----------
   ----------
fi


if 0  is true
if 1 is false

Write a program to change the directory.

[osboxes@linuxhost ~]$ cat >changedir.sh
#!/bin/bash
echo " Enter a directory name: "
read dir
if cd $dir
then
   echo " Change the directory to $dir "
   pwd
fi

[osboxes@linuxhost ~]$ sh changedir.sh
 Enter a directory name:
Desktop
 Change the directory to Desktop
/home/osboxes/Desktop

Write a program to copy a file

[osboxes@linuxhost ~]$ cat>filecopy.sh
#!/bin/bash
echo "Enter source and target: "
read source target
if cp $source $target
then
        echo " File copied from $source to $target "
else
        echo " File copy failed"
fi

[osboxes@linuxhost ~]$ sh filecopy.sh
Enter source and target:
sample.sh sample1.sh
 File copied from sample.sh to sample1.sh

Write a program to find greatest number of two numbers.

[osboxes@linuxhost ~]$ cat>gretestnumber.sh
#!/bin/bash
echo "Enter two numbers: "
read a b
if [ $a -gt $b ]
then
   echo " $a is the greatest number "
else
   echo " $b is the greatest number "
fi

[osboxes@linuxhost ~]$ sh gretestnumber.sh
Enter two numbers:
8 1
 8 is the greatest number

Write a program to check the given number is even or odd.

[osboxes@linuxhost ~]$ cat even-odd.sh
#!/bin/bash
echo "Enter a number: "
read num
if [ `expr $num % 2` -eq 0 ]
then
   echo "$num is even number"
else
   echo "$num is odd number"
fi

[osboxes@linuxhost ~]$ sh even-odd.sh
Enter a number:
8
8 is even number

test command:  the test command is used to perform checks and comparisons. 
Here's its syntax: test expression
test checks the file types and variables

if test condition
then
   commands
else
   commands
fi


Write a program to check how many users are working on the system.

[osboxes@linuxhost ~]$ cat noofusers.sh
#!/bin/bash
total=`who|wc -l`
if test $total -eq 1
then
        echo "you are the only user logged in"
else
        echo "total no of users logged in: $total"
fi

Write a program to check the given number is +ve or –ve?

[osboxes@linuxhost ~]$ sh noofusers.sh
you are the only user logged in

[osboxes@linuxhost ~]$ cat numbercheck.sh
#!/bin/bash
echo " enter a number "
read num
if test $num -gt 0
then
        echo " the number is +ve "
elif test $num -eq 0
then
        echo " the number is 0 "
else
        echo " the number is -ve "
fi

[osboxes@linuxhost ~]$ sh numbercheck.sh
 enter a number
-9
 the number is -ve
[osboxes@linuxhost ~]$








Ansible Certification Exam EX294

Study points for the exam

As an RHCE exam candidate, you should be able to handle all responsibilities expected of a Red Hat Certified System Administrator, including these tasks:

Be able to perform all tasks expected of a Red Hat Certified System Administrator

  • Understand and use essential tools
  • Operate running systems
  • Configure local storage
  • Create and configure file systems
  • Deploy, configure, and maintain systems
  • Manage users and groups
  • Manage security

Understand core components of Ansible

  • Inventories
  • Modules
  • Variables
  • Facts
  • Plays
  • Playbooks
  • Configuration files
  • Use provided documentation to look up specific information about Ansible modules and commands

Install and configure an Ansible control node

  • Install required packages
  • Create a static host inventory file
  • Create a configuration file
  • Create and use static inventories to define groups of hosts
  • Manage parallelism

Configure Ansible managed nodes

  • Create and distribute SSH keys to managed nodes
  • Configure privilege escalation on managed nodes
  • Validate a working configuration using ad hoc Ansible commands

Script administration tasks

  • Create simple shell scripts
  • Create simple shell scripts that run ad hoc Ansible commands

Create Ansible plays and playbooks

  • Know how to work with commonly used Ansible modules
  • Use variables to retrieve the results of running a command
  • Use conditionals to control play execution
  • Configure error handling
  • Create playbooks to configure systems to a specified state

Use Ansible modules for system administration tasks that work with:

  • Software packages and repositories
  • Services
  • Firewall rules
  • File systems
  • Storage devices
  • File content
  • Archiving
  • Scheduled tasks
  • Security
  • Users and groups

Work with roles

  • Create roles
  • Download roles from an Ansible Galaxy and use them

Use advanced Ansible features

  • Create and use templates to create customized configuration files
  • Use Ansible Vault in playbooks to protect sensitive data

Create and use templates to create customized configuration files Work with Ansible variables and facts Create and work with roles Download roles from an Ansible Galaxy and use them Manage parallelism Use Ansible Vault in playbooks to protect sensitive data Use provided documentation to look up specific information about Ansible modules and commands

As with all Red Hat performance-based exams, configurations must persist after reboot without intervention.

Ansible Roles

Roles


Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

Role directory structure

An Ansible role has a defined directory structure with seven main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use. For example:

# playbooks
site.yml
webservers.yml
fooservers.yml
roles/
    common/
        tasks/
        handlers/
        library/
        files/
        templates/
        vars/
        defaults/
        meta/
    webservers/
        tasks/
        defaults/
        meta/

By default Ansible will look in each directory within a role for a main.yml file for relevant content (also main.yaml and main):
  • tasks/main.yml - the main list of tasks that the role executes.
  • handlers/main.yml - handlers, which may be used within or outside this role.
  • library/my_module.py - modules, which may be used within this role (see Embedding modules and plugins in roles for more information).
  • defaults/main.yml - default variables for the role (see Using Variables for more information). These variables have the lowest priority of any variables available, and can be easily overridden by any other variable, including inventory variables.
  • vars/main.yml - other variables for the role (see Using Variables for more information).
  • files/main.yml - files that the role deploys.
  • templates/main.yml - templates that the role deploys.
  • meta/main.yml - metadata for the role, including role dependencies.

You can add other YAML files in some directories. For example, you can place platform-specific tasks in separate files and refer to them in the tasks/main.yml file:

# roles/example/tasks/main.yml
- name: Install the correct web server for RHEL
  import_tasks: redhat.yml
  when: ansible_facts['os_family']|lower == 'redhat'

- name: Install the correct web server for Debian
  import_tasks: debian.yml
  when: ansible_facts['os_family']|lower == 'debian'

# roles/example/tasks/redhat.yml
- name: Install web server
  ansible.builtin.yum:
    name: "httpd"
    state: present

# roles/example/tasks/debian.yml
- name: Install web server
  ansible.builtin.apt:
    name: "apache2"
    state: present

Roles may also include modules and other plugin types in a directory called library. For more information, please refer to Embedding modules and plugins in roles below.

Storing and finding roles


By default, Ansible looks for roles in two locations:

in a directory called roles/, relative to the playbook file

in /etc/ansible/roles

If you store your roles in a different location, set the roles_path configuration option so Ansible can find your roles. Checking shared roles into a single location makes them easier to use in multiple playbooks. See Configuring Ansible for details about managing settings in ansible.cfg.

Alternatively, you can call a role with a fully qualified path:

---
- hosts: webservers
  roles:
    - role: '/path/to/my/roles/common'

Using roles

You can use roles in three ways:
  • at the play level with the roles option: This is the classic way of using roles in a play.
  • at the tasks level with include_role: You can reuse roles dynamically anywhere in the tasks section of a play using include_role.
  • at the tasks level with import_role: You can reuse roles statically anywhere in the tasks section of a play using import_role.
Using roles at the play level

The classic (original) way to use roles is with the roles option for a given play:

---
- hosts: webservers
  roles:
    - common
    - webservers
When you use the roles option at the play level, for each role ‘x’:
  • If roles/x/tasks/main.yml exists, Ansible adds the tasks in that file to the play.
  • If roles/x/handlers/main.yml exists, Ansible adds the handlers in that file to the play.
  • If roles/x/vars/main.yml exists, Ansible adds the variables in that file to the play.
  • If roles/x/defaults/main.yml exists, Ansible adds the variables in that file to the play.
  • If roles/x/meta/main.yml exists, Ansible adds any role dependencies in that file to the list of roles.
  • Any copy, script, template or include tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely.

When you use the roles option at the play level, Ansible treats the roles as static imports and processes them during playbook parsing. Ansible executes your playbook in this order:

  • Any pre_tasks defined in the play.
  • Any handlers triggered by pre_tasks.
  • Each role listed in roles:, in the order listed. Any role dependencies defined in the role’s meta/main.yml run first, subject to tag filtering and conditionals.
  • Any tasks defined in the play.
  • Any handlers triggered by the roles or tasks.
  • Any post_tasks defined in the play.
  • Any handlers triggered by post_tasks.

Note

If using tags with tasks in a role, be sure to also tag your pre_tasks, post_tasks, and role dependencies and pass those along as well, especially if the pre/post tasks and role dependencies are used for monitoring outage window control or load balancing.

You can pass other keywords to the roles option:

---
- hosts: webservers
  roles:
    - common
    - role: foo_app_instance
      vars:
        dir: '/opt/a'
        app_port: 5000
      tags: typeA
    - role: foo_app_instance
      vars:
        dir: '/opt/b'
        app_port: 5001
      tags: typeB

When you add a tag to the role option, Ansible applies the tag to ALL tasks within the role.

When using vars: within the roles: section of a playbook, the variables are added to the play variables, making them available to all tasks within the play before and after the role. This behavior can be changed by DEFAULT_PRIVATE_ROLE_VARS.

Including roles: dynamic reuse

You can reuse roles dynamically anywhere in the tasks section of a play using include_role. While roles added in a roles section run before any other tasks in a playbook, included roles run in the order they are defined. If there are other tasks before an include_role task, the other tasks will run first.

To include a role:

---
- hosts: webservers
  tasks:
    - name: Print a message
      ansible.builtin.debug:
        msg: "this task runs before the example role"

    - name: Include the example role
      include_role:
        name: example

    - name: Print a message
      ansible.builtin.debug:
        msg: "this task runs after the example role"

You can pass other keywords, including variables and tags, when including roles:

---
- hosts: webservers
  tasks:
    - name: Include the foo_app_instance role
      include_role:
        name: foo_app_instance
      vars:
        dir: '/opt/a'
        app_port: 5000
      tags: typeA
  ...

When you add a tag to an include_role task, Ansible applies the tag only to the include itself. This means you can pass --tags to run only selected tasks from the role, if those tasks themselves have the same tag as the include statement. 

You can conditionally include a role:

---
- hosts: webservers
  tasks:
    - name: Include the some_role role
      include_role:
        name: some_role
      when: "ansible_facts['os_family'] == 'RedHat'"

Importing roles: static reuse

You can reuse roles statically anywhere in the tasks section of a play using import_role. The behavior is the same as using the roles keyword. For example:

---
- hosts: webservers
  tasks:
    - name: Print a message
      ansible.builtin.debug:
        msg: "before we run our role"

    - name: Import the example role
      import_role:
        name: example

    - name: Print a message
      ansible.builtin.debug:
        msg: "after we ran our role"

You can pass other keywords, including variables and tags, when importing roles:

---
- hosts: webservers
  tasks:
    - name: Import the foo_app_instance role
      import_role:
        name: foo_app_instance
      vars:
        dir: '/opt/a'
        app_port: 5000
  ...

When you add a tag to an import_role statement, Ansible applies the tag to all tasks within the role. See Tag inheritance: adding tags to multiple tasks for details.

Running a role multiple times in one playbook

Ansible only executes each role once, even if you define it multiple times, unless the parameters defined on the role are different for each definition. For example, Ansible only runs the role foo once in a play like this:

---
- hosts: webservers
  roles:
    - foo
    - bar
    - foo
You have two options to force Ansible to run a role more than once.

Passing different parameters

You can pass different parameters in each role definition as:

---
- hosts: webservers
  roles:
    - { role: foo, vars: { message: "first" } }
    - { role: foo, vars: { message: "second" } }
or

---
- hosts: webservers
  roles:
    - role: foo
      vars:
        message: "first"
    - role: foo
      vars:
        message: "second"

In this example, because each role definition has different parameters, Ansible runs foo twice.

Using allow_duplicates: true

Add allow_duplicates: true to the meta/main.yml file for the role:

# playbook.yml
---
- hosts: webservers
  roles:
    - foo
    - foo

# roles/foo/meta/main.yml
---
allow_duplicates: true
In this example, Ansible runs foo twice because we have explicitly enabled it to do so.

Using role dependencies

Role dependencies let you automatically pull in other roles when using a role. Ansible does not execute role dependencies when you include or import a role. You must use the roles keyword if you want Ansible to execute role dependencies.

Role dependencies are stored in the meta/main.yml file within the role directory. This file should contain a list of roles and parameters to insert before the specified role. For example:

# roles/myapp/meta/main.yml
---
dependencies:
  - role: common
    vars:
      some_parameter: 3
  - role: apache
    vars:
      apache_port: 80
  - role: postgres
    vars:
      dbname: blarg
      other_parameter: 12

Ansible always executes role dependencies before the role that includes them. Ansible executes recursive role dependencies as well. If one role depends on a second role, and the second role depends on a third role, Ansible executes the third role, then the second role, then the first role.

Running role dependencies multiple times in one playbook

Ansible treats duplicate role dependencies like duplicate roles listed under roles:: Ansible only executes role dependencies once, even if defined multiple times, unless the parameters, tags, or when clause defined on the role are different for each definition. If two roles in a playbook both list a third role as a dependency, Ansible only runs that role dependency once, unless you pass different parameters, tags, when clause, or use allow_duplicates: true in the dependent (third) role.

For example, a role named car depends on a role named wheel as follows:

---
dependencies:
  - role: wheel
    vars:
      n: 1
  - role: wheel
    vars:
      n: 2
  - role: wheel
    vars:
      n: 3
  - role: wheel
    vars:
      n: 4

And the wheel role depends on two roles: tire and brake. The meta/main.yml for wheel would then contain the following:

---
dependencies:
  - role: tire
  - role: brake
And the meta/main.yml for tire and brake would contain the following:

---
allow_duplicates: true
The resulting order of execution would be as follows:

tire(n=1)
brake(n=1)
wheel(n=1)
tire(n=2)
brake(n=2)
wheel(n=2)
...
car

To use allow_duplicates: true with role dependencies, you must specify it for the dependent role, not for the parent role. In the example above, allow_duplicates: true appears in the meta/main.yml of the tire and brake roles. The wheel role does not require allow_duplicates: true, because each instance defined by car uses different parameter values.

Note

See Using Variables for details on how Ansible chooses among variable values defined in different places (variable inheritance and scope).

Embedding modules and plugins in roles

If you write a custom module  or a plugin , you might wish to distribute it as part of a role. For example, if you write a module that helps configure your company’s internal software, and you want other people in your organization to use this module, but you do not want to tell everyone how to configure their Ansible library path, you can include the module in your internal_config role.

To add a module or a plugin to a role: Alongside the ‘tasks’ and ‘handlers’ structure of a role, add a directory named ‘library’ and then include the module directly inside the ‘library’ directory.

Assuming you had this:

roles/
    my_custom_modules/
        library/
            module1
            module2

The module will be usable in the role itself, as well as any roles that are called after this role, as follows:

---
- hosts: webservers
  roles:
    - my_custom_modules
    - some_other_role_using_my_custom_modules
    - yet_another_role_using_my_custom_modules

If necessary, you can also embed a module in a role to modify a module in Ansible’s core distribution. For example, you can use the development version of a particular module before it is released in production releases by copying the module and embedding the copy in a role. Use this approach with caution, as API signatures may change in core components, and this workaround is not guaranteed to work.

The same mechanism can be used to embed and distribute plugins in a role, using the same schema. For example, for a filter plugin:

roles/
    my_custom_filter/
        filter_plugins
            filter1
            filter2

These filters can then be used in a Jinja template in any role called after ‘my_custom_filter’.

Sharing roles: Ansible Galaxy

Ansible Galaxy is a free site for finding, downloading, rating, and reviewing all kinds of community-developed Ansible roles and can be a great way to get a jumpstart on your automation projects.

The client ansible-galaxy is included in Ansible. The Galaxy client allows you to download roles from Ansible Galaxy, and also provides an excellent default framework for creating your own roles.