Ansible/우아한앤서블

4장 Vagrant로 Ansible 사용하기 - 윈도우

Jen'_' 2021. 4. 29. 17:49
반응형

 

사용할 운영체제 이미지

 

 

C:\HashiCorp>vagrant box add sysnet4admin/Windows2016

윈도우 이미지의 크기는 4.83Gib로 기존에 리눅스 파일에 비해서 10가 크다

그러므로 이미지 다운로드 시간이 많이 걸리므로 미리 이미지를 다운로드 하자

 

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure("2") do |config|
  #================#
  # windows node   #
  #================#
  
  #ansible-node07
  config.vm.define "ansible-node07" do |cfg|
    cfg.vm.box = "sysnet4admin/windows2016"
    cfg.vm.provider "virtualbox" do |vb|
      vb.name = "Ansible-Node07(github_SysNet4Admin)"
	  vb.customize ['modifyvm', :id, '--clipboard', 'bidirectional']
	  vb.gui = false
    end
    cfg.vm.host_name = "ansible-node07"
    cfg.vm.network "public_network", ip: "192.168.1.17"
    cfg.vm.network "forwarded_port", guest: 22, host: 60017, auto_correct: true, id: "ssh"
    cfg.vm.synced_folder "../data", "/vagrant", disabled: true
	cfg.vm.provision "shell", inline: "netsh advfirewall set allprofiles state off"
  end	
	
   
	
  #================#
  # ansible-server #
  #================#
  config.vm.define "ansible-server" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vm.provider "virtualbox" do |vb|
      vb.name = "Ansible-Server(github_SysNet4Admin)"
    end
    cfg.vm.host_name = "ansible-server"
    cfg.vm.network "public_network", ip: "192.168.1.10"
    cfg.vm.network "forwarded_port", guest: 22, host: 60010, auto_correct: true, id: "ssh"
    cfg.vm.synced_folder "../data", "/vagrant", disabled: true 
	cfg.vm.provision "shell", inline: "yum -y install epel-release" 
    cfg.vm.provision "shell", inline: "yum install ansible -y"
	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"  
	cfg.vm.provision "shell", path: "bash_ssh_conf_4_CentOS.sh"
	end
end	

책에서는 centos 노드 3개 ubuntu 노드 3개를 포함하지만

용량이 매우 커지므로 윈도우 노드만 만들겠다

 

 

ansible_env_ready.yml

---
- name: Setup for the Ansible's Environment
  hosts: localhost  # 목적지, ansible-server에서 사용할 거니까 localhost
  gather_facts: no
  
  tasks:
    - name: Add "/etc/ansible/hosts"
      blockinfile:
        path: /etc/ansible/hosts
        block: |
          [Windows]
          192.168.1.17 
              
    - name: Install sshpass for Authentication
      yum:
        name: sshpass
        state: present
    
    
    - name: Create vim env's directories & files
      shell: "{{item}}"
      with_items:
       - "mkdir -p /home/vagrant/.vim/autoload /home/vagrant/.vim/bundle"
       - "touch /home/vagrant/.vimrc"
       - "touch /home/vagrant/.bashrc"
       
    - name: Install vim-enhanced
      yum:
        name: vim-enhanced
        state: present

    - name: Install git
      yum:
        name: git
        state: present

    - name: Download pathogen.vim
      shell: "curl -fLo /home/vagrant/.vim/autoload/pathogen.vim
              https://tpo.pe/pathogen.vim"

    - name: Git clone vim-ansible-yaml
      git:
        repo: https://github.com/chase/vim-ansible-yaml.git
        dest: /home/vagrant/.vim/bundle/vim-ansible-yaml

    - name: Configure vimrc
      lineinfile:
        path: /home/vagrant/.vimrc
        line: "{{item}}" 
      with_items:
       - "set number"
       - "execute pathogen#infect()"
       - "syntax on"
    
    - name: Configure Bashrc
      lineinfile:
        path: /home/vagrant/.bashrc
        line: "{{item}}"
      with_items:
       - "alias ans='ansible'"
       - "alias anp='ansible-playbook'"       

 

리눅스와 같은 절차로 server에서 node로 win_ping을 보내려고 하면 에러가 뜬다!

이유는 리눅스는 통신에 필요한 선제조건이 이미 충족되어 있지만 윈도우는 그렇지 않기 때문이다

 

 

통신을 위한 선제 조건

- ansible-server와 ansible-node 간의 통신을 위해서는 다음과 같은 세가지 조건이 필요하다

1. 프로토콜: WinRM

- 리눅스는 기본값으로 ssh 프로토콜을 사용하지만 윈도우에서는 WinRM 의 사용을 권장한다

- ssh 서버가 기본적으로 없음

2. 프로토콜이 사용하는 포트: 5985

- WinRM의 기본 포트 번호는 5985

3. 사용자: vagrant

- ansible에서 기본으로 사용하는 ssh 프로토콜을 사용할 때는 사용자를 지정할 필요가 없지만 윈도우같이 WinRM을 사용하는 경우에는 해당 사용자를 설정해줘야 한다

 

 

 

