Friday 29 April 2022

Install Docker Container using Ansible YAML File on Ubuntu 22.04


cat ansible_docker.yml
---
 - name: Setup Docker 
   hosts: localhost
   become: yes
   tasks:
     - name: Install docker dependencies
       include: docker_dependency.yml

     - name: update apt
       apt:
         update_cache: yes

     - name: Install docker
       apt:
         name: docker-ce
         state: latest
         update_cache: yes

     - name: Service status - docker
       service:
         name: docker
         state: started

     - name: docker ps -a
       shell: sudo docker ps -a
       register: docker_ps

     - debug:
         var: docker_ps.stdout_lines

     - name: Run Hello World
       shell: sudo docker run hello-world
       register: docker_run

     - debug:
         var: docker_run.stdout_lines

     - name: docker ps -a run
       shell: sudo docker ps -a
       register: docker_ps_run

     - debug:
         var: docker_ps_run.stdout_lines


cat docker_dependency.yml
---
- name: Ensure old versions of Docker are not installed.
  package:
    name:
      - docker
      - docker-engine
    state: absent

- name: Ensure dependencies are installed.
  apt:
    name:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
      - gnupg
      - lsb-release
    state: present

- name: Add Docker GPG apt Key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Add Docker Repository
  apt_repository:
    repo: deb https://download.docker.com/linux/ubuntu focal stable
    state: present

ansible-playbook ansible_docker.yml

Wednesday 27 April 2022

Install and Configure Wordpress LAMP on Ubuntu 22.04

 


git clone https://github.com/jpmolekunnel/Wordpress-LAMP-on-Ubuntu-22.04.git

vim vars/default.yml

sudo apt update

ansible-playbook playbook.yml


http://<IP/URL/localhost>/wp-admin/


tags

ansible-playbook playbook.yml --tags=wordpress -v

ansible-playbook playbook.yml --tags=system -v

ansible-playbook playbook.yml --tags=mysql -v

ansible-playbook playbook.yml --tags=mysql-root -v


Install Ansible on Ubuntu 22.04 and Configure in the user/current Directory


sudo apt install -f

sudo apt install software-properties-common

sudo apt-add-repository ppa:ansible/ansible

sudo apt update

sudo apt install ansible


ansible --version

ansible [core 2.12.4]

  config file = /etc/ansible/ansible.cfg


Configure ansible in the current directory /home/ansible

cat ansible.cfg

[defaults]

inventory - /home/ansible/inventory


cat inventory

controller


ansible --version

ansible [core 2.12.4]

  config file = /home/ansible/ansible.cfg


hostname -i

sudo hostnamectl set-hostname controller

exec bash

hostname


sudo echo "192.168.182.131 controller" | sudo tee -a /etc/hosts

192.168.182.131 controller

cat /etc/hosts


ansible all --list

  hosts (1):

    controller


#run "sudo apt install" if  Failed sudo: a password is required

#hosts: localhost, in playbook 

ansible-playbook install.yml


Reset Mysql root Password Ubuntu 22.04


systemctl status mysql.service

systemctl stop mysql.service

ps -eaf|grep mysql


If mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists

sudo mkdir -p /var/run/mysqld

sudo chown mysql:mysql /var/run/mysqld


sudo mysqld_safe --skip-grant-tables &

[1] 28088

 2022-05-09T18:18:53.504935Z mysqld_safe Logging to '/var/log/mysql/error.log'.

2022-05-09T18:18:53.533285Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root_password';

Query OK, 0 rows affected (0.02 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> exit

Bye


sudo systemctl restart mysql.service 


mysql --version

mysql  Ver 8.0.29-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))


mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.


Error

sudo mysqld_safe --skip-grant-tables &

[1] 110591

2022-04-28T03:23:04.569926Z mysqld_safe Logging to '/var/log/mysql/error.log'.

2022-04-28T03:23:04.583095Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.


[1]+  Exit 1                  sudo mysqld_safe --skip-grant-tables


Solved:

sudo mkdir -p /var/run/mysqld

sudo chown mysql:mysql /var/run/mysqld

sudo mysqld_safe --skip-grant-tables &

mysql -u root


Tuesday 26 April 2022

