Tuesday, September 21, 2021

Ansible Playbook to collect uptime of servers

Hi, 

This is to explain how we can write a small playbook to collect uptime of all servers in our environment. We can use our own server list or group name as referred in Ansible Inventory. Here I used "infra_redhat" which is already defined in Ansible Inventory.

We can also mention the mail Id to which we need to recieve uptime report. 

With this playbook, we are running in a jumphost or some admin server from where, all servers are connected. 

We are just collecting outout of "uptime" command and saving a small CSV file. 

- name: Checking the server  uptime
  hosts: infra_redhat
  gather_facts: no
  tasks:
  - name: Create the server uptime report
    copy:
      content: Server Name, Uptime
      dest: /var/tmp/serveruptime.csv
    delegate_to: localhost

  - name: getting the uptime of mysql
    shell: uptime
    register: output_uptime
    changed_when: "output_uptime.rc != 0"
  - name:
    set_fact:
      UPTIME: "{{ output_uptime.stdout | replace('\t','') }}"
  - name: display the message
    lineinfile:
      line: "{{ inventory_hostname }},{{ UPTIME }}"
      insertafter: EOF
      path: /var/tmp/serveruptime.csv
    delegate_to: localhost

- name: Mail uptime report
  hosts: localhost
  tasks:
  - name: server uptime mail
    mail:
      subject: Server uptime report
      body: Hi, Please find attached server uptime report
      to:
      - root@localhost
      attach:
      - /var/tmp/serveruptime.csv


 Once this is run, we get good output in CSV format like follows, 

 $ cat /var/tmp/serveruptime.csv 
Server Name, Uptime 
server1, 04:00:20 up 38 days, 13:12, 1 user, load average: 0.42, 1.38, 0.87 
server2, 04:00:18 up 38 days, 14:33, 1 user, load average: 0.00, 0.07, 0.13 
server3, 04:00:20 up 39 days, 13:14, 1 user, load average: 0.04, 0.74, 0.79


Wednesday, May 12, 2021

 How to copy block of lines to end of file through Ansible


Following playbook can be used to push block of lines in list.cfg(in this case) to nrpe.cfg  at End of file.

We can push to many number of servers from single ansible Control node.

---

- name: copy the repo

  hosts: localhost

  tasks:

    - blockinfile:

            block: "{{ lookup('file', 'list.cfg') }}"

            dest: /etc/nagios/nrpe.cfg

            backup: yes

      register: output


Monday, November 24, 2014

FTP Configuration in RHEL7

                In this scenario, local Repository has been built with Operating System Media so that packages get installed from media,

In RHEL, as same as in previous versions, ftp services served by the package/daemon called vsftpd i.e. Very Secure FTP Daemon,
[root@linux ~]#
[root@linux ~]#
[root@linux ~]# yum install vsftpd

Command to install VSFTPD package is as follows,

[root@linux ~]# yum install vsftpd
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
local                                                                                                                                 | 4.1 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-9.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================
 Package                             Arch                                Version                                    Repository                          Size
====================================================================
Installing:
 vsftpd                              x86_64                              3.0.2-9.el7                                local                              166 k

Transaction Summary
====================================================================
Install  1 Package

Total download size: 166 k
Installed size: 343 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : vsftpd-3.0.2-9.el7.x86_64                                                                                                                 1/1
  Verifying  : vsftpd-3.0.2-9.el7.x86_64                                                                                                                 1/1

Installed:
  vsftpd.x86_64 0:3.0.2-9.el7

Complete!
[root@linux ~]#

Once the installation of package completes, enable the vsftpd service at startup. This can be done using the following command,

[root@linux ~]# systemctl enable vsftpd.service
ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@linux ~]#

Then start the service using systemctl as below,

[root@linux ~]# systemctl start vsftpd.service
[root@linux ~]#

Now FTP Service is enabled and available for localhost. To make available through LAN or outside network, this has to be enabled through firewall,

To do the same, following is the command to allow ftp through firewall,

[root@linux ~]#
[root@linux ~]# firewall-cmd --permanent --add-service=ftp
Success

To save the rule, execute the following command,
[root@linux ~]# firewall-cmd --reload
success
[root@linux ~]#


Now we are ready with FTP Server configuration. From any other machine, just go to browser and type ftp://192.168.x.x/ and it will open list of files which are available in FTP default directory


Wednesday, November 19, 2014

Quiz3 - RHEL7 Introduction

RHEL7 - Introduction
1.       What is the default filesystem in RHEL7?
ext4
ext5
xfs
ext3

2.       Does RHEL7 support container?
Yes
     No

3.       RHEL7 is available only as 64 bit operating system.
True
             False

4.       Which is the new boot loader included in RHEL7?
LILO
Yaboot
Grub2
Grub3

5.       Which is the new system and service manager introduced in RHEL7 which is not on previous versions of RHEL?
init2d
systemd
sysd              
sysV

6.       What is the command line tool provided to allow users to interact with NetworkManager
nmi
nmcli
nmcmi
nmedit

