수업 76일차 Ansible 앤서블 서버 , 웹 서버 , 플레이북 , NFS 서버 , 시간대 변경

유동우·2023년 1월 14일
1

#ansible all -m ping - root로 접속
$ansible all -m ping - vagrant로 접속

■ 앤서블 서버에 앤서블 코어 설치

PS C:\git_repo\02-ansible\one-server> vagrant ssh ansible-server
su -
vagrant

ansible all -m ping 치고 에러뜨면

vi /etc/ansible/hosts에서 제일 아래에

[nginx]
192.168.56.101
192.168.56.102
192.168.56.103

추가

cat ~/.ssh/known_hosts 치면 키가 3개나옴

ansible all -m ping -k
vagrant치고 접속

여기서 all 은 /etc/ansible/hosts
-m 은 모듈 ping은 모듈이름 -k는 패스워드 접속

ansible all -m ping --list-hosts // all 대상을 보여줌

ansible nginx -m shell -a "uptime" -k // shell 모듈을 사용해서 노드들의 업타임을 체크

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

■ 웹 서버 설치 및 실행

ansible all -m yum -a "name=httpd state=present" -k // yum을 이용해 httpd 웹 서버설치

curl https://httpd.apache.org -o index.html // curl로 아파치 웹 서버의 웹 페이지 "apache.org" 다운로드

ansible all -m copy -a "src=index.html \
dest=/var/www/html/index.html" -k // 다운로드 받은 웹 페이지를 앤서블 노드들에 전달

ansible all -m service -a "name=httpd state=started" -k // 웹 서버의 서비스를 시작

ansible all -m shell -a "systemctl stop firewalld" -k // 방화벽을 내림

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

■ 플레이북

vim 편집기 설치
yum install vim -y

vim nginx_install.yml


  • name: Install nginx on linux
    hosts: nginx
    gather_facts: no // 필요하지않은 내용을 수집하지 않는 옵션

    tasks:

    • name: install epel-release
      yum: name=epel-release state=latest
    • name: install nginx web server
      yum: name=nginx state=present
    • name: upload default index.html for web server
      get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/ mode=0644
    • name: start nginx web server
      service: name=nginx state=started

ansible all -m service -a "name=httpd state=stopped" -k // httpd서버 중지
ansible all -m shell -a "systemctl stop httpd" -k // httpd서버 중지

ansible-playbook nginx_install.yml -k // 앤서블 플레이북을 통해 nginx_install.yml 실행 이때 -k 옵션줘서 암호 입력받도록 해야함

플레이북은 윈도우에서 실행되지않아 3.2.3의 ansible_env_ready.yml파일을 복사해서
one-server에 붙혀넣어주고 Vagrantfile에

cfg.vm.provision "file", source: "ansible_env_ready.yml",
 destination: "ansible_env_ready.yml"
cfg.vm.provision "shell", inline: "ansible-playbook ansible_env_ready.yml"

추가해줌

저장하고

Powershell에서
PS C:\git_repo\02-ansible\one-server> vagrant provision

vagrant ssh ansible-server
ls하면
ansible_env_ready.yml

cat .bashrc

cat .vimrc

sudo mv /root/nginx_install.yml .

ls

sudo chown vagrant.vagrant nginx_install.yml

ll

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

■ 앤서블 서버에 앤서블 플레이북 실행 환경 추가하기

ansible_env_ready.yml파일에

tasks안에

- name: Add "/etc/ansible/hosts"
  blockinfile:
    path: /etc/ansible/hosts
    block: |
     [CentOS]
     192.168.56.101
     192.168.56.102
     192.168.56.103

추가

PowerShell로 와서

sudo vim /etc/ansible/hosts에
추가해줬던 nginx 다삭제
저장하고나와서
exit

PS C:\git_repo\02-ansible\one-server> vagrant provision

vagrant ssh ansible-server

tail -10 /etc/ansible/hosts 해보면
CentOs로 네트워크 주소 삽입되어있음(yml로 추가됨)

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

■ 앤서블 서버에서 앤서블 노드들에 대한 known_hosts의 키를 자동으로 입력하기

