Sunday, December 13, 2020

Kubernetes - kubectl apply command

 kubectl apply command
# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end-service
  sepc: 
    containers:
    - name: nginx-container
      image: nginx:1.19
# kc apply-f naginx.yaml
live update definition
when you run apply command, if object is not created, it  will create it.

when you run this command, the yaml format is converted to json format and is stored as last applied configuration.

when you updated say verison of nginx to 1:19, in our local file and run kubectl apply command,
this value willbe compare with live config file. if there is difference, live config is updated with the 
new value. after apply, the last updated configuration json file is updated at last.
Last updated configuration file is always uptodated.
say, if one of the field is deleted,  and run apply command, and your last updated config file and that file available, 
then it will remove from live configuration.
so, we have three copy of configuration file

1) your local copy that you created
nginx.yaml
2) last applied configuration on the system
in JSON format
3) live object configuration file
you can modify it on the fly.
Note: if you use apply option, live object config has entry for last-applied-configuration
create/replace wont add entry to live config file..
apply command checks all three files.





Kubernetes - imperative and declarative way of managinv infracture


Imperative way of managing infracture is like 

A. Create objects
1. Create a pod
# kc run --image-nginx nginx
2. Create deployment
# kc create deployment --image=nginx nginx
3. Create a service to expose a deployment 
# kc expose deployment nginx --port 80

B. Update objects
4. Edit an existing object
# kc edit deployment nginx
5. Scale the deployment replica set
# kc scale deployment nginx --replicas=5
6. Update in image on deployment 
# kc set image deployment nginx nginx=nginx:1.18
These above command remain on session history
7. Update configuration
# kc create -f nginx.yaml
8. Editing an object using replace command
# kc replace -f nginx.yaml
9. Delete object
# kc delete -f nginx.yaml

declrartive approache for infracture
# kc apply -f nginx.yaml


A. Create objects
# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
  sepc: 
    containers:
    - name: nginx-container
      image: nginx
# kc create 0f naginx.yaml

B. Update object
If youlike to update any value, use edit..
# kc edit deployment nginx
when you run edit command, it opens with some additional fields, such as status:
which stires the status about the POD,
if you edit and save the file, its a live change. your local copy might not be upto date..
to resolve this proble, first edit the local copy of the file and make changes. and run the replace command to update the object.
# kc replace -f nginx.yaml
or you can complately delete and recreate object.
# kc replace --force -f ngins.yaml

This is still an imperative approach.
when you run replace option, make sure object exists before running the replac ecommand.
other wise it will fail.


Declarative approach.
Create approach - using apply command. it will create object if its already not created.
# kc apply -f nginx.yaml
or you can specify a directory rather than a file. This way all the files are created.
# kc apply -f /path-to_config_files/

Update objects
if you need to make change, you make change t config file and run the apply command
# kc apply -f nginx.yaml
if object exists, it wil not throw error. adding or updating will not have any problem,

In the exam, use imperative approach to create pod or deployment.
to edit you can use edit but for long time use, use declarative approach.

By default, when you run the command, resources will be created. Before you create resources,
you would like to test your commands, use the option --dry-run=client option.
This will not create resources but will check if your command is right.
you can also use -o yaml to see the resource definition output in yaml format on the screen.
 
POD
1. create an nginx pod
# kc run nginx --image-nginx
2. Generate POD manifest YAM lfile (use -o yaml  and --dry-run option to not to create object)
# kc run nginx --image=nginx --dry-run=client -o yaml

Deployment
1. create a deployment
# kc create deployment --image=nginx nginx
2. Generate deployment yam file (using -o yam and --dry-run option)
# kc create deployment -image=nginx nginx --dry-run -o yaml
3. Generate Deployment with 4 replicas
# kc create deployment nginx --image=nginx --replicas=4
4. Scale your deployment
# kc scale deployment nigix --replicas=4
or you can also create a yaml file and use it later
# kc create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml
Using this file, you can update the change with replicas or any other values before creating the deployment.

