2017년 6월 22일 목요일

ATOM기반으로 Ansible개발환경 설정하기

앞서 Eclpise기반으로 yml을 편집하고 런쳐로 실행하는 포스팅을 올렸다.

https://yckwon2nd.blogspot.kr/2017/03/ansible.html

근데 오타 대마왕인 나로써는 코드 코디네이터(자동완성기능같은...)가 꼭 필요한데

Eclipse에서 찾을 수 가 없더라고....

그래서 혹시나 ATOM을 뒤져 봤더니 대박!

그것 뿐 아니라 Syntax checker까지 다 나와있다... 이미 몇 년전 부터  -.-;;

그래서 ATOM을 기반으로 Ansible playbook 개발과 실행을 위한 환경을 구성해 보았다.







ATOM IDE로  ansible 편집

Eclipse에서 Ansible code assistant를 찾아보다가....못 찾았음....없을리가 없을 것 같은데...

혹시나 해서 ATOM에서 보니 이거저거 잔뜩 있길래 ATOM으로 갈아타 봤다.

겁내 가볍고 좋내!!  (Eclips도 필요 없는거 정리하면 가벼워지겠으나...기본 설치시 넘 무거워졌음)

개발자들은 몇 년 전부터 아톰으로 넘어가던데 연장(?)바꾸기 귀찮아서 이클립스만 쓰다가 이번에 아톰 첨 써봄


ATOM버젼 1.18기준 yml 확장자는 자동을 2칸띄기 등을 인식하고 하일라이팅도 해준다.

하지만  지시자나 모듈에 대한 가이드와 구문체크는 제공되지 않는다.
(아래 패키지를 추가 설치하면 지원됨)

메뉴에서
Edit --> Preference 를 선택하면 install 메뉴가 있다.
검색창에 ansible 입력하고 조회하면 이것 저것 나온다.
install 버튼을 누르면 설치됨
혹시 설치후 오류나거나 하믄 정지시키거나 삭제도 여기서함


1. 먼저 기본 에디터설정 
기본 에디터에 yml편집을 쉽게 하기 위해 아래 옵션을 켜주도록 한다.

setting --> Editor --> Show Indent Guide & Show Invisibles 를 선택해준다

들여쓰기 라인을 눈으로 바로 확인할 수 있고

공백과 엔터를 편집기에서 표시해준다.


2. 코드 코디네이터 설치 
autocomplete-ansible package
https://atom.io/packages/autocomplete-ansible

지시자와 모듈리스트, 모듈의 속성에 대한 자동완성 및 샘플을 자동으로 입력하도록 지원한다.
오타로 인한 고생을  줄이려면 꼭 필요한 패키지 되시겠다.

directive 와 module의 자동완성 과
module의 경우 필수항목 및 모든 항목의 셈플을 지원한다.

자동완성은  타이핑시 자동으로 드롭다운되므로 방향키 등으로 선택하면되고
모듈의 속성 셈플을 자동입력 하기 위해서는
module이름을 자동완성으로 입력하거나 손가락이 빠르다면 코드어시스트가 출력되기 전에 모두 타이핑을 한 다음에
바로 이어서 r 또는 a 키를 눌러서 지원 받을 수 있다.
r은 필수 속성항목 샘플 코드가 아래쪽에 추가 되고
a는 모든 속성항목  샘플 코드가 입력된다.

3. 구문오류 체크 설치
Linter-UI-Default 설치 필수
Atom에서 패키지 설치를 하면
의존성이 있어 필요한 intentions, linter, busy-signal을 물어보면
yes 눌러서 같이 설치하도록 한다.
구문분석을 위한 기본 패키지로 아래 패키지를 위하여 먼저 설치해 주어야 한다.

구문을 분석해서 잘못된 부분에 대한 검토를 대신 수행해 준다.
Linter-Ansible-Syntax
https://atom.io/packages/linter-ansible-syntax
기본환경에서는 ansible-playbook --syntax-check 을 별도로 수행해야 하나
이 패키지가 설치되면 구문에 대한 체크를 수행하고 오류위치  및 내용을 IDE에 상에서 확인할 수  있다.
디버깅을 위해  필요하다.


ansilbe-lint 기반으로 구문체크를 해준다.
너무 빡빡하게 검사를 하는지 온통 Warning이 발생하지만 애교로 넘어간다 ㅋㅋ
linter-ansible-linting  
https://atom.io/packages/linter-ansible-linting

이 패키지는 ansible-lint가 설치 되어 있어야 하므로 ATOM과 별도로 설치를 해야 한다.

안깔고 패키지만 올리면 파일 저장할때 마다 에러를 보게 될 것이다. ^^;;

파이선 패키지로 설치 해야하므로 아래와 같이 패키지관리 및 이런저런것을 설치하도록 한다.

