Thursday, December 3, 2020

Ansible - task automation, install, start service and configure web service

 Day6- ansible

Ansible

- Control Node
- Managed node
    - Web Server (IP)


- hosts: myweb  # myweb comes from
  tasks:
  - package:
      name: "httpd"
  - copy:
      dest: "/var/www/html/index.html"
      content: " for lb testing"

  - service:
      name: "httpd"
      state: restarted

- hosts: mylb
  tasks:
  - name: "Install LB software"
    package:
      name: "haproxy"
~


add template


go to haproxy server and copy this file to your ansible controller node.

For me, I used same server for controller node and proxy.

[root@master ~]# cat lb.yaml
- hosts: myweb  # myweb comes from
  tasks:
  - package:
      name: "httpd"
  - copy:
      dest: "/var/www/html/index.html"
      content: " for lb testing"

  - service:
      name: "httpd"
      state: restarted

- hosts: mylb
  tasks:
  - name: "Install LB software"
    package:
      name: "haproxy"

  - template:
      dest: "/etc/haproxy/haproxy.cfg"
      src: "haproxy.cfg"

  - service:
      name: "haproxy"
      state: restarted


[root@master ~]# ansible-playbook lb.yaml


Go to your LB and check if you can access the sites...

if you have new host, just add it to ansible inventory and lb, run the play, it will configure..
one click, web server is ready and added to lb

Note: if you forget to update LB, adding new server does not work at all.


This concept is call scale -> horizontal scaling.. adding node
veritcal scaling - > adding cpu/ram/disk


go to load balancer, and check the config file...

check under baclend and add new host and rerun the lb.yml file.

add new host (horizontal scaling)

Challanges in terms of management
1. Knowing config file of every software is challanging
2. everytime, we have to go to control node and update new host

if there is a new IP address, update the config file and upload automatically..

we want new IP will be added to haproxy.conf automatically
or say we have old IP and we no longer using and need to be removed..

Task:
On controller node, when you have new IP is detected, configure web server, update the template file for haproxy and upload it to haproxy server?


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

How to configure docker using ansible
Steps

Note: docker-ce does not come on redhat iso.
1. Configure yum repo for docker
Get url from internet

2. Install docker
docker-ce

3. Start service

4. docker images

5. Run comtainers


Masternode: .50
Managed node: .51/.52

[root@master ~]# ping goo.gl
[root@master ~]# yum list docker-cd


go to your controller node.

# ansible-docs yumrepositry



- hosts: 192.168.10.50
  tasks:
  - name: setting up docker yum confuration
    yum_repository:
      name: df
      description: EPEL yum repo
      file: external_repo
      baseurl: https://download ....
      gpgcheck: no

# install the package
  - package:
      name: "docker-ce"
      state: present

We got error

Now, go to the system and try to install manually.

# yum install docker-ce

error: use '--nobest'

# yum install docker-ce --nobest

It installs successfully
but we want to perform this task using ansible.


google ansible package module
and go to docs.ansible

read the doc, we didn't find anything under package

google ansible yum module

ad-hoc commands,

[root@master ~]# ansible all -m command -a date
worker1 | CHANGED | rc=0 >>
Thu Dec  3 11:40:44 EST 2020
worker2 | CHANGED | rc=0 >>
Thu Dec  3 11:40:44 EST 2020
master | CHANGED | rc=0 >>
Thu Dec  3 11:40:44 EST 2020

Reboot all the machines
[root@master ~]# ansible all -m command -a reboot

reboot web servers
[root@master ~]# ansible myweb -m command -a reboot

rather than package module, use ad-hoc command

- hosts: 192.168.10.50
  tasks:
  - name: setting up docker yum confuration
    yum_repository:
      name: df
      description: EPEL yum repo
      file: external_repo
      baseurl: https://download.docker.con/linux/centos/7/x86_64/stable/
      gpgcheck: no

# install the package
#  - package:
#      name: "docker-ce"
#      state: present

  - command: "yum install docker-ce --nobest -v"


you will encounter same kind of problem on hadoop as well.
yum comand does not work with --force

need flag --force

For that you have to use command module and os specific command.
# rpm -ivh hadoop --force




Write a playbook that goes to aws-cloud and create an ec2 instance









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