Using ec2_facts to gather and print ansible ec2 variables

some people may asked, how can i use my ansible to print our my ec2_facts, this is the simple way to do that :

1. Login to your ec2 instance, create your playbook

#mkdir provisioning
#vim provisioning/ec2-gather.yml
- name: Initialize EC2 Instance For '{{ name_web_host }}'
hosts: localhost
connection: local
gather_facts: True
tasks:
- name: Get instance ec2 facts
action: ec2_facts
register: ec2_facts
- debug: var=ec2_facts

2. Let’s put your ec2.py in same directory also ec2.private-ip.ini

#cp ec2.py ec2.private-ip.ini ~/provisioning/

3. Let’s test add some information for it

#export ANSIBLE_HOST_KEY_CHECKING=False
#export EC2_INI_PATH=./ec2.private-ip.ini
#export AWS_ACCESS_KEY={your aws access key}
#export AWS_SECRET_KEY={your aws secret key}

#~/provisioning/ec2.py –refresh-cache

#ansible-playbook -i local, provisioning/ec2-gather.yml –extra-vars=”aws_access_key=$AWS_ACCESS_KEY aws_secret_key=$AWS_SECRET_KEY” -vvvv

your output will be something like this

 

TASK: [debug var=ec2_facts] ***************************************************
ok: [localhost] => {
“ec2_facts”: {
“ansible_facts”: {
“ansible_ec2_ami_id”: “ami-xxxxx”,
“ansible_ec2_ami_launch_index”: “0”,
“ansible_ec2_ami_manifest_path”: “(unknown)”,
“ansible_ec2_block_device_mapping_ami”: “/dev/sda1”,
“ansible_ec2_block_device_mapping_ephemeral0”: “sdb”,
“ansible_ec2_block_device_mapping_ephemeral1”: “sdc”,
“ansible_ec2_block_device_mapping_root”: “/dev/sda1”,
“ansible_ec2_hostname”: “ip-10-xx-xx-xx.ap-southeast-1.compute.internal”,
“ansible_ec2_instance_action”: “none”,
“ansible_ec2_instance_id”: “i-xxabxx”,
“ansible_ec2_instance_type”: “t2.small”,
“ansible_ec2_local_hostname”: “ip-10-xx-xx-xx.ap-southeast-1.compute.internal”,
“ansible_ec2_local_ipv4”: “10.x.xx.xxxx”,
“ansible_ec2_mac”: “02:17:41:4d:4c:91”,
“ansible_ec2_metrics_vhostmd”: ““,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_device_number”: “0”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_interface_id”: “eni-8xx0dxxx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_ipv4_associations_54.xx.xx.xxx”: “10.xx.xx.xxxx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_local_hostname”: “ip-10-0-xx-x.ap-southeast-1.compute.internal”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_local_ipv4s”: “10.xxx.xx.x”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_mac”: “02:17:41:4d:4c:91”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_owner_id”: “x38xxxx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_public_hostname”: “ec2-54-xx-xxx-xxxx.ap-southeast-1.compute.amazonaws.com”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_public_ipv4s”: “54.xx.xxx.xxxx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_security_group_ids”: “sg-xbxxx3xx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_security_groups”: “bastixx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_subnet_id”: “subnet-xdxxx2xx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_subnet_ipv4_cidr_block”: “10.0.0.0/18”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_vpc_id”: “vpc-x0xxx8xx”,
“ansible_ec2_network_interfaces_macs_02_17_41_4d_4c_91_vpc_ipv4_cidr_block”: “10.0.0.0/16”,
“ansible_ec2_placement_availability_zone”: “ap-southeast-1a”,
“ansible_ec2_placement_region”: “ap-southeast-1”,
“ansible_ec2_profile”: “default-hvm”,
“ansible_ec2_public_hostname”: “ec2-54-xx-xxx-x.ap-southeast-1.compute.amazonaws.com”,
“ansible_ec2_public_ipv4”: “54.x.x.x”,
“ansible_ec2_public_key”: “ssh-rsa AAAxxzaC1yc2EAAAADxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrn3IGNMePcms277wgx9X+YI/rVYCxxxxxxxxxMgoTJ/xbdVsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/h google-staging\n”,
“ansible_ec2_reservation_id”: “r-dXXX7XXX”,
“ansible_ec2_security_groups”: “bastixx”,
“ansible_ec2_services_domain”: “amazonaws.com”,
“ansible_ec2_user_data”: null
},
“changed”: false,
“invocation”: {
“module_args”: “”,
“module_name”: “ec2_facts”
}
}
}

