상세 컨텐츠

본문 제목

클라우드 앤서블_2

클라우드+마이크로서비스

by 김일국 2019. 5. 14. 12:51

본문

지금까지 테스트한 앤서블 history

------ 윈도우 파워쉘 에서 ------

버추얼박스설치: https://download.virtualbox.org/virtualbox/6.0.4/VirtualBox-6.0.4-128413-Win.exe 

버추얼박스확장팩설치: https://download.virtualbox.org/virtualbox/6.0.4/Oracle_VM_VirtualBox_Extension_Pack-6.0.4.vbox-extpack

베어그랜트설치: https://releases.hashicorp.com/vagrant/2.2.4/vagrant_2.2.4_x86_64.msi
cd \
mkdir ansible
cd \ansible (Vagrantfile 파일 복사) Vagrantfile
vagrant plugin install vagrant-hostmanager (베어그랜트 호스트네임 플러그인 설치)
vagrant plugin list (확인)
vagrant box add centos/7 (삭제: vagrant box remove centos/7 --all)
vagrant up
vagrant box list
vagrant ssh vagrant@controller
------ 버추얼박스 ssh 접속 이후(암호: vagrant) ------
  ip a s
  cat /etc/hosts
  ping -c1 host1
  ping -c1 host2
  id
  sudo -i
  ssh vagrant@host1
  exit
  ssh vagrant@host2
  sudo -i
  id
  exit
  ls ~/.ssh
  ssh-keygen
  ls ~/.ssh
  ssh-copy-id vagrant@host1
  ssh-copy-id vagrant@host2
  ssh-copy-id vagrant@controller
  ansible all -m ping
  ansible all -m ping -u vagrant
  ansible all -m command -a 'id'
  ansible all -m command -a 'id' -b
  sudo vi /etc/ansible/hosts
  ansible host1,host2 -m ping
  ansible host* -m ping
  pwd
  ansible all --list-hosts
  ansible host* --list-hosts
  ansible webserver:webapp --list-hosts
  ansible 'webserver:&webapp' --list-hosts
  ansible 'webserver:!webapp' --list-hosts
  ansible-inventory
  ansible-inventory --list
  ansible-inventory --host host1
  ansible-inventory --graph
  ansible-doc -t connection -l
  ansible --version
  pwd
  touch .ansible.cfg
  ansible --version
  mkdir test
  cd test
  touch ansible.cfg
  ansible --version
  cd ..
  ansible --version
  rm .ansible.cfg
  ansible --version
  unset ANSIBLE_CONFIG
  ansible --version
  grep '^[' /etc/ansible/ansible.cfg
  grep '^\[' /etc/ansible/ansible.cfg
  ansible-inventory --graph
  vi ansible.cfg
  vi .ansible.cfg
  ls -al
  vi .ansible.cfg
  ls -al
  rm -rf test
  cat .ansible.cfg
  ansible-config list
  ansible-config list
  ansible-config dump
  ansible-config view
  ansible-doc -l
  ansible-doc -l | wc -l
  ansible --version
  ls /usr/lib/python2.7/site-packages/ansible/modules
  ls /usr/lib/python2.7/site-packages/ansible/modules/system
  cat /usr/lib/python2.7/site-packages/ansible/modules/system/ping.py
  vi inventory
  ansible host1 -m ping
  ansible host1 -m ping -a 'data=abc'
  ansible-doc command
  ansible host1 -m command -a
  ansible host1 -m command -a 'id'
  ansible-doc copy
  echo "hello" > /tmp/hello.txt
  ansible host1 -m copy -a 'src=/tmp/hello.txt dest=/tmp'
  ansible host1 -m command -a 'cat /tmp/hello.txt'
  ansible host1 -m command -a 'ls -l /tmp/hello.txt'
  ansible-doc copy
  ansible host1 -m copy -a 'src=/tmp/hello.txt dest=/tmp mode=666'
  ansible host1 -m command -a 'ls -l /tmp/hello.txt'
  ansible host1 -m copy -a 'src=/tmp/hello.txt dest=/tmp mode=666'
  echo "hello111" > /tmp/hello.txt
  ansible host1 -m copy -a 'src=/tmp/hello.txt dest=/tmp mode=666'
  ansible host1 -m command -a 'cat /tmp/hello.txt'
  ansible host1 -m copy -a 'src=/tmp/hello.txt dest=/tmp mode=666'
  ansible host1 -m fetch -a 'src=/tmp/hello.txt dest=/tmp/'
  cat /tmp/hello111.txt
  ls /tmp/*.txt
  ls -al /tmp/*.txt
  cat /tmp/hello.txt
  ansible host1 -m fetch -a 'contetn="hello world!" dest=/tmp/hello2.txt'
  ansible host1 -m copy -a 'contetn="hello world!" dest=/tmp/hello2.txt'
  ansible host1 -m copy -a 'content="hello world!" dest=/tmp/hello2.txt'
  ansible-doc file
  ansible host1 -m command -a 'ls /tmp/abc'
  ansible host1 -m file -a 'path=/tmp/abc'
  ansible host1 -m file -a 'path=/tmp/abc state=touch'
  ansible host1 -m file -a 'path=/tmp/abc'
  ansible host1 -m file -a 'path=/tmp/abc state=touch'
  ansible host1 -m file -a 'path=/tmp/abc'
  ansible host1 -m file -a 'path=/tmp/abc state=file'
  ansible host1 -m file -a 'path=/tmp/abc state=directory'
  ansible host1 -m file -a 'path=/tmp/abc state=absent'
  ansible host1 -m command -a 'ls /tmp/abc'
  ansible host1 -m command -a 'ls /tmp/'
  ansible host1 -m file -a 'path=/tmp/abc state=directory'
  ansible host1 -m file -a 'path=/tmp/abc state=absent'
  ansible host1 -m yum -a 'name=httpd'
  ansible host1 -m yum -a 'name=httpd' -b
  ansible host1 -m yum -a 'name=httpd state=installed' -b
  ansible host1 -m yum -a 'name=httpd state=absent' -b
  ansible-doc yum
  ansible-doc user
  ansible -m user -a 'name=abc' -b
  ansible host1 -m user -a 'name=abc' -b
  ansible host1 -m command -a 'grep abc /etc/shadow' -b
  ansible host1 -m user -a 'name=abc password=abc' -b
  ansible host1 -m command -a 'grep vagrant /etc/shadow' -b
  ansible host1 -m user -a 'name=abc password=abc state=absant' -b
  ansible host1 -m user -a 'name=abc password=abc state=absent' -b
  ansible host1 -m yum -a 'name=httpd state=installed' -b
  ansible hosts -m service -a 'name=httpd state=started enabled=yes' -b
  ansible host1 -m service -a 'name=httpd state=started enabled=yes' -b
  ansible host1 -m command -a 'systemctl status httpd' -b
  ansible host1 -m service -a 'name=httpd state=stopped enabled=yes' -b
  ansible host1 -m command -a 'systemctl status httpd' -b
  ansible host1 -m command -a 'systemctl status httpd' -b
  cat inventory
  ansible host1 -m ping -a ''
  vi test.yml
  ansible-playbook test.yml
  ansible host1 -m ping -a 'data=abc'
  cat test.yml
  vi test.yml (플레이북파일 내용: yaml문법참조 https://ko.wikipedia.org/wiki/YAML

---
- name: PLAY1
  hosts: host1
  tasks:
    - name: TASK1
      ping:
        data: abc
    - name: TASK2
      command:
        id

- name: PLAY2
  hosts: host2
  tasks:
    - name: TASK1
      ping:


  ansible-playbook test.yml (플레이북 실행)
  ansible host1 -m command -a 'id'
  ansible host1 -m shell -a 'id'
  ansible host1 -m raw -a 'id'
  ansible host1 -a 'id'
  ansible host1 -m command -a 'ps aux | grep journal' -b
  ansible host1 -m shell -a 'ps aux | grep journal' -b
  history

----- 작업초기화 시작

PS C:\ansible> vagrant snapshot save host1 init

PS C:\ansible> vagrant snapshot save host2 init

PS C:\ansible> vagrant snapshot restore host1 init

PS C:\ansible> vagrant snapshot restore host2 init

PS C:\ansible> vagrant halt

... 작업초기화 끝

ansible host1 -m uri -a 'url="http://host1/" status_code=200'

위 Ad-Hoc 을 플레이북으로 저장(status.yml)

---
- name: Checking Status Web
  hosts: host1
  tasks:
    - uri:
        url: "http://host1"
        status_code: 200


아래 ansible-playbook 으로 controller노드에서 host1노드로 웹서버 프로비저닝함.

ansible-playbook web_deploy.yml (아래 yaml파일내용)

---
- name: Deploying Web Service
  hosts: host1
  become: yes
  tasks:
    - name: Installing Web Package
      yum:
        name: httpd
        state: installed
      notify:
        - restart httpd

    - name: Copying Web Configuration File
      copy:
        src: httpd.conf
        dest: /etc/httpd/conf/httpd.conf
      notify:
        - restart httpd

    - name: Copy Web Contents file
      copy:
        src: index.html
        dest: /var/www/html/index.html

    - name: Enable & Start Service
      service:
        name: httpd
        enabled: yes
        state: started

  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted


--- vagrant 호스트1에서 ---

sudo mkdir -p /etc/ansible/facts.d

vi /etc/ansible/facts.d/host1.fact

내용

[web]
web_port=80
web_admin=webadmin@host1

--- controller 노드에서 로컬변수 확인---

ansible host1 -m setup -a "filter=ansible_local"

--- yaml파일 디버그방법 ---

tail -20 web_deploy.yml

vim returncode.yml

내용

ansible-playbook returncode.yml
- hosts: host1
  tasks:
    - command:
        id
      register: comm_result
    - debug:
        var: comm_result.cmd

ansible-playbook returncode.yml


--- 변수 처리 ---

---
- name: Deploying Web Service
  hosts: host1
  become: yes
  vars:
    web:
      pkg: httpd
      svc: httpd
    config:
      src: httpd.conf
      dest: "/etc/httpd/conf/httpd.conf"
    content:
      src: index.html
      dest: "/var/www/html/index.html"

  tasks:
    - name: Installing {{ web.pkg }} Package
      yum:
        name: '{{ web.pkg }}'
        state: installed
      notify:
        - restart '{{ web.svc }}'

    - name: Copying {{ config.src }} Configuration File
      copy:
        src: '{{ config.src }}'
        dest: '{{ config.dest }}'
      notify:
        - restart '{{ web.svc }}'

    - name: Copy Web {{ content.src }} Contents file
      copy:
        src: '{{ content.src }}'
        dest: '{{ content.dest }} '

    - name: Enable & Start Service
      service:
        name: '{{ web.svc }}'
        enabled: yes
        state: started

  handlers:
    - name: restart '{{ web.svc }}'
      service:
        name: '{{ web.svc }}'
        state: restarted

- name: Web Access Test
  hosts: controller
  tasks:
    - uri:
        url: "http://host1"
        status_code: 200

--- 변수를 외부파일로 처리 ---

---
- name: Deploying Web Service
  hosts: host1
  become: yes
  vars_files:
    - web_deploy_vars.yml

  tasks:
    - name: Installing {{ web.pkg }} Package
      yum:
        name: '{{ web.pkg }}'
        state: installed
      notify:
        - restart '{{ web.svc }}'

    - name: Copying {{ config.src }} Configuration File
      copy:
        src: '{{ config.src }}'
        dest: '{{ config.dest }}'
      notify:
        - restart '{{ web.svc }}'

    - name: Copy Web {{ content.src }} Contents file
      copy:
        src: '{{ content.src }}'
        dest: '{{ content.dest }} '

    - name: Enable & Start Service
      service:
        name: '{{ web.svc }}'
        enabled: yes
        state: started

  handlers

...

--- web_deploy_vars.yml 파일내용 ---

---
web:
  pkg: httpd
  svc: httpd
config:
  src: httpd.conf
  dest: "/etc/httpd/conf/httpd.conf"
content:
  src: index.html
  dest: "/var/www/html/index.html"


안시블 template모듈로 http.conf.j2 를 동적으로 포트 부여하기(결과 파일 vagrant.tar )

ansible host1 -m shell -a "head -3 /etc/httpd/conf/httpd.conf"

vagrant.tar
0.07MB
Vagrantfile
0.0MB

관련글 더보기

댓글 영역