[known_hosts의 키 값을 자동으로 입력해주는 배시 스크립트]

VSCode로 와서 one-server에 파일생성
add-ssh-auth.sh라고 파일 만들어주고

#! /usr/bin/env bash

#ssh key 생성
sshpass -p vagrant ssh -T -o StrictHostKeyChecking=no vagrant@192.168.56.101
sshpass -p vagrant ssh -T -o StrictHostKeyChecking=no vagrant@192.168.56.102
sshpass -p vagrant ssh -T -o StrictHostKeyChecking=no vagrant@192.168.56.103

입력하고 저장

[앤서블 서버에 앤서블을 사용할 수 있는 환경을 구성해주는 야믈 파일]

ansible_env_ready.yml파일에
tasks 아래 block아래

- name: Install sshpass for Authentication
  yum:
    name: sshpass
    state: present

추가하고 저장

PS C:\git_repo\02-ansible\one-server> vagrant provision

vagrant ssh ansible-server

ls ~/.ssh/known_hosts

cat ~/.ssh/known_hosts

ans CentOS -m ping -k

ls

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

■ nginx 삭제 야믈

vim nginx_remove.yml


  • name: Remove nginx on CentOS
    hosts: CentOS
    gather_facts: no
    become: yes // root권한으로 실행

tasks:
- name: remove epel-release
yum: name=epel-release state=absent
- name: remove nginx web server
yum: name=nginx state=absent

저장

vim nginx_install.yml


  • name: Install nginx on CentOS
    hosts: CentOS
    gather_facts: no
    become: yes

tasks 위에 부분만 수정함
저장

anp nginx_remove.yml -k

nginx를 삭제했기때문에 192.168.56.1 ~ 3 접속이 안됨.

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

■ 시간대 변경하기

ans all -m shell -a "timedatectl | grep 'Time zone'" -k

vim timezone.yml


  • name: Setup CentOS timezone
    hosts: CentOS
    gather_facts: no
    become: yes

tasks:
- name: set timezone to Asia/Seoul
timezone: name=Asia/Seoul

anp timezone.yml -k // 실행

ans all -m shell -a "timedatectl | grep 'Time zone'" -k // timedatectl에서 Time zone 부분만 확인

sudo timedatectl set-timezone Asia/Seoul // ansible-server의 시간대도 변경해줌

timedatectl | grep "Time zone" // Time zone 부분 가져와서 확인

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

■ NFS 서버와 클라이언트를 구성하기

ansible-server에 node를 마운트시켜 연결

VSCode에 4.1.3 nfs.yml파일 복사해서

PowerShell로 와서
vim nfs.yml 만들고


  • name: Setup for nfs server
    hosts: localhost
    gather_facts: no

    tasks:

    • name: make nfs_shared directory
      file:
      path: /home/vagrant/nfs_shared
      state: directory
      mode: 0777

    • name: configure /etc/exports
      become: yes
      lineinfile:
      path: /etc/exports
      line: /home/vagrant/nfs_shared 192.168.56.0/24(rw,sync)

    • name: nfs service restart
      become: yes
      service:
      name: nfs
      state: restarted

  • name: Setup for nfs clients
    hosts: CentOS
    gather_facts: no

    tasks:

    • name: make nfs_client directory
      file:
      path: /home/vagrant/nfs
      state: directory

    • name: mount point directory as client
      become: yes
      mount:
      path: /home/vagrant/nfs
      src: 192.168.56.10:/home/vagrant/nfs_shared
      fstype: nfs
      opts: nfsvers=3
      state: mounted

저장하고 나와서

anp nfs.yml -k

ls

nfs_shared있는지 확인

ans all -m shell -a "ls -al | grep nfs" -k

[vagrant@ansible-server ~]$ ans all -m shell -a \

"cat /etc/hostname | xargs -i touch ./nfs/{}" -k

ls -l nfs_shared/

ans 192.168.56.103 -m shell -a "ls -l nfs/" -k

profile
클라우드 엔지니어가 되고싶은 클린이

0개의 댓글