Service
1. Create a service called redis-service of type ClusterIP to expose the pod redis on port 6379
# kc expose pod redis --pod=6379 --name redis-service --dry-run=client -o yaml
it will automatically use the POD's label as a selector.
or
# kc create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
This will not use pod labels as a selector, instead it assume selectors as app=redis.
Note: It does not work if your pod has a different lable set so first generate a file and modify the selector before createing the service.
2. Create a service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes:
# kc expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml
This will automatically use the pod's label as selectors but you can not specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.
or 
# kc create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
This iwll not use pods label as selectors
Here both ommands are not perfect. One can not accept a selector while other can not accept node port. 
Use kubectl expose command and generate a file. manually update nodeport before creating the service.
source: https://kubernetes.io/docs/reference/kubectl/conventions/

Saturday, December 12, 2020

Ansible - error handling

 Class notes: 
ansible-class-notes-Dec-11-2020

PB
- set up
- docker install
- image centos
- os continer
- config web server


CN ->       ssh (IP) enable 

Docker, -> OS -> Continer
Run time environment.
Docker
PODMAN
CRI-O
kubernetes

DevOps
- ansible
===============================
# cat web.yaml
- hosts: w1
  tasks:
  - command: "date"
  - package:
         name: "httpd"
  - service:
      name: "httpd"
      state: "started"
# ap.web.yaml
successful, but if type miss-type a work, what happens? all your task fails..

control the error which is called error handling...


what we want to do it, if somehing fails, continue. lets try with ignore_errors key word
[root@master wk-11]# vi web.yaml
- hosts: w1
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: "httpd"
  - service:
      name: "httpd"
      state: "started"
[root@master wk-11]# ansible-playbook web.yaml
TASK [command] *****************************************************************************************
fatal: [w1]: FAILED! => {"changed": false, "cmd": "dates", "msg": "[Errno 2] No such file or directory: b'dates': b'dates'", "rc": 2}
...ignoring

know what you are doing. can you ignore the error?
- hosts: w1
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: "httpd"
  - service:
      name: "httpd"
      state: "started"

# cat kb.conf.j2
Listen 8080

# ap web.yaml
# ap -v web.yaml
always checks changed features... idompotence
- we see, even there is no change, its keep running.
- and also service is keep restarting...
notify
we tell template or copy module, notify if there is any change...
it will do remaining handling.... 
we can create handler
its like 
[root@master wk-11]# cat web.yaml
- hosts: w1
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: "httpd"
  - copy:
      dest: "/var/www/html/index.html"
      content: "Hello hi"
  - template:
      dest: /etc/httpd/conf.d/kb.conf
      src: "kb.conf.j2"
    notify: web services
  handlers:
  - name: web services
    service:
      name: "httpd"
      state: "restarted"
  - debug:
      msg: "test"
  - service:
      name: "haproxy"
[root@master wk-11]# cat kb.conf.j2
Listen 8080
[root@master wk-11]#
# ap web.yaml

Nothing is changed.
Pls disable firewall and selinux and change
lets change the port and run it again.
[root@master wk-11]# cat kb.conf.j2
Listen 85
[root@master wk-11]# ansible-playbook web.yaml
review the output.

use when or lets use variable...

- hosts: w1
  vars:
  - os_name: ansible_facts[ 'distribution' ]
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: "httpd"
  - copy:
      dest: "/var/www/html/index.html"
      content: "Hello hi"
  - template:
      dest: /etc/httpd/conf.d/kb.conf
      src: "kb.conf.j2"
    notify: web services
  handlers:
  - name: web services
    service:
      name: "httpd"
      state: "restarted"
  - debug:
      msg: "test"
  - service:
      name: "haproxy"
# you can use register if this task changed, run that ...

lets say you have different pkg name
- hosts: w1
  vars:
    - os_name: ansible_facts[ 'distribution' ]
    - p1: "httpd"
    - p2: "apache2"
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: p1
    when: os_name == "RedHat" and os_var == 7
  - copy:
      dest: "/var/www/html/index.html"
      content: "Hello hi"
  - template:
      dest: /etc/httpd/conf.d/kb.conf
      src: "kb.conf.j2"
    notify: web services
  handlers:
  - name: web services
    service:
      name: "httpd"
      state: "restarted"
  - debug:
      msg: "test"
  - service:
      name: "haproxy"
# you can use register if this task changed, run that ...

can you write code in such that it finds ip , os version and software that you want to install.. ??
lets create extra files
OS specific variable files

# vi RedHat-7.yml
-p: "httpd"
# vi RedHat-6.yml
-p: "httpd"
# vi Ubuntu-14.04.yml
-p: "apache2"


