Tuesday, February 15, 2022

Day2 - Ansible class notes

Ansible - class notes - 2/14/2022 by - Ravi 

Recap from last class,

Ansible
-------
Why do they use ansible?
- its a configuration management tool to save time. 
- Remote execution of configuration infomration
- Agentless (ssh communication), push mechanism.
- modules


google

ansible modules
go to - module index.


-----------------
install new instance
apt update
# apt 

# cat nginx.yaml

- hosts: remote
  vars:
  packagename: "nginx"
  tasks:
  - name: add repo nginx
  - name: install package nginx
    apt:
         name: "{{packagename}}"
         state: latest
  - name: create a dir tutorial
  - name: copy index.html file
  ....
handlers:
  - name: restart service nginx


# ansible-playbook -i hosts nginx.yaml

Everything came green, ok, ok..
The reason was - 
it was already done

in this example, we use a variable.

Get the ip and paste on the browser, you will see the page.


ansible roles:
we are going to create multiple roles, and those roles are called through playbook.

tmcat, docker,, IAM are seperate roles,

$ site.yaml

---
- hosts: remote

  roles:
  - nginx

# how do you declare 

# cd /etc/ansible
# mkdir roles;cd roles
# ansible-galaxy init niginx

lets install tree pkg through ansible.

# cd ../hosts
[remote]
192.168.10.100

# ansible -i hosts localhost -m apt -a "name=tree state=latest"

# tree roles/nginx

# vi hosts
[remote]
192.168.10.100

[local]
localhost

# all task section you can define  [00:30]

# vi tasks/main.yaml



---
- name:restart nginx
  service "{{pkgname}}
# vi tasks


# vi vars/main.yml

---
packagename: nginx


# cd /etc/ansible; ls 
tutorial
index.html
nginx.yml
roles
hosts
ansible.cfg
# mv index.html tutorial roles/nginx/files



# cp nginx.yml site.yml
# cat site.yml
---
- hosts: remote
  roles:
  - nginx


# ansible-playbook -i hsots site.yml

roles under stite.yaml will call from roles directory in 

# ansibleplaybook -i hosts site.yml

# cd /etc/ansible; cat site.yml
---
- hosts: remote
  roles:
  - nginx

nginx is going to be under roles directory
cd roles/nginx; ls -ltr
# cd tasks; ls -ltr
main.yml

# cat main.yml

we can use ansible galary to use roles

# vi nginx/vars/main.yml
---
packagename: nginx

# vi nginx/tasks/main.yml

# cd ..
# ansible-loayboot -i hosts site.yml

https://github.com/qf-devops/ansible-jenkins

$ cat jenkins.yml
---
- hosts: "webserver"
  roles:
    - naisble-java8-orale
    - jenkins
    - maven


anisble-galary install -r requirements.yaml

-------------------------------
variables/ different types of variables.

how to execute
ansible tower
-------------------------------

variables - vars
got to roles directory
# cd /etc/ansbile/roles; ls -ltr
# cd nginx; ls -ltr
# cd vars

static -> once allocated, value can not be changed.
dynamic -> default value can be override, mean it can be changed.

local variables - scope is local, you can not use in other yaml file
global variables -> you can use anywhere, available in key-value pair. it can collect the facts on target node.

# cd /etc/ansible
# ansible -i hosts remote -m setup

review the output. you will see key-value pair.

ansible_nodename: "ip"
ansible_os_family: ""

# cd roles/nginx/default
# cat main.yaml
---
version: 1:0.0

# cd ../tasks
# vi main.yaml

# print he value
---
- name: debug # module name is debug
  debug:
     msg: "vars: {{ version }}"

.......... [00:57:00]


# ansible-playbook -i /etc/ansible/hosts /etc/ansible/site.yml

see the output, goto debug section and you will see the version output.

# vi ../default/main.yml
removet eh version line and run the play book

# ap -i hosts site.yml
error: undefind variable..
# cd /etc/ansible
# vi ../defaults/main.yml
---
version: 0.0.0

we have to stie,
webserver1
adn webserver2
Where can you declare? 
vars is local variable and can't be overridden.

on inventory section you can defailne

[remote]




# /etc/ansible
# mkdir group_vars; group_vars/remote

# vi main.yml
---
version: 2.0.0v1


# vi /etc/hosts
[1:05:00]

# ap 


# vi etc/ansible/hosts
[remote]
192.168.10.20 version=1.2.3"
 see the output

# ansible-plabook..

# /etc/ansible
# mkdir host_vars

# mkdir 192.168.10.20
# vi main.yml
---
version: 3.00v1


# cat /etc/ansible 
# ap -o hosts site.yml

view the output, 


you can define -e at the promot too.

# ap -i hosts site.yml -e "version=4.0.0"

look at the output.


order that variable is read from,

extra_vars
host_vars
group_vars
default section


extra_vars - > vars 
Note: we can use all types of variables but only extra_vars value will be read.
cd roles/nginx/vars
#
vi main.yml
---
packagename: gninx
dbname: cassandra

vi ../../task/main.yaml
---
- name: debug
  debug
  msg: "vars: {{ version}} {{dbname }}"

# ap -i hosts site.yml -e "version=4.0.0" -e "dbname=mongod"

it will pick up the value you speacify at the command line.

certificate to deploy for all host, use host_vars

for dev, prod, test
extra_vars -> extra is a high priority so it will pick it up first and executed from the inventory file.


# ap -i hosts /etc/ansible.siteyml -e "version=4.0.0" -e "dbname=mongod"


nested variables

in case of aws,
we have multiple datacenter in a region

we have multiple regions

google for default group

how do you handle multiple group?

group variables for groups of group
----------------------------------

[atlanta]
host1
host2

[releigh]
host2
host3

[southeast: children]
atlanta
releigh

[southeast:vars]
....

[usa:children]
southwest
northeast
southwest
nothewest




all:
  children
    usa:
....

No comments:

Post a Comment

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. ...