PLAY RECAP ********************************************************************
Get instance ec2 facts ————————————————– 0.13s
debug var=ec2_facts —————————————————– 0.01s
Total Timing :0.13s
localhost : ok=3 changed=0 unreachable=0 failed=0

 

feel free to get it from my github

https://github.com/addhewarman/myansible.git

7 plugins untuk editor brackets

Bagi mereka yang belum pernah tahu atau pakai brackets, brackets adalah opensource editor yang dibuat oleh adobe. mari kita mulai review sederhana untuk 7 plugins brackets kita. catatan sampingan buat anda brackets is not new, di github ada 23970 star dengan lebih dari 5025 fork

Github : https://github.com/adobe/brackets
Slack: Brackets on Slack (You can join by requesting an invite)
Developers mailing list: http://groups.google.com/group/brackets-dev
Twitter: @brackets
Blog: http://blog.brackets.io/
IRC: #brackets on freenode

1. Brackets Git

plugin brackets editor yang satu ini akan menghubungkan anda dengan git, tentunya anda harus install git di computer anda. create branch, commit dan jangan lupa settings remote anda. bisa github, bitbucket atau apapun.

2. Code folding

Plugin ini sangat berguna ketika anda ingin melipat code anda dalam kurung kurawal atau berdasarkan tag html tertentu misalnya div, bila code anda terlalu panjang anda dapat melipatnya untuk melihatnya lebih singkat.

3. Beautify

secara umum ctrl+shift+l shortcut untuk beautify, apa yang beautify lakukan untuk anda ? dia akan merapikan tab dan spasi yang telah anda buat di code anda. Merapikan tab dan spasi pada code anda yang berantakan secara otomatis.

4. Emmet

https://github.com/emmetio/brackets-emmet adalah plugin untuk HTML dan CSS dimana emmet akan membuat waktu coding anda singkat karena dengan perintah singkatnya dia akan membuatkan tag dan css yang berulang menjadi lebih singkat. kindly goto youtube https://www.youtube.com/watch?v=NhVdn7_ub5k

5. Documents Toolbar

Document toolbar seperti sublime meletakkan file yang sedang anda edit dalam urutan tab

6. HTMLHint

ini adalah plugin linter untuk HTML, ketika menuliskan class pada html dan class itu belum ada, maka editor brackets akan memberikan kamu kesempatan untuk membuat class baru itu dan kamu secara otomatis akan dibawa ke class yang baru atau ke class yang sudah ada. langsung di edit inline di tempat tidak perlu buka file yang berbeda. sangat membantu mencegah lupa akan class css sewaktu menulis html.

7. Minimap

ini dia minimap code, ketika code anda sangat panjang dan rumit anda ingin tahu anda kini ada di area mana pada coding anda. minimap bisa membantu anda memberikan informasi anda ada di tengah-tengah code atau di bagian bawah code saat ini.

My Custom Git Cheet Sheet Command

git identity

- git config --global user.name "Addhe Warman"
- git config --global user.email "addhe.warman@gmail.com"

git editor

- git config --global core.editor vim

git diff tool

- git config --global merge.tool vimdiff

git check settings

- git config --list

git help

- git help
- man git

git initial a repository in existing directory

$ git init
$ git add sample.txt
$ git add README
$ git commit -m 'first initial project versioning'

git cloning existing repository

$ git clone https://github.com/addhewarman/docker-locust


$ git clone git@github.com:addhewarman/docker-locust.git

git check status of your file changes

$ git status

git tracking new files

$ git add YOUR_FILE


$ git add YOUR_FILE_1 YOUR_FILE_2

git how you commit your changes


$ git status


$ git diff


$ git add YOUR_MODIF_FILE


$ git commit -m 'i have updated my file'


$ git log


$ git show SHA_NUM

git revert your changes

$ git checkout YOUR_MODIF_FILE

git revert one commit

$ git reset HEAD~1

or

$ git revert HEAD

git ignore file

$ vim .gitignore

git check your ignore file