7.       How to start the service in RHEL7?
systemctl start
chkconfig start
systemd start
initctl start

8.       Which is the new service unit introduced in RHEL7 for managing firewall?
iptables2
firewalld
firewall2
ip2tables

9.       Is NFSv2supported in RHEL7?
Yes
No

10.   What is the alternate command against "resize2fs" in RHEL7 with xfs filesystem?
xfsgrowfs
xfs_growfs
resizexfs
extendxfs


                

Quiz2 - Red Hat Linux


Red Hat Linux - 2
1.       What is the command to install rpm package in Linux?
rpm -I packagename
rpm -i packagename
rpm -a packagename
rpmpkg -i packagename
2.       What is the command to check the detailed information about installed package?
rpm -qi packagename
rpm -qI packagename
rpm -l packagename
rpm -q packagename
3.       What is the command to check list of files installed by any package?
rpm -ql packagename
rpm -h packagename
rpm -i packagename
rpm -D packagename
4.       Create new logical volume with 25 extends with the name "database" in volume group vg1?
lvcreate -l 25 -n /dev/vg1/database
lvcreate -e 25 /dev/vg1/database
lvcreate -L 25 database
lvcreate -E 25 /dev/database
5.       what is the command to delete all the rules of iptables immediately?
iptables -D
iptables -F
iptables -R
iptables -d

6.       What is the command to check whether package is installed?
rpm -q packagename
rpm -ll packagename
rpm -l packagename
rpm -Q packagename
7.       Expand NTP
Network Time port
Network Time protocol
Network Transfer protocol
Network transfer port
8.       Which is the default MTA used in RHEL 6?
sendmail
postfix
qmail
rainmail
9.       which is the default file system in RHEL 6?
ext4
ext5
ext3
xfs
10.    Create a user raja with id 1456 and he should not have interactive login shell
useradd -x nologin raja –U 1456
useradd -S /bin/nologin –u 1456raja
useradd -s /sbin/nologin –u 1456 raja
useradd -s login disable –u 1456 raja

Quiz1 - Red Hat Linux

Red Hat Linux

1. Communication through telnet is in encrypted form. True or False?

a. True
b. False


2. in which file, default runlevel is set?

a. /etc/init.conf
b. /etc/rc.sysinit
c. /etc/boot.conf
d. /etc/inittab

3. Which is the option used along with ls to see hidden files in a directory?

a. -a
b. -h
c. -s
d. -A

4. Which is the default configuration file for FTP Daemon/Service?

a. /etc/ftp.cfg
b. /etc/vsftpd/vsftpd.conf
c. /etc/sftpd/sftpd.conf
d. /etc/ftp.conf

5. Which file should be edited to set hostname?

a. /etc/hosts
b. /etc/sysconfig/network
c. /etc/nss.conf
d. /etc/hosts.allow

6. What is the command to delete all the iptable rules?

a. iptables -D
b. iptables -F
c. iptables -d
d. iptables delete all

7. How to get list of files installed by a package? (Package already installed in system)

a. rpm -qf package name
b. rpm -ql package name
c. rpm -qv package name
d. rpm -qd package name

8. Which is the following are correct with respect to service and port?

a. telnet-23, ssh-22, smtp-25
b. telnet-22, ssh-25, smtp-110
c. telnet-22, ssh-25, pop=110

9. Is it possible to configure password less login between two servers through ssh session?

a. Yes.
b. No

10. What is the command to see IP configuration in a system?

a. ifconf
b. ifconfig
c. ipconfig

d. ifnet

Thursday, August 14, 2014

Recovering Ubuntu after loading windows in dual booting environment

How to recover Ubuntu 10 after installing windows in dual booting environment?

How to recover Ubuntu 10 after installing windows in dual booting environment?

Setup:
Machine with 2 disks and Ubnutu 10 and Windows xp installed in second disk /dev/sdb. Due to some problem in Windows, reinstalled the windows XP. After re-installation, ubuntu grub is over writen by windows and only windows is booting.
Procedure to recover:
1. Boot through Ubnutu Live CD
2. Type $ sudo fdisk -l to confirm the disk availbility.
3. Here Ubuntu is installed in /dev/sdb7 which is / partition and /dev/sdb8 is swap and /dev/sdb1 is windows partition.
4. For modifying Grub, root partition to be mounted first.
$ sudo mount /dev/sdb7 /mnt
5. For installing grub on /dev/sdb , give following command,
$ sudo grub-install --root-directory=/mnt /dev/sdb
6. Rebooted the machine. Now grub options coming for Ubnutu. But If we select windows, it gives "Insert system disk" error and was not booting.
7. Now booted in Ubnutu OS and given following command for updating grub.
$ sudo update-grub2
8. Restared the desktop now and verified both Ubnutu and windows XP booting.
9. Problem solved.

Ansible Playbook to collect uptime of servers

Hi,  This is to explain how we can write a small playbook to collect uptime of all servers in our environment. We can use our own server l...