Ansible을 통해 환경을 자동화합니다!③
개시하다
할머니를 쓰러뜨리기 위해서 이번엔 노드야.제이스를 치워야 하니까 노드.Ansible로 js 설정을 자동화하기 위해 (^^)/!
개요
■Ansible을 통해 환경을 자동화하려면
■① SSH 설정
■② 타겟 노드에 설정하고 내용을 손으로 확인
■플레이북의 특징
■③ 수작업 절차에 따라 플레이북을 만든다.(제어 노드에서 생성됨)
■
■④ 목표 노드에 플레이북을 실행한다.
정리했어요!(^^)/
① SSH 설정까지 요약Ansible을 통해 환경을 자동화합니다!①
플레이북의 특징까지Ansible을 통해 환경을 자동화합니다!② 정리했습니다!
■③ 수작업 절차에 따라 플레이북을 만든다.(제어 노드에서 생성됨)
원하는 환경을 구축하다
Node.js에 포장을 관리하면서 다양한 걸 설치해요.
및
Node.js 스크립트를 수호하려는 소망!
및
Ansible을 통해 환경을 자동화합니다!②
정리를 해봤는데 그걸 바탕으로 한번 만들어보고 싶어요!!(^^)!(전혀 통일은 아니지만!)
이렇게
└── ansible
├── ansible.cfg #Ansibleの色々な設定が書いてあるやつ
├── hosts #Ansibleの色々な接続設定とかが書いてあるやつ
├── inventory #Ansibleで使用する接続サーバーの設定を自分で書いたやつ
├── playbook.yml #Ansibleで実行したいタスク
└── server.js #Ansibleで実行したいファイル
Ansible의 대상 노드의 내용을 inventory 파일에 기술하고 ansible에 기술하도록 지정합니다.cfg 파일은 다음과 같이 변경됩니다.기본값은 inventory=/etc/ansible/hosts이므로 inventory=./inventory로 변경합니다.
ansible.cfg
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts
inventory = ./inventory
略
forever를 설치할 때까지
nvm 노드를 사용합니다.js를 설치하고 싶었지만 1000번 정도 노력했지만 결국 실행하지 못했다. 대신 nodebrew라는 Node가 그 뒤를 이었다.js 버전 관리 도구를 사용했습니다.
playbook.yml
---
- hosts: all
remote_user: suidou
tasks:
- name: download and install nodebrew
shell: curl -L git.io/nodebrew | perl - setup
args:
creates: "{{ ansible_env.HOME }}/.nodebrew"
- name: path to current node
lineinfile:
dest=~/.bashrc
state=present
line="PATH={{ ansible_env.HOME }}/.nodebrew/current/bin:$PATH"
- name: install node安定版
shell: |
nodebrew install-binary stable
nodebrew alias default stable
nodebrew use stable
npm install -g webpack
args:
creates: "{{ ansible_env.HOME }}/.nodebrew/node/stable"
- name: foreverをインストール
shell: |
npm install forever -g
플레이북의 문법이 정확한지 확인하십시오.문제 없으면 플레이북:플레이북.yml로 표시됩니다.$ ansible-playbook playbook.yml --syntax-check
playbook: playbook.yml
dry-run으로 플레이북에 대한 리허설 테스트를 진행하다.
-C 옵션을 사용하여 dry-run playbook을 사용합니다.더 자세한 내용을 확인하려면 -vv를 추가하세요.
$ ansible-playbook playbook.yml -C
PLAY [all] **********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
[WARNING]: Platform linux on host xx.xx.xx.xx is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [xx.xx.xx.xx]
TASK [download and install nodebrew] ********************************************************************************************************************************************************
ok: [xx.xx.xx.xx]
TASK [path to current node] *****************************************************************************************************************************************************************
ok: [xx.xx.xx.xx]
TASK [install node] *************************************************************************************************************************************************************************
changed: [xx.xx.xx.xx]
TASK [foreverをインストールする] *********************************************************************************************************************************************************************
changed: [xx.xx.xx.xx]
PLAY RECAP **********************************************************************************************************************************************************************************
xx.xx.xx.xx : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
오류 메시지가 없고 문제가 없으면 플레이북을 적용합니다.
$ ansible-playbook playbook.yml
PLAY [all] **********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
[WARNING]: Platform linux on host xx.xx.xx.xxis using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [xx.xx.xx.xx]
TASK [download and install nodebrew] ********************************************************************************************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'. If you need to use command because get_url or uri is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [xx.xx.xx.xx]
TASK [path to current node] *****************************************************************************************************************************************************************
changed: [xx.xx.xx.xx]
TASK [install node] *************************************************************************************************************************************************************************
changed: [xx.xx.xx.xx]
TASK [foreverをインストールする] *********************************************************************************************************************************************************************
changed: [xx.xx.xx.xx]
PLAY RECAP **********************************************************************************************************************************************************************************
xx.xx.xx.xx : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Node.forever로 js 스크립트 지키기
준비한 스크립트입니다.↓
server.js
const http = require('http'); #httpモジュールの読み込み
const hostname = '127.0.0.1'; #ポートとホスト名を指定
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200; #処理を記述
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => { #アドレス
console.log(`Server running at http://${hostname}:${port}/`);
});
Node.forever로 js 스크립트를 지키는 플레이북을 만듭니다.playbook2.yml
- hosts: all
remote_user: suidou
tasks:
- name: server.jsをコピーする
copy:
src: ./server.js
dest: /tmp
mode: 0644
- name: server.jsをデーモン化する
shell: |
forever start /tmp/
dry-run으로 플레이북에 대한 리허설 테스트를 진행하다.
$ ansible-playbook playbook2.yml -C
$ ansible-playbook playbook2.yml
PLAY [all] **********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
[WARNING]: Platform linux on host xx.xx.xx.xx is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [xx.xx.xx.xx]
TASK [server.jsをコピーする] **********************************************************************************************************************************************************************
ok: [xx.xx.xx.xx]
TASK [server.jsをデーモン化する] ********************************************************************************************************************************************************************
changed: [xx.xx.xx.xx]
PLAY RECAP **********************************************************************************************************************************************************************************
xx.xx.xx.xx : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
총결산
길어서 계속!
Reference
이 문제에 관하여(Ansible을 통해 환경을 자동화합니다!③), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/suiudou/articles/a09ad273cfdd4b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)