반응형
Jen'_'
Jen's Space
Email
GitHub
전체 방문자
오늘
어제
  • 분류 전체보기 (98)
    • Ansible (4)
      • 우아한앤서블 (4)
    • Cloud (19)
      • AWS (17)
      • GCP (2)
    • DevOps (40)
      • Docker (6)
      • Kubernetes (26)
      • CICD (6)
      • Terraform (2)
    • Language (13)
      • Python (12)
      • Node.js (1)
    • Linux (10)
    • etc. (7)
    • 개인기록 (1)
    • AEWS (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 피드백 환영합니다 :)

인기 글

태그

  • AWS
  • terraform
  • autoscaling
  • CICD
  • ansible
  • k8s
  • Kubernetes
  • INGRESS
  • EKS
  • 쿠버네티스
  • jenkins
  • ubuntu
  • vagrant
  • 우아하게앤서블
  • karpenter
  • argocd
  • Docker
  • ALB
  • gitops
  • 자격증

최근 댓글

Jen'_'

Jen's Space

5장 Vagrant로 Ansible 사용하기 - 네트워크(nxos)
Ansible/우아한앤서블

5장 Vagrant로 Ansible 사용하기 - 네트워크(nxos)

2021. 5. 2. 21:18
반응형

nx-os

시스코 넥서스 스위치의 nx-os 운영체제는 리눅스 기반으로 작성되어 있기 때문에 앤서블로 통신하고자 하는 기본 조건은 이미 충족되어 있다

 

C:\HashiCorp> vagrant box add sysnet4admin/nxosv_v7

미리 운영체제 이미지 다운로드

 

 

vagrant box(운영체제 이미지) 명령어

  • vagrant box add <box name>
  • vagrant box remove <box name>
  • vagrant box list

 

 

Vgrantfile

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


Vagrant.configure("2") do |config|
  #============#
  #   nx-os    #
  #============#
  config.vm.define "nxos01" do |cfg|
    cfg.vm.box = "sysnet4admin/nxosv_v7"
    cfg.vm.provider "virtualbox" do |vb|
      vb.name = "NxOS01(github_SysNet4Admin)"
    end
    cfg.vm.host_name = "nxos01"
  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: "add_ssh_auth.sh", privileged: false	
	cfg.vm.provision "shell", path: "bash_ssh_conf_4_CentOS.sh"
	#cfg.vm.provision "file", source: "nginx_install.yml", destination: "nginx_install.yml"
	#cfg.vm.provision "file", source: "nfs.yml", destination: "nfs.yml"
	end
end	

 

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: |
          [NXOS]
          192.168.1.251 ansible_user=admin ansible_connection=network_cli ansible_network_os=nxos
          
    
    - name: Generate sshkey
      become: yes
      become_user: vagrant
      shell: "{{ item }}"
      with_items:
        - "ssh-keyscan 192.168.1.251 >> ~/.ssh/known_hosts"
       
    
    - 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 up

 

nxos Xshell 연결하기

네트워크 > 기본 NAT에서 어뎁터에 브리지로 바꿔준다

직렬 포트 > 경로/주소를 복사한다

이게 포트 물리적 주소임!

 

 

nxos는 ssh연결이 안되니 serial 선택

 

아까 복사한 포트 주소 붙여넣기

 

Xshell 연결이 잘된다!

id: admin

pw: vagrant

 

 


넥서스 스위치의 시간을 동기화

nxos01에 IP 넣어주기 

Nexus9000v# conf t
Enter configuration commands, one per line. End with CNTL/Z.

Nexus9000v(config)# int mgmt0
Nexus9000v(config-if)# ip add 192.168.1.251/24
Nexus9000v(config-if)# no shut

 

 

ansible-server에서 네트워크들의 timezone을 일치시키는 playbook 만들기 

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

 

nxos_timezone.yml

---
- name: Setup NXOS timezone with ntp configuration
  hosts: NXOS
  gather_facts: no

  tasks:
    - name: ntp configuration
      nxos_config:

        # 스위치에서 그대로 실행 (config)# ntp server 192.168.1.10 use-vrf management
        lines: 'ntp server 192.168.1.10 use-vrf management'

        # 변경을 감지하여 저장
        save_when: modified

    - name: timezone to UTC 9 0
      nxos_config:
        lines: 'clock timezone UTC 9 0'
        save_when: modified

 

 

nxos_timezone.yml 파일을 이용해서 nxos_config 모듈을 사용하여 lines로 입력한 구문을 실행시켜 주고 
save_when: modified을 사용하여 RAM(running-config)에 저장된 파일을 ROM(startup-config)에 저장시킨다
그러면 네트워크 장비를 껏다켜도 데이터가 날아가지 않는다!

원래 네트워크 장비의 데이터는 RAM에 저장되는데 이는 휘발성 메모리이기 때문에 리부트 하면 데이터가 날아간다 리부트해도 정보를 유지시키 위해선 ROM에 저장해야 한다

RAM 
running-config

ROM 
startup-config

RAM의 내용을 ROM에 저장하는 명령어 
copy r s

 

 

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

 

nxos01

Nexus9000v# sh clock  
10:15:43.831 UTC Thu Apr 29 2021
Time source is NTP
Nexus9000v# sh running-config | include config
!Command: show running-config
!Running configuration last done at: Thu Apr 29 09:58:07 2021
Nexus9000v# sh startup-config | include config
!Command: show startup-config
!Startup config saved at: Fri Jul 24 12:35:55 2020

 

 

running-config와 startup-config에 정보가 저장되어 있는것을 볼 수 있다!

 

 

 

 

반응형
저작자표시 (새창열림)

'Ansible > 우아한앤서블' 카테고리의 다른 글

[Ansible] known_hosts 등록, 전자서명을 위한 authorized_keys 등록  (0) 2021.04.30
4장 Vagrant로 Ansible 사용하기 - 윈도우  (0) 2021.04.29
우아한앤서블 3장 Vagrant로 Ansible 사용하기 - centos/7  (2) 2021.04.29
    'Ansible/우아한앤서블' 카테고리의 다른 글
    • [Ansible] known_hosts 등록, 전자서명을 위한 authorized_keys 등록
    • 4장 Vagrant로 Ansible 사용하기 - 윈도우
    • 우아한앤서블 3장 Vagrant로 Ansible 사용하기 - centos/7
    Jen'_'
    Jen'_'
    DevOps 엔지니어 Jen의 기술 블로그 / 이메일: jenakim47@gmail.com

    티스토리툴바