Vagrant & Ansible - 10.14

양승현·2022년 10월 14일
0

vagrant 재시작 문제 발생 시

  • 일시중지 후 재 실행 > 네트워크 연결 문제
  • 가상머신 실행되지 않는 경우 : virsh start project1_control ; vagrant status
  • 네트워크 문제인경우
[root@hypervisor project1]# virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 project10            active     no           yes   <-- private
 vagrant-libvirt      active     no           yes   <--- default mgmt
virsh net-autostart vagrant-libvirt
virsh net-autostar project10
  • 위와 같이 설정 후 재 부팅 해 주기
  • centos | ubuntu 에 따라 조건을 걸고 실행되는 파일을선택할 수 있다.
install.yaml       ->     skip 너무 많은  install_include_tasks.yaml ->   줄일 수 있음 install_if.yaml -> skip 없앨 수 있음

if 조건을 이용하여 nginx 설치/삭제

[사전 요건]

    1. host 에서 echo "1" > /proc/sys/net/ipv4/ip_forward
    1. 필요파일
- install_nginx.yaml
- install_centos.yaml
- install_ubuntu.yaml
- remove_nginx.yaml
- remove_centos.yaml
- remove_ubuntu.yaml
    1. /etc/ansible/hosts @ control node
[centos]
centos1 ansible_host=10.10.10.11
centos2 ansible_host=10.10.10.12
centos3 ansible_host=10.10.10.13

[ubuntu]
ubuntu1 ansible_host=10.10.10.14
ubuntu2 ansible_host=10.10.10.15

[east]
centos1
centos2
ubuntu1

[west]
centos3
ubuntu2
vagrant@control:~/lab1$ tree
.
├── install_centos.yaml
├── install_nginx.yaml
├── install_ubuntu.yaml
├── remove_centos.yaml
├── remove_nginx.yaml
└── remove_ubuntu.yaml

설치

  • install_nginx.yaml
---
- name: install nginx
  hosts: all
  become: yes    # root 로 동작 만약 다른 사용자여야 한다면 become_user: user1
  vars:
    dist: "{{ 'centos' if ansible_distribution == 'CentOS'
               else 'ubuntu' if ansible_distribution == 'Ubuntu'
               else 'linux' }}"
  tasks:
    - name: install nginx per distribution
      include_tasks: "install_{{ dist }}.yaml"
  • install_centos.yaml
- name: install epel-release
  action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"

- name: install nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=present"

- name: create index.html
  get_url: url=https://www.nginx.com dest=/usr/share/nginx/html

- name: start nginx
  service: name=nginx state=started
  • install_ubuntu.yaml
- name: install nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=present"

- name: create index.html
  get_url: url=https://www.nginx.com dest=/var/www/html
  • 설치 시작
vagrant@control:~/lab1$ ansible-playbook install_nginx.yaml

삭제

  • remove_nginx.yaml
---
- name: remove nginx
  hosts: all
  become: yes    
  vars:
    dist: "{{ 'centos' if ansible_distribution == 'CentOS'
               else 'ubuntu' if ansible_distribution == 'Ubuntu'
               else 'linux' }}"
  tasks:
    - name: remove nginx per distribution
      include_tasks: "remove_{{ dist }}.yaml"
  • remove_centos.yaml
- name: remove epel-relase
  action: "{{ ansible_pkg_mgr }} name=epel-release state=absent"

- name: remove nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=absent"
vagrant@control:~/lab1$
  • remove_ubuntu.yaml
- name: remove nginx & etc...
  action: "{{ ansible_pkg_mgr }} name=nginx state=absent autoremove=yes purge=yes"

- name: stop nginx
  service: name=nginx state=stopped
vagrant@control:~/lab1$

group_vars & host_vars

group_vars

  • Inventory 파일 group 에 변수 정의
mkdir group_vars
touch group_vars/east

vagrant@control:~/lab1$ cat group_vars/east
 dist: "{{ 'centos' if ansible_distribution == 'CentOS'
           else 'ubuntu' if ansible_distribution == 'Ubuntu'
           else 'linux' }}"
  • cat install_nginx.yaml
---
- name: install nginx
  hosts: east
  become: yes
