#1 VMWare의 master 노드에서 root의 두 키를 생성하고 이를 servera ~ c 의 konan 사용자에게 복사
< 모든 노드 >
1. 인스턴스를 확인하여 각 노드의 공인IP 확인
2. master 노드는 213.0.113.3으로 설정했기에 그대로 진행
vim /etc/hosts
> [각 공인IP] master.example.com
ping servera, b ,c
< 각 서버 노드에서의 사용자 생성 >
useradd konan
passwd konan
su - konan
usermod -aG wheel konan
sudo vim /etc/sudoers
sudo vim /etc/ssh/sshd_config
< master 노드 >
ssh-keygen -t rsa
ls -l .ssh/
ssh-copy-id konan@servera, b, c
#2 master노드에 ansible을 설치하고 inventory, ansible.cfg 파일 생성
- inventory
-
더보기[webservers]
server[a:c].example.com
[seoul]
servera.example.com
[busan]
serverb.example.com
serverc.example.com
[development]
serverc.example.com
[production]
servera.example.com
serverb.example.com
[korea:children]
seoul
busan - ansible.cfg
-
더보기[defaults]
inventory = ./inventory
remote_user = konan
ask_pass = false
command_warnings=false
interpreter_python=/usr/bin/python
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
yum install -y epel-release
yum install -y ansible
vim inventory
vim ansible.cfg
#3 webserver.yaml 파일을 사용하여 seoul 호스트에 nginx, busan 호스트에 httpd 패키지를 설치하고 서비스를 시작
- webserver.yaml
-
더보기---
- name: Install and start Apache web service
hosts: busan
vars:
web_pkg: httpd
firewall_pkg: firewalld
web_service: httpd
firewall_service: firewalld
python_pkg: python3
rule: http
tasks:
- name: Install required packages
yum:
name:
- "{{ web_pkg }}"
- "{{ firewall_pkg }}"
- "{{ python_pkg }}"
state: latest
- name: Start and enable the firewall service
service:
name: "{{ firewall_service }}"
enabled: true
state: started
- name: Start and enable the web service
service:
name: "{{ web_service }}"
enabled: true
state: started
- name: Create index.html file
copy:
content: “Bespin website index file"
dest: /var/www/html/index.html
- name: The firewall port for {{ rule }} is open
firewalld:
service: "{{ rule }}"
permanent: true
immediate: true
state: enabled
- name: Enable web services
hosts: seoul
become: yes
tasks:
- name: nginx is installed
yum:
name:
- nginx
state: latest
- name: html page is installed
copy:
content: "Welcome to the nginx cloud service!\n"
dest: /usr/share/nginx/html/index.html
- name: nginx enabled and running
service:
name: nginx
enabled: true
state: started
vim webserver.yaml
ansible-playbook --syntax-check webserver.yaml
ansible-playbook -v webserver.yaml
#4 임의의 index.html 파일을 생성하고 이를 각 웹서버의 홈 디렉토리로 복사하고 접속 테스트
echo 'test file' > index.html
ansible seoul -m copy -a 'src=index.html dest=/usr/share/nginx/html/index.html'
ansible busan -m copy -a 'src=index.html =dest=/var/www/html/index.html'
ansible all -m -a 'curl localhost'
curl servera, b, c
'Ansible' 카테고리의 다른 글
Ansible 반복문과 조건문 (0) | 2023.08.21 |
---|---|
Ansible-playbook & multiple (0) | 2023.08.17 |
Ansible 복습문제 (0) | 2023.08.17 |
Ansible 기본 (1) | 2023.08.16 |