Git checkouts using ansible.builtin.git module: example


cat git.yml 

---

 - name: git module demo

   hosts: all

   become: yes

   tasks: 

     - name: ensure git pkg installed

       ansible.builtin.apt:

         name: git

         state: present

     - name: checkout git repo

       ansible.builtin.git:

         repo: https://<URL> 

         dest: /Path/to/Dir 


ansible-playbook git.yml


ansible.builtin.git module

Saturday 16 April 2022

How to List Packages Added/Installed in Ubuntu Debian-based System


dpkg -l

dpkg -l pattern

dpkg --list pattern

dpkg -l |awk '/^[hi]i/{print $2}'

dpkg -l | grep '^ii '


dpkg-query -l | less

dpkg-query -l | grep ansible


dpkg --get-selections

dpkg --get-selections | grep -v deinstall

dpkg --get-selections | grep ansible

dpkg --get-selections | grep -w "install" 

dpkg --get-selections | grep -w "install" | cut -f1 


apt list

apt list pattern

apt list -a sudo


apt list --installed

apt list --installed pattern

apt list --installed | grep ansible

apt list --installed | awk '{split($0, a, "/"); print a[1]}'


apt list --manual-installed=true

apt-mark showmanual

apt show '~i' -a


apt-cache pkgnames

apt-cache policy <package_name>

apt-cache policy ansible


zgrep " installed " /var/log/dpkg.log*

grep " install " /var/log/apt/history.log


cat /var/lib/apt/lists/ppa.launchpad.net_*_Packages | grep '^Package:'

awk '/^Package: / {print $2}' /var/lib/apt/lists/ppa.launchpad.net_*_Packages | sort -u


grep Package /var/lib/apt/lists/ppa.launchpad.net_*_Packages

grep -h -P -o "^Package: \K.*" /var/lib/apt/lists/ppa.launchpad.net_*_Packages | sort -u


grep ^Package /var/lib/apt/lists/ppa.launchpad.net_*_Packages | awk '{print $2}' | sort | uniq

grep ^Package /var/lib/apt/lists/ppa.launchpad.net_*_Packages | awk '{print $2}' | sort -u


grep ' installed ' /var/log/dpkg.log /var/log/dpkg.log.1 | awk '{print $5}' | sort -u


#Intentionally Installed

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null | egrep '^(Start-Date:|Commandline:)' | grep -v aptdaemon | egrep '^Commandline:'

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null | egrep '^(Start-Date:|Commandline:)' | grep -v aptdaemon | egrep -B1 '^Commandline:'

zgrep -hE '^(Start-Date:|Commandline:)' $(ls -tr /var/log/apt/history.log ) | egrep -v 'aptdaemon|upgrade' | egrep -B1 '^Commandline:'


#sudo apt install dctrl-tools

grep-dctrl -sPackage . /var/lib/apt/lists/ppa.launchpad.net_*_Packages

grep-status -FStatus -sPackage -n   "install ok installed"


#sudo apt install aptitude

aptitude search '~i!~M'

aptitude search -F "%p %V %v" "?narrow(?installed,?not(?archive(stable)))" | grep ansible


#To exclude just stable you need to anchor the regex pattern:

aptitude search -F "%p %V %v %t" '?any-version(?installed ?not(?archive("^stable$")))'


#To exclude multiple repositories:

aptitude search -F "%p %V %v %t" '?any-version(?installed ?not(?archive("^(xenial|xenial-updates)$")))'


#count

sudo dpkg-query -f '${binary:Package}\n' -W | wc -l


#ppa-purge

apt install ppa-purge

Then get ppa list by tab-completion

ppa-purge -o (hit Tab key twice)


How to List Added/All Repositories and PPA's in Ubuntu Debian-based System


apt policy

apt-cache policy


#saved repos

cat /etc/apt/sources.list.save


#added repos

ls /etc/apt/sources.list.d/

#example

cat /etc/apt/sources.list.d/ansible-ubuntu-ansible-focal.list

apt-cache policy | grep ansible

apt policy | grep ansible


#List all repos

sudo grep -rhE ^deb /etc/apt/sources.list* 

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*