- hosts: w1
  vars_files:
      - "RedHat-6.yaml"
  vars:
    - os_name: ansible_facts[ 'distribution' ]
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: p1
    when: os_name == "RedHat" and os_var == 7
  - copy:
      dest: "/var/www/html/index.html"
      content: "Hello hi"
  - template:
      dest: /etc/httpd/conf.d/kb.conf
      src: "kb.conf.j2"
    notify: web services
  handlers:
  - name: web services
    service:
      name: "httpd"
      state: "restarted"
  - debug:
      msg: "test"
  - service:
      name: "haproxy"
# you can use register if this task changed, run that ...

This way also, you have lots of file and hard to manage. You need something intellegent decision to make.

in all files, name is different, but the variable is same. OS is different..
# cat web.yaml
- hosts: w1
  vars_files:
      - "RedHat-6.yaml"
  vars:
    - os_name: ansible_facts[ 'distribution' ]
  tasks:
  - command: "dates"
    ignore_errors: yes
  - package:
         name: p
  - copy:
      dest: "/var/www/html/index.html"
      content: "Hello hi"
  - template:
      dest: /etc/httpd/conf.d/kb.conf
      src: "kb.conf.j2"
    notify: web services
  handlers:
  - name: web services
    service:
      name: "httpd"
      state: "restarted"
  - debug:
      msg: "test"
  - service:
      name: "haproxy"

Thursday, December 10, 2020

YAML - an intro

 YAML Tutorial - Yet Another Markup Language.

YAML is indentation-based markup language. The main purpose of this language is easy to read and write. JSON and YAML are very similar but JSON uses brackets and braces.
---
  bird: "sky is where he plays"
  fish: "Ocean is where he plays"
  pi: 3.1415
  rice: 3
  fruits:
    - apple
    - banana
    - straberry
    - "Pine Apple"
  party-supply:
    fruits: four
    rice: 3
    tomato: 25
    chicken:
      count: 5
      location: "Bob's Poltry"

YAML syntax

- This file starts with three dashes(---), which indicates that the start of new YAML document.
- Next we see, a key-value pair. bird is a key pointing to sky is where he plays.
Note: Newline indicates the end of a field and indentation can be one ormore spaces and no tab.

Here, we see different data types.
bird and fish are strings.
pi is integer
rice is 3 bags an integer.
strings can be enclosed in single, double or no quotes.
fruits has 4 values defined by -.
Indentation is very importand in yaml. We intended with two space and hyphen and a space.
You can not use tab. This is how you can nexted the values in YAML.

Now, look at the values on party supply. we see 4 intended values (element). We see it as a dictionary which contains one string value, two numerical value and another dictionary (chicken).

We see values are nexted and mix.

Comments:
YAML comments begin with a # (hash) sign and can appear anywehre in the document.
ANything after the # sign will be ignored.

- - -
# This document contains info about birds
  bird: "Fly high"



YAML Datatypes
---------------
Values in YAML key-value pairs are scalar. Its a good practice to enclose strings in quotes and leave the number unquoted.

- key is always string and the value is a scalar so it can be of any data type such as string, number or dictionary.


Numeric types
YAML supports different types of numeric data types. The number (integer) can be of decimal, hexadecimal or octal.

---
  serial-number: 123456
  student-id: 8976567
  bar-code: F2AA215


Strings
YAML strings are unicode.
---
  greet: "Good Morning"

Note: \ - escape sequence.
\n - new line. if you want \ on your return value, do not use double quote.

---
  greet: "Good Morning \n"
  bye: Have a great day \n


You can specify multiline value using fold (greater than sign)

  inst: >
    Please note: user must have admin access to
    the system and enough space on the disk.
    Please review before proceed ....

The above lines will be a single line. If you want the way it is written, use pipe(|) instead of fold (>).


Nulls





Scalars: scalars are ordinary values: numbers, strings, booleans. YAML syntax also allows unquoted string values for convenience. suc as player: Peter Johnson

number-value: 25
floating-point-value: 3.1415
boolean-value: true
string-value: 'John'

Lists and Dictionaries:
Lists are collections of elements:

players:
 - John
 - Mary
 - Peter
 - Bill

Every element of the list is indented and starts with a dash and a space.