$ cat .gitignore
*.log
*.[abc]
*.~
*.a
doc/*.txt
build/rake/*
composer/dump/*

git compare your changes


$ git diff


$ git diff --cached

git compare staged changes to your last commit

$ git diff --staged

git commit your changes

$ git commit -m '
i made this cheatsheet as simple and posible
as clear as clean water and 5 years old kid can
learn and read it
'


$ git commit -a -m '
add -a will add everything without have to add
or using git add one by one
'

git remove file that you already add

$ git rm YOUR_FILE


$ git rm *~


$ git rm log/*.log

git moving files

$ git mv file_from file_to

git show commit history

$ git log


$ git log -p -2

git series of summarizing option

$ git log --stat

git log output to formats other than default (one liner)

$ git log --pretty=oneline

git log output to your own format

$ git log --pretty=format:"%h - %an, %ar: %s"

git limit log output

$ git log --since=12.weeks

git log commit by awan in december 2014 to january 2015

$ git --pretty="%h:%s" --author=awan --since="2014-12-01" --before="2015-01-28"

git log pretty format with ASCII graph visual of your branch and merge

$ git log --pretty-format:"%h %s" --graph

git commit forget one file to commit

$ git commit -m 'i commit this and i forget one file'
$ git add file_kelupaan
$ git commit --amend

git command to revert one file from list of your git add

case : dayummm i git add everything, i should not add them all this cubbysmart.txt should not in the list


awan@google:~/django# git add .
awan@google:~/django# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# new file: awan.txt
# new file: cubbysmart.txt
# new file: encrypt.py
# new file: index.py
# new file: master_code.py
#
awan@google:~/django# git reset HEAD cubbysmart.txt
awan@google:~/django# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# new file: awan.txt
# new file: encrypt.py
# new file: index.py
# new file: master_code.py
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# cubbysmart.txt
awan@google:~/django#

git unmodifying a modified file

$ git checkout your_modified_file.py

git showing your remotes

$ git remote


$ git remote -v

git add remote repository

$ git remote add awan_cloud git://github.com/awan/cloudofme.git


awan@google:~/django# git remote
origin
awan@google:~/django# git remote -v
origin ssh://534dda7f5004461b4d0000f8@django-addhewarman.rhcloud.com/~/git/django.git/ (fetch)
origin ssh://534dda7f5004461b4d0000f8@django-addhewarman.rhcloud.com/~/git/django.git/ (push)
awan@google:~/django# git remote add awan_cloud git://github.com/awan/cloudofme.git
awan@google:~/django# git remote -v
awan_cloud git://github.com/awan/cloudofme.git (fetch)
awan_cloud git://github.com/awan/cloudofme.git (push)
origin ssh://534dda7f5004461b4d0000f8@django-addhewarman.rhcloud.com/~/git/django.git/ (fetch)
origin ssh://534dda7f5004461b4d0000f8@django-addhewarman.rhcloud.com/~/git/django.git/ (push)

git get data from remote projects

$ git fetch your_remote


$ git fetch awan_cloud

git push to your remote

$ git push origin remote


$ git push origin awan_cloud

git inspecting a remote

$ git remote show origin


$ git remote show awan_cloud

git remove and rename remotes

$ git remote rename awan_cloud cloud_aws


$ git remote


$ git remote rm cloud_aws

git tag your revision

$ git tag


$ git tag -a your_tag_name -m 'my first tag' your_sha_revision_id


awan@google:~/django# git tag -a my_tag -m 'tset tag' 22d9cd99b2184967033fd14b1be8cd5c88a1d037
awan@google:~/django# git tag -l
my_tag
awan@google:~/django# git show my_tag
tag my_tag
Tagger: Awan
Date: Sun Aug 30 23:09:26 2015 +0700

tset tag

commit 22d9cd99b2184967033fd14b1be8cd5c88a1d037
Author: Awan
Date: Wed Apr 16 08:17:40 2014 +0700

update code

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..70166b0
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+#addhe warman django project
awan@google:~/django#


git sharing tag

$ git push origin my_tag

git alias

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.unstage 'reset HEAD --'
$ git config --global alias.last 'log -1 HEAD'

git check your current branch

$ git branch

git create a branch

$ git checkout -b MY_NEW_BRANCH

git push changes to branch

$ git push origin MY_NEW_BRANCH

git pull from branch

$ git pull origin MY_NEW_BRANCH

git change back to master branch

$ git checkout master

git merge branch to master

$ git checkout master
$ git merge MY_NEW_BRANCH

—————— continue to part 2

Let’s Encrypt SSL Gratis Untuk Semua Orang

ISRG (Internet Security Research Group) sebuah perusahaan di San Francisco, California meluncurkan Let’s Encrypt pada tanggal 27 July 2015. Let’s Encrypt adalah tempat dimana kamu bisa mendapatkan sertifikast ssl gratis untuk website kamu. Ada 2 alasan kenapa https diperlukan, pertama adalah Enkripsi, dan yang kedua adalah Identification. Kapan anda membutuhkannya ? ketika anda mengirimkan data sensitif ke server seperti login, informasi account bank, informasi kartu kredit.

Let’s Encrypt has reached a point where we’re ready to announce our launch schedule.

  • First certificate: Week of July 27, 2015
  • General availability: Week of September 14, 2015

Untuk mendapatkan sertifikasi ssl gratis ini anda hanya butuh menginstall aplikasi let’s encrypt di server anda lalu aplikasi ini akan mengenerate sertifikasi yang dibutuhkan lalu menguploadnya ke server Let’s Encrypt, anda akan mendapatkan sertifikasi authority dan tinggal di gunakan di website anda.

Untuk anda yang sudah memiliki / mengenerate sertifikat sendiri anda dapat menggunakannya dan menguploadnya ke server let’s encypt untuk mendapatkan certificate authority gratis

Skipfish scanner step by step

Skipfish adalah tool dibuat oleh karyawan yang kini bekerjaa di google, skipfish pada dasarnya akan scan webapplication dan hasilnya akan di pass ke mesin deteksi untuk di analisa.

Untuk menjalankan skipfish, anda butuh 2 hal yang penting.
– Direktori
– List kata / Kamus kata

1. Ciptakan folder untuk skipfish
mkdir skipfish-outlog

2. List kata ambil contoh kata bisa dari
cp /usr/share/skipfish/dictionaries/medium.wl .

3. Pastikan folder yang kamu buat bisa di tulis, eksekusi perintah ini untuk check
chmod 755 medium.wl

4. Jalankan skipfish
skipfish -S medium -o skipfish-outlog/

skipfish -S medium.wl -o skipfish-out/ http://detik.com

python telnet / monitoring redis or beanstalk

i wrote this python to monitor my redis or beanstalk process, cannot give you full of my scripts this is only basic scripts. in case me in the future forget how to write and send command to server via telnet and failed because of hung in the middle of process (got into limbo) this will get the value and send that value over to monitoring server thru REST API or CLI

Monitoring current connected client in redis

#!/usr/bin/env python
# Very simple python monitoring redis connected clients.

import sys
import telnetlib
import time

def run_telnet_session():
 #host = raw_input("Enter remote hostname e.g. localhost:")
 #port = raw_input("Enter remote hostname e.g. 6379:")
 host = "127.0.0.1"
 port = 6379
 session = telnetlib.Telnet(host,port)

 session.write("info \r\n")
 time.sleep(1)
 session.write("quit \r\n")
 data=session.read_very_eager()
 #print data
 for item in data.split("\n"):
  if "connected_clients" in item:
    print item.strip()
    print item.strip().split(':')[1]

if __name__ == '__main__':
 run_telnet_session()

Monitoring Current Job ready beanstalkd

#!/usr/bin/env python
# Very simple python monitoring redis connected clients.

import sys
import telnetlib
import time

def run_telnet_session():
 host = "127.0.0.1"
 port = 11300
 session = telnetlib.Telnet(host,port)

 session.write("stats\r\n")
 time.sleep(2)
 session.write("quit\r\n")
 data=session.read_very_eager()

 for item in data.split("\n"):
   if "current-jobs-ready" in item:
    print item.strip()
    print item.strip().split(':')[1]

if __name__ == '__main__':
 run_telnet_session()


mon_py_scripts

Easiest way “How to get tag from remote github repository”

when someone ask you, is it posible to get my latest tag from my github repository so i can put or add it into my automation deployment process for staging environment every morning without have to trigger it manually ? here it’s how to solve it


awan@google-provision:~$ MY_PROJECT=avengers
awan@google-provision:~$ git ls-remote --tags git@github.com:MY_REPOSITORY/"$MY_PROJECT".git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1

put it into variable and you can pass the variable into your automation scripts.

if you’re using ansible to deploy it, ansible has module to do this


- name: Get Latest Tag from my Repo
local_action: shell git ls-remote --tags git@github.com:MY_REPOSITORY/"$MY_PROJECT".git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1
register: my_last_tag

- name: Update Code Via Github
git: repo=git@github.com:MY_REPOSITORY/avengers.git
dest=/home/google/public
update=yes
accept_hostkey=yes
ssh_opts="-o ForwardAgent=yes"
version="{{ item }}"
with_items: my_last_tag.stdout_lines
register: code
sudo: no
tags: update