#  vars:
#    dist: "{{ 'centos' if ansible_distribution == 'CentOS'
#               else 'ubuntu' if ansible_distribution == 'Ubuntu'
#               else 'linux' }}"
  tasks:
    - name: install nginx per distribution
      include_tasks: "install_{{dist}}.yaml"
  • playbook 실행시 east 파일에 지정한 east group의 해당 node에게만 실행한다.

host_vars

mkdir host_vars
touch host_vars/centos1 host_vars/ubuntu1

변수 선언

  • cat host_vars/centos1
 dist: "{{ 'centos' if ansible_distribution == 'CentOS'
           else 'ubuntu' if ansible_distribution == 'Ubuntu'
           else 'linux' }}"
  • cat host_vars/ubuntu1
 dist: "{{ 'centos' if ansible_distribution == 'CentOS'
           else 'ubuntu' if ansible_distribution == 'Ubuntu'
           else 'linux' }}"

퀴즈! 화면에 다음과 같이 표기되도록 해 보세요!!

centos1 ->    
 Hello CentOS
211.183.3.161    <-- eth1

ubuntu1 ->
Hello Ubuntu
211.183.3.164

해설

cat check.j2

<!DOCTYPE html>
<html>
        <head>
                <meta charset="utf-8">
                <title>IP 와 배포판 확인 </title>
        </head>
<body>
        <center>
                <p><h2>Hello {{ ansible_distribution }}</h2></p>
                <p>IP ADDRESS : {{ ansible_eth1.ipv4.address }}</p>
        </center>
</body>
</html>
cat install_centos.yaml

- name: install epel-release
  action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"

- name: install nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=present"

- name: create index.html
  get_url: url=https://www.nginx.com dest=/usr/share/nginx/html

- name: start nginx
  service: name=nginx state=started

- name: template
  template:
    src: check.j2
    dest: /usr/share/nginx/html/check.html
    mode: 0644
  • 각 노드별 페이지에 정보가 추가 전달

필요한 파일 작성

vagrant@control:~/project3_role$ tree
.
├── nginx_install_with_roles.yml
└── roles
    └── nginx
        ├── defaults
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        ├── tasks
        │   ├── CentOS.yml
        │   ├── config.yml
        │   ├── main.yaml
        │   └── Ubuntu.yml
        ├── templates
        │   └── index.j2
        └── vars
            └── main.yml
  • tasks/main.yml
---
- name: 배포판 별 리눅스 설치
  include_tasks: "{{ lnx_name }}.yml"

- name: nginx 구성하기
  include_tasks: config.yml
  • roles/nginx/handlers/main.yml
---
- name: restart nginx web server
  service:
    name: nginx
    state: restarted
  • roles/nginx/tasks/config.yml
---
- name: 기본 웹 페이지 생성
  template:
    src: index.j2
    dest: "{{ nginxdir }}"
    mode: 0644
    backup: yes
  notify:
    - restart nginx web server
  • roles/nginx/tasks/Ubuntu.yml
- name: "{{ ansible_distribution }} 에 epel-release 설치하기"
  action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"
- name: "{{ ansible_distribution }} 에 nginx 설치하기"
  action: "{{ ansible_pkg_mgr }} name=nginx state=present"
roles/nginx/tasks/CentOS.yml 작성
- name: "{{ ansible_distribution }} 을 위한 nginx 설치"
  action: "{{ ansible_pkg_mgr }} name=nginx state=present update_cache=yes"
  • roles/nginx/templates/index.j2
<!DOCTYPE html>
<html>
        <head>
                <title>nginx web server</title>
        </head>
        <body>
                <center>
                <p><h2>ANSIBLE TEST PAGE</h2></p>
                <p>NGINX NUMBER : {{idx}}/{{nu}}</p>
                </center>
        </body>
</html>
  • roles/nginx/vars/main.yml
---
nu: "{{ groups.koreaeast | count }}"
idx: "{{ groups.koreaeast.index(inventory_hostname)+1 | int }}"
lnx_name: "{{ 'CentOS' if ansible_distribution == 'CentOS'
               else 'Ubuntu' if ansible_distribution == 'Ubuntu'
               else 'ETC' }}"

nginxdir: "{{ '/usr/share/nginx/html' if ansible_distribution == 'CentOS'
              else '/var/www/html' if ansible_distribution == 'Ubuntu'
              else 'ETC' }}"

0개의 댓글