Dictionaries are collections of key: value mappings. All keys are case-sensitive.









 from different sources....

Ansible - Install software - capture output - debug

 Ansible - Notes
----------------------------

what we want ?

Step1. Install software

RAL: Module
package : specify name of the software-package. say httpd for RHEL
what is you have some other OS, say ubuntu, software for httpd is apache2.
 
RHEL7 -> facts -> yum ..
ubuntu -> apt get

since package name is different of different OS, ansible can't help you on this situation.

How to install the software -ansible takes care of it.

You have to tell what you want. This is going to be part of playbook.


package
- httpd -> install this package if os=rehl
- apache2 -> install this software only if os=ubuntu

in ansible, we don't use if else or case.

we write condition using when...

Condition is like -
install httpd when os is redhat


how they find os name? - using facts -> facts gather all info about the system and stored under ansible_facts.

You can store os name on osname variable.


Note: You need to know the manual steps before you can autimate the tasks.

Lets look at the example.


- hosts: myweb
  tasks:
  - package:
      name: "httpd"

if you run this playbook, it will fails


- hosts: myweb
  tasks:
  - package:
      name: "apache2"

it works on ubuntu

--------------------


$ cat myweb.yaml
- hosts: myweb
  tasks:
  - package:
      name: "httpd"


- hosts: myweb
  tasks:
  - package:
      name: "apache2"

You get all variables by running
# ansible 192.168.10.55 -m setup | less

search for RedHat
you will seee ansible_distribution



$ cat myweb.yaml
- hosts: myweb
  vars:
  - x: "redhat"
  tasks:
  - package:
      name: "httpd"
    when: x == "redhat"

see the variable x has a redhat value, if condition meets it runs

# ap myweb.yaml

and it is successful

lets say, its not a good practice to hardcode the OS name, so lets try this way,

$ cat myweb1.yaml
- hosts: myweb
  vars:
  - os_name: ansible_facrs[ansible_distribution]
  tasks:
  - package:
      name: "httpd"
    when: os_name == "redhat"

[root@master wk10]# ansible-playbook myweb1.yaml


you see it skips. see the case? facts is uppercase


[root@master wk10]# cat myweb1.yaml

- hosts: myweb
  vars:
  - os_name: ansible_facrs[ansible_distribution]
  tasks:
  - package:
      name: "httpd"
    when: os_name == "RedHat"


[root@master wk10]# ansible-playbook myweb1.yaml


it still skip

------------------------------------------
lets debug what happening


[root@master wk10]# more myweb1.yaml

- hosts: myweb
  vars:
#  - os_name: ansible_facrs[ansible_distribution]
  - x: "John"
  tasks:
  - package:
      name: "httpd"
    when: os_name == "RedHat"


  - debug:
#      var: ansible_facts[ansible_distribution]
#      var: os_name
      var: x

# String intropolation or something
  - debug"
      msg: " Hey {{ x }}
#      msg: "Hi Hello {{ os_name }}
[root@master wk10]#

------------------------------------------

Lets re-write it again...
- hosts: myweb
  vars:
  - os_name: "{{ ansible_facts['distribution'] }}"
  - x: "John"
  tasks:
  - package:
      name: "httpd"
    when: os_name == "RedHat"


  - package:
      name: "apache2"
    when: os_name == "Debian"

  - debug:
      var: x

[root@master wk10]# ansible-playbook myweb.yaml

Finally it is successful...



ad-hoc command

ansible 192.168.10.20 -m command -a date
[root@master wk10]# ansible worker1 -m command -a date
worker1 | CHANGED | rc=0 >>
Thu Dec 10 11:27:05 EST 2020


$ cat anc.yaml
- hosts: worker1
  tasks:
  -  command: date

  - debug:
      msg: "hi test !!!"

    

when you run the playbook, they hide the output.

You can use -v option to see the output..