나는 PC가 민트리눅스라 우분투명령어로 설치했음. 센트나 레뎃이면 아래 링크를 참고해서 설치

pip 설치 http://sens.tistory.com/443  참고

#sudo apt-get install python-pip python-dev build-essential
#sudo pip install --upgrade pip
#sudo pip install --upgrade virtualenv

ansible-lint 설치  https://github.com/willthames/ansible-lint  참고
#pip2 install ansible-lint


4. playbook  런쳐 설치
script
Atom에서 ansible을 실행시키기위한 패키지(ansible뿐아니라 이것저것 다 돌린다)
https://github.com/rgbkrk/atom-script

script 패키지를 이용해서 playbook을 돌리기 위해서는
shift+Ctrl+b 로 직접 수행할 수 있다.
다만 처음에 실행해보면 host파일을 현 yml위치가 아니라 /etc/ansible/host 에서 찾는등 환경 변수가 정상적이지 않다
이때는 프로파일 적용이 필요하다.
shift+Ctrl+alt+O 를 누르면 프로파일 지정이 가능하다.

Current Working Directory :  실행될 위치 비워두면 된다.
command : ansible-playbook 실행파일의 위치  path에 걸려있으면 안넣어도 되고 linux라면 "/usr/bin/ansible-playbook" 로 넣어주면 된다.
command Arguemnts :  Ansible 수행시 필요한 옵션을 넣는다 대표적으로 hosts 파일 지정을 위해 "-i hosts" 를 추가하면 현재 폴더의 hosts파일을 참고한다.
Program Arguments: APP에 적용할 환경 변수를 넣는다. Ansible에서는 사용할일 없으므로 비워둔다
Environment Variables: OS 환경변수에 넣을 설정을 넣는다. Ansible에서는 넣을 일이 없다.
     예외적으로 필자의 경우 실행시 lang 환경변수가 없어서 ATOM편집기의  패키지에서 에러가 발생하는 문제가 있어서 "export lang=ko_KR.UTF-8" 을 넣어 주었다.
(env에 LANG 은 있는데 lang이 없다... -.-;;)

이후 프로파일을 적용해서 실행하기 위해서는

shift + Ctrl + alt + b 를 누르면 저장한 프로파일을 선택하여 실행할 수 있다.

개인적인 생각으로 프로젝트마다 -u(사용자지정) 등을 지정할 수 도 있겠으나 이건 hosts나 yml에서 지정할 수 있으므로 프로파일은 단순하게 -i 만 지정하고 공통으로 쓰는것이 편하지 않을까 생각된다.


기타로....

yml파일이 아이콘이  밋밋하다면 
Atom file-specific icons for improved visual grepping.
https://github.com/file-icons/atom


Ansible 코드를 암호화 하고 싶다면 
https://atom.io/packages/ansible-vault

Ansible galaxy init로 폴더구조를 생성하고자 한다면
ansible-galaxy
https://atom.io/packages/ansible-galaxy



.이상


추가

 linux mint 사용중 python ansible module이 설치가 안되는 경우가 있다

위에서 모듈을 설치하면 해당 패키지가 ansible module을 필요로 하는데 설치가 안된다.

1. python 을 기본 2.x에서 3.x로 바꾸기


yckwon@yckwon-Desktop ~ $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
yckwon@yckwon-Desktop ~ $ sudo update-alternatives --config python
대체 항목 python에 대해 (/usr/bin/python 제공) 2개 선택이 있습니다.

  선택       경로            우선순� 상태
------------------------------------------------------------
* 0            /usr/bin/python2   2         자동 모드
  1            /usr/bin/python2   2         수동 모드
  2            /usr/bin/python3   1         수동 모드

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in manual mode

yckwon@yckwon-Desktop ~ $ python --version
Python 3.5.2