grep -r --include '*.list' '^deb ' /etc/apt/sources.list*

grep -r --include '*.list' '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/


grep "^[^#]" /etc/apt/sources.list /etc/apt/sources.list.d/*


grep -r --include '*.list' '^deb ' /etc/apt/ | sed -re 's/^\/etc\/apt\/sources\.list((\.d\/)?|(:)?)//' -e 's/(.*\.list):/\[\1\] /' -e 's/deb http:\/\/ppa.launchpad.net\/(.*?)\/ubuntu .*/ppa:\1/'

grep -hE '^deb\s' /etc/apt/sources.list /etc/apt/sources.list.d/*.list | sed '/ppa/ s/deb //g' | sed -re 's#http://ppa\.launchpad\.net/([^/]+)/([^/]+)(.*?)$#ppa:\1/\2#g'


apt-cache policy | grep http | awk '{print $2" "$3}' | sort -u

sudo apt update > /dev/null 2>&1 && sudo apt-cache policy | grep http | awk '{print $2 $3}' | sort -u

find /etc/apt/sources.list* -type f -iname "*.list" -exec grep -viE '(^#|^$)' {} \; -print | column -tx

sed -r -e '/^deb /!d' -e 's/^([^#]*).*/\1/' -e 's/deb http:\/\/ppa.launchpad.net\/(.+)\/ubuntu .*/ppa:\1/' -e "s/.*/sudo add-apt-repository '&'/" /etc/apt/sources.list /etc/apt/sources.list.d/*


#Examine the origin tag (such as o=Debian) for each of your current repositories

apt-cache policy | sed -n 's/.*o=\([^,]\+\).*/\1/p' | uniq


Friday 8 April 2022

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock


ISSUE:-

ubuntu@ubuntu:~/docker-work$ sudo docker rm $(docker ps -qa)

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json?all=1": dial unix /var/run/docker.sock: connect: permission denied

"docker rm" requires at least 1 argument.

See 'docker rm --help'.

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers


Solution:-

sudo chmod 666 /var/run/docker.sock


How to Change Docker cgroup Driver to systemd


docker info | grep -i driver

 Storage Driver: overlay2

 Logging Driver: json-file

 Cgroup Driver: cgroupfs


vim /etc/docker/daemon.json

{

  "exec-opts": ["native.cgroupdriver=systemd"]

}


sudo systemctl restart docker


docker info | grep -i driver

Storage Driver: overlay2

Logging Driver: json-file

Cgroup Driver: systemd


sudo systemctl status docker


Verify that Docker Engine is installed correctly

sudo docker run hello-world


Wednesday 6 April 2022

SSH access to AWS EC2 Instance Node Using .pem key


Convert File_name.ppk file to File_name.pem

sudo apt-get install putty-tools

puttygen File_name.ppk -O private-openssh -o File_name.pem
chmod 400 File_name.pem

ssh -i
File_name.pem username@IP

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/maven.yml` without `workflow` scope) error: failed to push some refs to 'https://github.com/path/filename.git'

  

Issue

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/maven.yml` without `workflow` scope)

error: failed to push some refs to 'https://github.com/path/filename.git'


Solution

Login GitHub URL 

Go to Settings ➡ Developer settingsPersonal access tokens


✔workflow Update GitHub Action workflows


Docker Compose to set up and run a simple Django/PostgreSQL app


docker --version
Docker version 20.10.12, build e91ed57

Create a Dockerfile

sudo mkdir Docker-Django

cd Docker-Django

cat Dockerfile

# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

cat requirements.txt
Django>=3.0,<4.0
psycopg2>=2.8

cat docker-compose.yml
version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db 

sudo docker-compose run web django-admin startproject composeexample .


Django-admin created are owned by root. Change the ownership of the new files.

sudo chown -R ubuntu:ubuntu

ls -l


Connect the database

Edit the composeexample/settings.py file, Replace the DATABASES = ... with the following.

sudo vim composeexample/settings.py
# settings.py
   
import os
   
[...]
   
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': 'db',
        'PORT': 5432,
    }
}

sudo docker-compose up

sudo docker ps

http://localhost:8001/


Ref: https://docs.docker.com/samples/django/