[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date

  - debug:
      msg: "hi test !!!"
[root@master wk10]# ansible-playbook -v abc.yaml

review output
changed -> true -> made change...

- v shows the output in detail...
[root@master wk10]# ansible-playbook -v myweb.yaml


debug module, only run when changed is false ...

how do we do this?

When you run the playbook, by default they hide the output. so use -v to see the out put of the command module..

store all the output to register variable x
x contains entire output of task output.


[root@master wk10]# cat  abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"

  - debug:
      var: x


This time, entire output is display. because we say to print debug module to store the output to variable x and print it.
[root@master wk10]# ansible-playbook abc.yaml



if you review the output x has all values in array like format

to print particular value
you have to do like
x.rc



- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"

  - debug:
      var: x.rc

[root@master wk10]# ansible-playbook abc.yaml

ok: [worker1] => {
    "x.rc": "0"


----------------
[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc == 0

  - debug:
      var: x.rc


change and run it again
[root@master wk10]# ansible-playbook abc.yaml



Use with not

- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc != 0

  - debug:
      var: x.rc


[root@master wk10]# ansible-playbook abc.yaml

lets modify the yaml file

[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - service:
      name: "httpd"
      state: "started"
    register: y
  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc != 0

  - debug:
      #var: x.rc
      var: y


and run it now,

[root@master wk10]# ansible-playbook abc.yaml

check failed: false

you can write condition here as well.. if this successful, do next ...

if package is not installed, do not start service - does not make sense...


start the service only if software is running.

if this successful, write the firewall rule...

ask debug module to

  - debug"
      msg: "final message"
    when: x.rc == 0 && y.failed == false
# when both conditions are true, then only run the next tasks.


[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - service:
      name: "httpd"
      state: "started"
    register: y

  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc != 0

  - debug:
      #var: x.rc
      var: y

  - debug:
      msg: "final message"
    when: x.rc == 0 and y.failed == false
[root@master wk10]#

[root@master wk10]# ansible-playbook abc.yaml


[root@master wk10]# cat ../myhosts

[mylb]
master  ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh

[myweb]
worker1 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh
worker2 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh

[nfs_server]
master  ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh

[nfs_clients]
worker1 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh


Write code in such a way that it runs..
think what if you have diff os,
what to do with new IP added, how to configure...


========================================


aws -> facts -> output
public_ip came none...


- store the output to one variable and print the public ip..





Wednesday, December 9, 2020

Ansible - EC2 instance creation using ansible..

1. Install boto3

[root@master wk-dec9]# pip3 install boto3
Successfully installed boto3-1.16.33 botocore-1.19.33 s3transfer-0.3.3 urllib3-1.26.2

 2. Write your playbook

 


# cat aws-ec2.yaml
- hosts: localhost # 192.168.56.4 - your own control node)
  tasks:
  - ec2_instance:
      region: us-east-1
      image_id: ami-04d29b6f966df1537
      instance_type: t2.micro
      #image: t2.micro
      vpc_subnet_id: subnet-e261d2ec
      security_group: sg-f5b18ad2
      key_name: kt-2020-k
      name: os_from_ansible
      state: present
      aws_access_key: AKIA6DEA42GA2PGZJ7G3
      aws_secret_key: 3IYF568qVJ8I#RZYnUV2OPG8/XDKVrhDfJRJPnbc

[root@master wk-dec9]# ansible-playbook aws-ec2.yaml

PLAY [localhost] *****************************************************************************************

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

TASK [ec2_instance] **************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (botocore or boto3) on master's Python /usr/bin/python3.6. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

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


[root@master wk-dec9]# pip3 install boto3
Successfully installed boto3-1.16.33 botocore-1.19.33 s3transfer-0.3.3 urllib3-1.26.2


[root@master wk-dec9]# ansible-playbook aws-ec2.yaml

PLAY [localhost] *****************************************************************************************

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

TASK [ec2_instance] **************************************************************************************

changed: [localhost]

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

[root@master wk-dec9]#
[root@master wk-dec9]# cat aws-ec2.yaml
- hosts: localhost # 192.168.56.4 - your own control node)
  tasks:
  - ec2_instance:
      region: us-east-1
      image_id: ami-04d29b6f966df1537
      instance_type: t2.micro
      #image: t2.micro
      vpc_subnet_id: subnet-e251d2ec
      security_group: sg-f7a18ad2
      key_name: kb-2020-key
      name: os_from_ansible
      state: present
      aws_access_key: AKIC6HXA42MR2PGZJ7G3
      aws_secret_key: 3IYF590qVJ8ISpZYnUV92PG8/XDKVrhHsJcMPnbc


--------------------------------------------------------------------

Ansible - class

We are using ansible to provision the server on AWS

1. Install OS
2. Configure OS

1. For OS provisioning, we use tools like
- Foreman
- Cloudformation - aws
- VMWare
- Terraform
- Ansible (too)

2. Configuration Management
- ansible
- puppet
- chef

You can use ansible to provisioning the OS but its not feature rich to provision the server.

Dynamic inventory
-----------------

CN -> ansible running
Ansible needed 3 things
1. Username
2. Password
3. Client IP - need to add it to the inventory file
Worker node ->
- RHEL
- Windows

How to write a playbook for a system that does not exists?

Router/cloud (aws)/firewall

- Webapp
- cli
- API (for custom requirement - interpreted -> programming -> (java, go, python) while loop, for loop ..

Ansible (CM) -->  ssh  ->   RHEL8 (TN)
Pythoncode   -> https  -> Router (login, config)

API - > Playbook
- run playbok on your own control node using its own IP (lo-localhost)

run the program over http
-> go to the client -> URL (browser - firefox)

you run from client side

control node -> create and run play book -> run on target node (router, server) -> using API of say EC2 - execute the playbook.

---------------------
ansible comes with lots of modules
# ansible-docs -l | grep ec2

review the output and look for ec2.insance .. -> create and manage

# ansible-docs ec2.instance

------------------------

Provisioning AWS ec2 instance

- Lets collect manual tasks

1. Select your region
region: mumbai ap-south-1

2. Select EC2

google ansible ec2 module - create and manage
(requires - boto3 library)

on your control node, you have to install boto library before running playbook.
# pip3 install boto3

# ansible-doc ec2_instance

ansible

2. Choose OS type
copy AMI-ID

os image: ami-o9f63....ff5f

3. Select Instance type:
Instance type: t2.micro

4. How many?
Count: 1

5. Define subnet (data center
subnet: subnet-9898888

6. Storage: by default it picks

7. firewall or by default it uses default one
firewall: sg-0be7...53d
8. review and launch..
need key using ssh
key: awskey2020-key

So, lets put all together. the stuff needed to launch the EC2 instance

-m ec2_instance
-a
region: mumbai ap-south-1
image_id: ami-o9f63....ff5f
instance_type: t2.micro
vpc_subnet: subnet-9898888
security_group: sg-0be7...53d
key_name: awskey2020-key
state: present

write playbook

- hosts: localhost # 192.168.56.4 - your own control node)
  tasks:
  - ec2_instance:
      region: mumbai ap-south-1
      image_id: ami-o9f63....ff5f
      instance_type: t2.micro
      vpc_subnet: subnet-9898888
      security_group: sg-0be7...53d
      key_name: awskey2020-key
      state: present

if you run this play, it will fail. because, you have to first login.

You will use key to logic

access key - user name
secret is like pw

- hosts: localhost # 192.168.56.4 - your own control node)
  tasks:
  - ec2_instance:
      region: mumbai ap-south-1
      image_id: ami-o9f63....ff5f
      instance_type: t2.micro
      vpc_subnet: subnet-9898888
      security_group: sg-0be7...53d
      key_name: awskey2020-key
      state: present
      aws_access_key: SGJHGFGFJKFJKFJF
      aws_secret_key: KHJHJJKJHJKHKJH

go to services -> IAM service -> create your own access key/secret key.

go to users ->
username: sam
access type -> select programmatic access -> next
attach existing policy -> give admin access
review next and finish
you will see access key

This key is very importand, keep it secret.

We are using ansible to provision the server on AWS

Run this code on your controller node.

# cat aws-ec2-create.yaml
- hosts: localhost # 192.168.56.4 - your own control node)
  tasks:
  - ec2_instance:
      region: mumbai ap-south-1
      image_id: ami-o9f63....ff5f
      instance_type: t2.micro
      vpc_subnet: subnet-9898888
      security_group: sg-0be7...53d
      name: os_from_ansible
      key_name: awskey2020-key
      state: present
      aws_access_key: SGJHGFGFJKFJKFJF
      aws_secret_key: KHJHJJKJHJKHKJH


# ap --list-hosts

# ansible 192.168.10.120 --list-hosts

# ansible 127.0.0.1 --list-hosts



RHEL - Scan every disk on the system

 [root@master kube]# cat ../disk-scan.sh
for i in `ls /sys/class/scsi_host`; do echo "- - -" >/sys/class/scsi_host/${i}/scan; done


Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...