Tikejhya: Ashish Nepal

Knowledgebase

Author: admin (Page 1 of 41)

sed replace epoc to timestamp

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

e.g files:
-rw-r--r-- 1 root root 500000684 Jul 22 08:21 my_log.log.01_1500707421_500000684

ls -lrt *.log* | sed 's/^/echo "/; s/\([0-9]\{10\}\)/`date -d @\1`/; s/$/"/;'

echo "-rw-r--r-- 1 root root 500000684 Jul 22 08:21 my_log.log.01_`date -d @1500707421`_500000684"

s/\([0-9]\{10\}\)/`date -d @\1`/

\1 brings your reference from LHS to RHS

in this case; you could run eval for instance ..

eval $(ls -lrt *.log* | sed 's/^/echo "/; s/\([0-9]\{10\}\)/`date -d @\1`/; s/$/"/;') | sed 's/echo/\n/g'

vi advance commands

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

d0 delete from the beginning till the cursor

>> Indent line by shiftwidth spaces
<< De-indent line by shiftwidth spaces
5>> Indent 5 lines
5== Re-indent 5 lines

>% Increase indent of a braced or bracketed block (place cursor on brace first)
=% Reindent a braced or bracketed block (cursor on brace)
<% Decrease indent of a braced or bracketed block (cursor on brace)
]p Paste text, aligning indentation with surroundings

=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block

>i{ Increase inner block indent
Decrease inner block indent

lvm howto’s

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

#How to create lvm

pvcreate /dev/xvdb
vgcreate vg_name /dev/xvdb
lvcreate -l 100%FREE -n lg vg
echo , | sfdisk /dev/mapper/vg_name-lg_name
mkfs.ext4 -F /dev/mapper/vg_name-lg_name

#How to extend existing lvm disk space.
Lets assume we have added new disk to EC2 instance

pvcreate /dev/xvdc

Add the physical volume to the volume group via ‘vgextend’.

vgextend vg_name /dev/xvdc
e.g: vgextend disk2 /dev/xvdc

Allocate the physical volume to a logical volume (extend the volume size by your new disk size).

$ lvextend -l +100%FREE /dev/mapper/disk2-esdata

Resize the file system on the logical volume so it uses the additional space.

$ resize2fs /dev/mapper/disk2-esdata

awk all but last

awk 'BEGIN{FS=OFS="_"}{NF--; print}')

grep split string and get last field

Get me last value from delimeter
Grek way

[root@ip-10-31-4-69 inventories]# echo "kube_node_1_111" | grep -o '[^_]*$'
111

OR

Awk way

[root@ip-10-31-4-69 inventories]# echo "kube_node_1123" | awk -F "_" '{print $NF}'
1123

jq cheat sheet

Jq basics

[root@ip-10-31-4-69 inventories]# ./hosts --list | jq '.tag_Role_kube_node'

[
 "kube_node_1"
 ]

Jq remove square brackets [] : use []
Output

 [root@ip-10-31-4-69 inventories]# ./hosts --list | jq '.tag_Role_kube_node[]'
 "kube_node_1"

Jq remove quote “” : use -r
Output

 [root@ip-10-31-4-69 inventories]# ./hosts --list | jq -r '.tag_Role_kube_node[]'
 kube_node_1

private docker registry

Private docker registry using letsencrypt and Authentication.

#Creating letsencrypt:

./letsencrypt-auto certonly -a manual --rsa-key-size 4096 -d www.tikejhya.com -d registry.tikejhya.com --debug

#Creating auth file

docker run --entrypoint htpasswd registry:2 -Bbn testuser testpassword > auth/htpasswd

 

#If required:

docker stop registry && docker rm -v registry

#Create registry
docker run -d -p 443:5000 --restart=always --name registry \
-v `pwd`/auth:/auth \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
-v `pwd`/certs:/certs \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/mydomain_public.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/mydomain_private.key \
registry:2

#Test login
docker login registry.tikejhya.com

#Pull some image
docker pull php:php-fpm
#Tag image into newly created registry

docker tag registry.tikejhya.com/my-php

#push image to repo

docker push registry.tikejhya.com/my-php

#Lets pull from remote server:

docker login registry.tikejhya.com
docker --config ~/.docker pull registry.tikejhya.com/php-fpm

docker pull registry.tikejhya.com/bmi-php

