Notice
Recent Posts
Recent Comments
250x250
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

삽더하기실수

Vagrantfile(node1,2,server) 구성하기 본문

Vagrant

Vagrantfile(node1,2,server) 구성하기

삽수 2023. 8. 26. 16:51
728x90

Vagrantfile

Vagrant_API_Version = "2"
Vagrant.configure(Vagrant_API_Version) do |config|

  config.vm.define "ansible-node01" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vbguest.installer_hooks[:before_install] = ["yum install -y epel-release", "sleep 1"]
    cfg.vbguest.installer_options = { allow_kernel_upgrade: false, enablerepo: true }
    cfg.vm.provider :virtualbox do |vb|
      vb.name = "Ansible-node01(Udemy-Bloter)"
      vb.customize ["modifyvm", :id, "--cpus", 1]
      vb.customize ["modifyvm", :id, "--memory", 512]
    end
    cfg.vm.hostname = "ansible-node01"
    cfg.vm.synced_folder ".", "/vagrant", disabled: true
    cfg.vm.network "public_network", ip: "192.168.1.11"
    cfg.vm.network "forwarded_port", guest: 22, host: 19211, auto_correct: false, id: "ssh"
  end
  
  config.vm.define "ansible-node02" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vbguest.installer_hooks[:before_install] = ["yum install -y epel-release", "sleep 1"]
    cfg.vbguest.installer_options = { allow_kernel_upgrade: false, enablerepo: true }
    cfg.vm.provider :virtualbox do |vb|
      vb.name = "Ansible-node02(Udemy-Bloter)"
      vb.customize ["modifyvm", :id, "--cpus", 1]
      vb.customize ["modifyvm", :id, "--memory", 512]
    end
    cfg.vm.hostname = "ansible-node02"
    cfg.vm.synced_folder ".", "/vagrant", disabled: true
    cfg.vm.network "public_network", ip: "192.168.1.12"
    cfg.vm.network "forwarded_port", guest: 22, host: 19212, auto_correct: false, id: "ssh"
  end
  
  config.vm.define "ansible-server" do |cfg|
    cfg.vm.box = "centos/7"
    cfg.vbguest.installer_hooks[:before_install] = ["yum install -y epel-release", "sleep 1"]
    cfg.vbguest.installer_options = { allow_kernel_upgrade: false, enablerepo: true }
    cfg.vm.provider :virtualbox do |vb|
      vb.name = "Ansible-Server(Udemy-Bloter)"
      vb.customize ["modifyvm", :id, "--cpus", 1]
      vb.customize ["modifyvm", :id, "--memory", 512]
    end
    cfg.vm.hostname = "ansible-server"
    cfg.vm.synced_folder ".", "/vagrant", disabled: true
    cfg.vm.network "public_network", ip: "192.168.1.10"
    cfg.vm.provision "shell", path: "bootstrap.sh"
    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: "add_ssh_auth.sh", privileged: false
  end
end

 

bootstrap.sh

#! /usr/bin/env bash

#ansible 설치
yum install epel-release -y
yum install ansible -y

#환경설정 초기 파일 구성 for vagrant only
mkdir -p /home/vagrant/.vim/autoload /home/vagrant/.vim/bundle
touch /home/vagrant/.vimrc
touch /home/vagrant/.bashrc

 

Ansible_env_ready.yml

---
- name: Setup for the Ansible's Environment
  hosts: localhost
  gather_facts: no

  tasks:
    - name: Add "/etc/hosts"
      blockinfile: |
        dest=/etc/hosts
        content="
          192.168.1.10 server
          192.168.1.11 node01
          192.168.1.12 node02"
          
    - name : Add "/etc/ansible/hosts"
      blockinfile: |
        dest=/etc/ansible/hosts
        content="
          [CentOs]
            node01
            node02"
            
    - name: Install sshpass for Authentication 
      yum:
        name: sshpass
        state: present
        
    - 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-yaml1
      git:
        repo: 'https://github.com/chase/vim-ansible-yaml.git'
        dest: /home/vagrant/.vim/bundle/vim-ansible-yaml

    - name: Configure vimrc
      lineinfile:
        dest: /home/vagrant/.vimrc
        line: "{{ item }}"
      with_items:
        - "set number"
        - "execute pathogen#infect()"

 

add_ssh_auth.sh

#! /usr/bin/evb bash

#ssh key 생성
sshpass -p vagrant ssh -T -o StrictHostKeyChecking=no vagrant@node01
sshpass -p vagrant ssh -T -o StrictHostKeyChecking=no vagrant@node02

 

CMD

cd "vagrant 파일 위치"
vagrant up

#vagrant 확인 명령어
vagrant ssh ansible-server
ping node1
ping node2

 

vagarnt를 통해서 ansible-server, node1, node2를 만들었으며, ansible-server에 host값을 입력하여 node들과 통신이 가능하도록 하였다.

 

 

 

728x90