Ossian Story
article thumbnail

[VAGRANT & ANSIBLE] CentOS Ansible SSH TEST 환경 구성





VAGRANT & ANSIBLE CentOS Test 환경 구성


이번 포스팅은 아래의 구성도와 같이 Vagrant로 VM을 Provisioning하고 난 뒤 Ansible을 통해 Ansible-Client의 SSH 설정을 하는 방법에 대해 알아보도록 하겠습니다.


[ Vagrant 기초 사용 방법 ]


1. [Vagrant] 설치 및 기초 사용방법 - Windows

2. [Vagrant] vagrantfile 기초 작성 방법



[ Ansible 기초 사용 방법 ]


1. [Ansible] Install - CentOS 7.x

2. [Ansible] ad-hoc 명령어 - CentOS 7.x





CentOS VM Provisioning을 위한 Vagrantfile 작성



[ vagrantfile 진행 과정]




[ vagrantfile 작성 내용]


# -*- mode: ruby -*-
# vi: set ft=ruby :
 
vagrant_AIP_version = "2"
 
Vagrant.configure(vagrant_AIP_version) do |config|
 
  # ansible-client01 setting
  config.vm.define:"ansible-client01" do |cfg| # vagrant에서 정의되는 이름
    cfg.vm.box = "centos/7" # vagrant에서 설치할 OS
    cfg.vm.provider:virtualbox do |vb| # vagrant와 연계하여 작동할 Provider 지정
      vb.name = "ansible-client01"  # virtualbox에서 보여질 VM 이름
      vb.customize ["modifyvm", :id, "--cpus"1]  # virtualbox에서 설정할 VM CPU
      vb.customize ["modifyvm", :id, "--memory"512]  # virtualbox에서 설정할 VM Memory
    end
    cfg.vm.host_name = "ansible-client01"  # OS에서 보여질 VM 이름
    cfg.vm.synced_folder ".""/vagrant", disabled: true  # Host와 VM간의 공유폴더 설정으로 현재 옵션은 사용하지 않음으로 ㄹ정
    cfg.vm.network "public_network", ip: "192.168.0.101"  # VM의 Network 설정
    cfg.vm.network "forwarded_port", guest: 22, host: 19211, auto_correct: false, id: "ssh" # Host와 VM간의 Network Forward 설정
    cfg.vm.provision "shell", path: "client_sshd_setting.sh"  # VM의 기본설정 완료 후 Host의 "client_sshd_setting.sh"파일을 VM으로 복사하여 Shell로 실행
  end
 
  # ansible-client02 setting
  config.vm.define:"ansible-client02" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vm.provider:virtualbox do |vb|
      vb.name = "ansible-client02"
      vb.customize ["modifyvm", :id, "--cpus"1]
      vb.customize ["modifyvm", :id, "--memory"512]
    end
    cfg.vm.host_name = "ansible-client02"
    cfg.vm.synced_folder ".""/vagrant", disabled: true
    cfg.vm.network "public_network", ip: "192.168.0.102"
    cfg.vm.network "forwarded_port", guest: 22, host: 19212, auto_correct: false, id: "ssh"
    cfg.vm.provision "shell", path: "client_sshd_setting.sh"
  end
 
  # ansible-master setting
  config.vm.define:"ansible-master" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vm.provider:virtualbox do |vb|
      vb.name = "ansible-master"
      vb.customize ["modifyvm", :id, "--cpus"1]
      vb.customize ["modifyvm", :id, "--memory"512]
    end
    cfg.vm.host_name = "ansible-master"
    cfg.vm.synced_folder ".""/vagrant", disabled: true
    cfg.vm.network "public_network", ip: "192.168.0.100"
    cfg.vm.network "forwarded_port", guest: 22, host: 19210, auto_correct: false, id: "ssh"
    cfg.vm.provision "shell", path: "ansible_install.sh"  # Ansible을 설치하는 Shell Script
    cfg.vm.provision "file", source: "ansible_setting.yml", destination: "ansible_setting.yml" # Ansible-Playbook에 사용 될 Yml파일을 VM으로 전송
    cfg.vm.provision "shell", inline: "ansible-playbook ansible_setting.yml"  # VM에서 "ansible-playbook ansible-setting.yml" 명령어 ㅣㄹ행
    cfg.vm.provision "shell", path: "client_knownhost_setting.sh", privileged: false # Host의 "Client_knownhost_setting.sh"파일을 VM으로 복사하여 vagrant 권한으로 Shell 실행
  end
end



[ cfg.vm.provision 옵션 ]

- "shell", path : Host의 파일을 VM으로 복사한 후 실행

- "shell", inline : VM에서 지정된 명령어 실행

- "file", source, destination : Host의 파일을 VM으로 복사 



[ client_sshd_setting.sh 작성 내용]


#! /usr/bin/env bash
 
now=$(date +"%m_%d_%y")  # now 변수에 일+월+년을 생성
 
cp /etc/ssh/sshd_config /etc/ssh/sshd_config_$now.backup # sshd_config 파일을 백업
 
sed --'s/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config # sshd_config의 "PasswordAuthentication no를 Yes로 변경
 
systemctl restart sshd  # sshd 




[ ansible_install.sh 작성 내용]


#! /usr/bin/env bash
 
yum -y install epel-release  # epel-release 저장소 설치
yum -y install ansible  # ansible 설치




[ ansible_setting.yml 작성 내용]


---
- name: Ansible Setting
  hosts: localhost
  gather_facts: no
  become: yes
 
  tasks:
    - name: Add client "/etc/hosts"
      blockinfile: |
        dest=/etc/hosts
        content="
          192.168.0.100 ansible-master
          192.168.0.101 ansible-client01
          192.168.0.102 ansible-client02"
    
    - name: Add Ansible-host client "/etc/ansible/hosts"
      blockinfile: |
        dest=/etc/ansible/hosts
        content="
          [centos]
          ansible-client01
          ansible-client02"
    
    - name: Install sshpass
      yum:
        name: sshpass
        state: latest
 
    - name: Change sshd_config
      replace:
        dest: /etc/ssh/sshd_config
        regexp: "PasswordAuthentication no"
        replace: "PasswordAuthentication yes"
    
    - name: Restart sshd
      service:
        name: sshd
        state: restarted




[ client_knownhost_setting.sh 작성 내용]


#! /usr/bin/env bash
 
sshpass -p vagrant ssh --o StrictHostKeyChecking=no vagrant@ansible-client01
sshpass -p vagrant ssh --o StrictHostKeyChecking=no vagrant@ansible-Client02




CentOS VM Provisioning


vagrantfile과 sh, yml 파일 작성 후 "vagrant up" 명령어를 통해 VM들을 provision합니다.


provision이 완료되었다면 "vagrant ssh ansible-master"로 접속하여 ansible-client들에게 ansible을 통한 ping 명령어를 실행합니다.


아래와 같이 정상적으로 ping에 대한 응답이 보여진다면 정상적인 provision이 완료된 것입니다.

 - ansible 실행에 대한 password는 "vagrant" 입니다.


D:\HashiCorp>vagrant up
 
...
 
D:\HashiCorp>vagrant ssh ansible-master
 
Last login: Fri Aug 17 17:22:34 2018 from 10.0.2.2
 
[vagrant@ansible-master ~]$ ansible centos -m ping -k
 
SSH password:
ansible-client02 | SUCCESS => {
    "changed"false,
    "ping""pong"
}
ansible-client01 | SUCCESS => {
    "changed"false,
    "ping""pong"
}






profile

Ossian Story

@ossians