dynamic inventory ec2.py with multiple inventory

While running  multiple inventory with ec2.py; You will also need to copy the ec2.ini file to location where you have ec2.py.

My structure was with 4 different environment and i didnt want to copy ec2.py all over the inventory rather use 1 and create ec2.ini according to requirement per environment. (which may be what many would like to do).

root@tikejhya:/etc/ansible# tree
.
├── development
│ └── ec2.ini
├── ec2.py
├── ext.py
├── ext.pyc
├── prod
│ └── ec2.ini
├── roles
├── staging
│ └── ec2.ini
└── uat
├── base
└── ec2.ini

Since there was not really out of the box solution; i created ext.py below and import into ec2.py.

#ec2.py

135 from ansible.module_utils import ec2 as ec2_utils
++ 136 from ext import mapper
137
138 HAS_BOTO3 = False

And:

487 help='Use boto profile for connections to EC2')
++ 488 parser.add_argument('--my_env', action='store', dest='my_env',
help='Use env for veriable')
490 self.args = parser.parse_args()
491
++ 492 mapper(self.args.my_env)

#ext.py
#!/usr/bin/python
import os
import sys

def mapper(self_args_my_env):
os.environ[“EC2_INI_PATH”] = “/etc/ansible/” + self_args_my_env + “/ec2.ini”
os.environ[“AWS_PROFILE”] = “profile_” + self_args_my_env

This made me able to run ansible with ec2.py followed by env parameter and use relevent boto profile.

ec2.py –my_env uat –list

chef [rendering template]

filebeat:
prospectors:

<% @rolename.each do |role| %>
#Some prospector should be passed in here based on role
<%= render "filebeat-syslog.yaml.erb" -%>

<%= render "filebeat-#{role}.yaml.erb" -%>
<% end %>
registry_file: <%= @path_registry %>

output:

logstash:
hosts: [“<%= node.filebeats.logstashhost %>:<%= node.filebeats.logstashport %>“]
#tls:
#certificate_authorities: [“/etc/pki/tls/certs/beats.crt”]
#insecure: true

shipper:

logging:

files:
rotateeverybytes: 10485760 # = 10MB

#In above rendering, variables comes from recipe which is loaded attributes.

$ cat attributes/default.rb
default[‘filebeat’][‘apache_log_file’] = [‘/var/log/httpd/*error_log’, ‘/var/log/httpd/*access_log’]

$ cat recipes/config.rb
rolename = node.roles

template ‘/etc/filebeat/filebeat.yml’ do
source ‘filebeat-default.yaml.erb’
mode ‘0440’
owner ‘root’
group ‘root’
variables(
path_apache_log_file: node[‘filebeat’][‘apache_log_file’],
input_type: node[‘filebeat’][‘input_type’],
document_type: node[‘filebeat’][‘document_type’],
path_registry: node[‘filebeat’][‘registry’],
:rolename => rolename,

)
end

$ cat templates/default/filebeat-default.yaml.erb
filebeat:
prospectors:

<% @rolename.each do |role| %>
#Some prospector should be passed in here based on role
<%= render "filebeat-syslog.yaml.erb" -%>

<%= render "filebeat-#{role}.yaml.erb" -%>
<% end %>
registry_file: <%= @path_registry %>

output:

logstash:
hosts: [“<%= node.filebeats.logstashhost %>:<%= node.filebeats.logstashport %>“]
#tls:
#certificate_authorities: [“/etc/pki/tls/certs/beats.crt”]
#insecure: true

shipper:

logging:

files:
rotateeverybytes: 10485760 # = 10MB
[ashnep@mgmt1-prod1 filebeats]$ cat templates/default/filebeat-magento.yaml.erb
<% @path_apache_log_file.each do |j| %>

paths:
– <%= j %>
input_type: <%= @input_type %>
<% if j =~ /error_log/ %>
document_type: apache-error-log
<% else %>
document_type: apache-access-log
<% end %>
fields:
service:
zone: <%= @zone %>

<% end %>

chef [templates]

template “/data/project/config.inc” do
source ‘config.inc.erb’
variables(
smtp_host: node[‘smtp’][‘host’],
smtp_port: node[‘smtp’][‘port’],
suffix: suffix,
)
owner ‘apache’
group ‘apache’
mode ‘0744’
end

Page 1 of 41

Powered by WordPress & Theme by Anders Norén