2. pip3 설치 
yckwon-Desktop ~ # apt-get install python3-pip
패키지 목록을 읽는 중입니다... 완료
의존성 트리를 만드는 중입니다       
상태 정보를 읽는 중입니다... 완료
다음 패키지가 자동으로 설치되었지만 더 이상 필요하지 않습니다:
  gcc-5-base:i386 gradle-5.2 gradle-5.3.1 gradle-5.4 gradle-5.4.1 gradle-5.5 icoutils jq libasn1-8-heimdal:i386
  libasound2:i386 libbsd0:i386 libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386 libdrm-radeon1:i386
  libdrm2:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386 libffi6:i386 libfontconfig1:i386
  libfreetype6:i386 libgd3:i386 libgl1-mesa-dri:i386 libgl1-mesa-glx:i386 libglapi-mesa:i386 libglu1-mesa:i386
  libgmp10:i386 libgnutls30:i386 libgphoto2-6:i386 libgphoto2-port12:i386 libgssapi3-heimdal:i386
  libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386 libhogweed4:i386 libhx509-5-heimdal:i386
  libicu55:i386 libidn11:i386 libjbig0:i386 libjpeg-turbo8:i386 libjpeg8:i386 libkrb5-26-heimdal:i386 liblcms2-2:i386
  libldap-2.4-2:i386 libllvm6.0:i386 libltdl7:i386 libmpg123-0:i386 libnettle6:i386 libonig2 libopenal1:i386
  libp11-kit0:i386 libpciaccess0:i386 libpng12-0:i386 libroken18-heimdal:i386 libsasl2-2:i386 libsasl2-modules-db:i386
  libsensors4:i386 libsqlite3-0:i386 libstdc++6:i386 libtasn1-6:i386 libtiff5:i386 libusb-1.0-0:i386 libvpx3:i386
  libwind0-heimdal:i386 libx11-6:i386 libx11-xcb1:i386 libxau6:i386 libxcb-dri2-0:i386 libxcb-dri3-0:i386
  libxcb-glx0:i386 libxcb-present0:i386 libxcb-sync1:i386 libxcb1:i386 libxdamage1:i386 libxdmcp6:i386 libxext6:i386
  libxfixes3:i386 libxml2:i386 libxpm4:i386 libxshmfence1:i386 libxxf86vm1:i386 ocl-icd-libopencl1:i386 wine1.6
  wine1.6-amd64 wine1.6-i386:i386
Use 'sudo apt autoremove' to remove them.
추천하는 패키지:
  python3-dev python3-setuptools python3-wheel
다음 새 패키지를 설치할 것입니다:
  python3-pip
0개 업그레이드, 1개 새로 설치, 0개 제거 및 12개 업그레이드 안 함.
109 k바이트 아카이브를 받아야 합니다.
이 작업 후 569 k바이트의 디스크 공간을 더 사용하게 됩니다.
받기:1 http://ftp.neowiz.com/ubuntu xenial-updates/universe amd64 python3-pip all 8.1.1-2ubuntu0.4 [109 kB]
내려받기 109 k바이트, 소요시간 0초 (1,812 k바이트/초)
Selecting previously unselected package python3-pip.
(데이터베이스 읽는중 ...현재 1211301개의 파일과 디렉터리가 설치되어 있습니다.)
Preparing to unpack .../python3-pip_8.1.1-2ubuntu0.4_all.deb ...
Unpacking python3-pip (8.1.1-2ubuntu0.4) ...
Processing triggers for man-db (2.7.5-1) ...
python3-pip (8.1.1-2ubuntu0.4) 설정하는 중입니다 ...


업그레이드 
yckwon-Desktop ~ # /usr/bin/python -m pip install --upgrade pip
The directory '/home/yckwon/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/yckwon/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/62/ca/94d32a6516ed197a491d17d46595ce58a83cbb2fca280414e57cd86b84dc/pip-19.2.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 1.0MB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-19.2.1



3. setuptool module 설치
yckwon-Desktop ~ # /usr/bin/python -m pip install setuptools
WARNING: The directory '/home/yckwon/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/yckwon/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/ec/51/f45cea425fd5cb0b0380f5b0f048ebc1da5b417e48d304838c02d6288a1e/setuptools-41.0.1-py2.py3-none-any.whl (575kB)
     |████████████████████████████████| 583kB 1.2MB/s 
Installing collected packages: setuptools
Successfully installed setuptools-41.0.1


4. ansible module 설치 
yckwon-Desktop ~ # /usr/bin/python -m pip install ansible 
WARNING: The directory '/home/yckwon/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/yckwon/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting ansible
  Downloading https://files.pythonhosted.org/packages/1f/eb/bdc25ca4d1918a2fb914de188b28374825f415643c69aa108d557203b901/ansible-2.8.3.tar.gz (14.3MB)
     |████████████████████████████████| 14.3MB 925kB/s 
Collecting jinja2 (from ansible)
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
     |████████████████████████████████| 133kB 26.9MB/s 
Collecting PyYAML (from ansible)
  Downloading https://files.pythonhosted.org/packages/a3/65/837fefac7475963d1eccf4aa684c23b95aa6c1d033a2c5965ccb11e22623/PyYAML-5.1.1.tar.gz (274kB)
     |████████████████████████████████| 276kB 24.9MB/s 
Requirement already satisfied: cryptography in /usr/lib/python3/dist-packages (from ansible) (1.2.3)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/lib/python3/dist-packages (from jinja2->ansible) (0.23)
Installing collected packages: jinja2, PyYAML, ansible
  Running setup.py install for PyYAML ... done
  Running setup.py install for ansible ... done
Successfully installed PyYAML-5.1.1 ansible-2.8.3 jinja2-2.10.1


ATOM을 실행하면 

댓글 없음:

댓글 쓰기

본 블로그의 댓글은 검토후 등록됩니다.