ansible_env_ready.yml

- 아래 빨간색 글씨 부분을 추가해준다

---

- name: Setup for the Ansible's Environment

hosts: localhost

gather_facts: no

 

tasks:

- name: Add "/etc/ansible/hosts"

blockinfile:

path: /etc/ansible/hosts

block: |

[Windows]

192.168.1.17 ansible_connection=winrm ansible_user=vagrant ansible_port=5985

 

#### add for windows node ####

- name: Install epel

yum:

name: epel-release

state: present

- name: Install pip

yum:

name: python-pip

state: present

- name: Install pywinrm

pip:

name: pywinrm

state: present

#############################

 

- name: Install sshpass for Authentication

yum:

name: sshpass

state: present

 

 

- name: Create vim env's directories & files

shell: "{{item}}"

with_items:

- "mkdir -p /home/vagrant/.vim/autoload /home/vagrant/.vim/bundle"

- "touch /home/vagrant/.vimrc"

- "touch /home/vagrant/.bashrc"

 

- name: Install vim-enhanced

yum:

name: vim-enhanced

state: present

- name: Install git

yum:

name: git

state: present

- name: Download pathogen.vim

shell: "curl -fLo /home/vagrant/.vim/autoload/pathogen.vim

https://tpo.pe/pathogen.vim"

- name: Git clone vim-ansible-yaml

git:

repo: https://github.com/chase/vim-ansible-yaml.git

dest: /home/vagrant/.vim/bundle/vim-ansible-yaml

- name: Configure vimrc

lineinfile:

path: /home/vagrant/.vimrc

line: "{{item}}"

with_items:

- "set number"

- "execute pathogen#infect()"

- "syntax on"

 

- name: Configure Bashrc

lineinfile:

path: /home/vagrant/.bashrc

line: "{{item}}"

with_items:

- "alias ans='ansible'"

- "alias anp='ansible-playbook'"

 

 

C:\HashiCorp>vagrant provision

 

윈도우 노드와 ping 통신 확인해야 할땐 기존에 ping 모듈이 아닌 win_ping 모듈 사용해야 한다

ping 성공!!

 

 


nginx 서비스 설치 및 실행

[vagrant@ansible-server ~]$ vi nginx_install.yml

---
- name: install nginx on Windows
  hosts: Windows # 목적지, hosts 파일에 Windows 그룹
  gather_facts: no 
  
  tasks:
    - name: create dir
      win_file:
        path: C:\nginx
        state: directory
        
    - name: download nginx
      win_get_url:
        url: http://nginx.org/download/nginx-1.14.0.zip
        dest: C:\nginx\nginx-1.40.0.zip
        
    - name: unzip nginx
      win_unzip:
        src: C:\nginx\nginx-1.40.0.zip
        dest: C:\nginx
        delete_archive: yes

    # win_chocolatey 모듈을 사용해서 nssm을 설치     
    - name: install NSSM
      win_chocolatey: 
        name: nssm
        
    - name: download index.html
      win_get_url: 
        url: https://www.nginx.com
        dest: C:\nginx\nginx-1.14.0\html\index.html
    
    # win_nssm 모듈을 사용해서 nginx.exe를 서비스로 등록한다     
    - name: nginx service on by NSSM
      win_nssm:
        name: nginx
        application: C:\nginx\nginx-1.14.0\nginx.exe      
        state: present

    # win_service 모듈을 사용해서 등록된 서비스를 다시 시작한다     
    - name: restart
      win_service:
        name: nginx
        state: restarted        
  
    

 

모듈 설명

  • win_file: nginx를 저장할 디렉터리 생성
  • win_get_url: nginx 다운로드 페이지에서 nginx-1.14.0.zip 파일을 다운
  • win_unzip: zip파일을 압축해제
  • win_chocolatey: 초코레이티는 centos의 yum 처럼 윈도우에서도 가능하게 해준다. 즉, 초코레이티를 이용하여 패키지의 이름만으로 설치가 가능하다
  • nssm: 실행 파일로만 존재하는 nginx를 서비스로 등록하고 운영 가능하도록 해준다

 

 

 

[vagrant@ansible-server ~]$ anp nginx_install.yml -k

SSH password:

PLAY [install nginx on Windows] ********************************************************************

TASK [create dir] **********************************************************************************

ok: [192.168.1.17]

TASK [download nginx] ******************************************************************************

changed: [192.168.1.17]

TASK [unzip nginx] *********************************************************************************

changed: [192.168.1.17]

TASK [install NSSM] ********************************************************************************

[WARNING]: Chocolatey was missing from this system, so it was installed during this task run.

changed: [192.168.1.17]

TASK [download index.html] *************************************************************************

changed: [192.168.1.17]

TASK [nginx service on by NSSM] ********************************************************************

changed: [192.168.1.17]

TASK [restart] *************************************************************************************

changed: [192.168.1.17]

PLAY RECAP *****************************************************************************************

192.168.1.17 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

 

 

파일 생겼고 웹페이지도 잘 열린다!

 

 

 

 

 

 

반응형