1. hosts 주소 설정 및 확인
관리할 Node 주소 설정
vi /etc/ansible/hosts
211.183.3.145
211.183.3.146
211.183.3.147
- 관리할 Node 의 Ip 만 추가해주면 된다. 이는 기본 값에 해당한다
- 이 파일 대신, 별도의 인벤토리 파일을 만들어 사용하는 것도 가능하다
- 만약, 인벤토리 파일을 지정하지 않고, Section 을 all 로 지정하면, /etc/ansible/hosts 에 등록된 모든 주소의 Node 들에게 명령이 전달된다
현재 상태 확인 - Ping 모듈
Ping 모듈을 이용하여 Node 들에게 명령이 전달되는지 확인하자
[root@ansible-server ~]
- all : 모든 host 들에게 명령. /etc/ansible/hosts 에 입력된 서버들에게 명령
- -m ping : ping 모듈 실행. 이는 일반적인 ICMP 를 보내는 방식이 아닌, 로그인 가능 여부와 파이썬 설치 여부를 묻는다. 정상적인 상태 확인시 PONG 이라고 응답이 온다
server 에서 명령을 실행하는 것이 아닌, node 들에게 node 에서 실행할 명령을 전달해 주는 것이다
[root@ansible-server ~]
211.183.3.145 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
211.183.3.146 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
211.183.3.147 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
- 결과를 보면, 결과가 SUCCESS 인 것을 확인할 수 있다. 변경 사항이 없으므로 changed 는 false 이다. pong 은 응답이다
2. Shell 모듈 & Inventory 파일
Shell 명령 전달하기
[root@ansible-server ~]
- 위와 같이 shell 모듈이 가져와서 shell 명령을 전달 가능하다. 이와 같이 shell 명령을 전달하려면 shell 모듈을 사용해야 한다
[root@ansible-server ~]
211.183.3.146 | CHANGED | rc=0 >>
root
211.183.3.147 | CHANGED | rc=0 >>
root
211.183.3.145 | CHANGED | rc=0 >>
root
- shell 을 이용하면 환경 변수에 접근할 수 있지만, 보안상 결함이 있다
Inventory 파일 사용해보기
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
[root@ansible-server ~]
- lst 파일을 두 개 만들고, IP 를 넣어주었다
[root@ansible-server ~]
211.183.3.145 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
211.183.3.146 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@ansible-server ~]
211.183.3.147 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
- 인벤토리 파일을 지정하여 명령을 보내면, 해당 인벤토리 파일에 적혀진 주소의 Node 들에게만 전달된다
- 인벤토리 파일을 지정하지 않고, Section 을 all 로 지정하면, /etc/ansible/hosts 에 등록된 모든 주소의 Node 들에게 명령이 전달된다
Inventory 파일 Section 구분
[web]
211.183.3.145
[db]
211.183.3.146
- seoul.lst 파일을 위와 같이 수정하자. Section 을 구분하였다
[root@ansible-server ~]
211.183.3.145 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
- 위와 같이 all 이 아닌, 특정 Section 을 지정하면, 해당 인벤토리 파일의 특정 Section 에 해당하는 주소에게만 명령이 전달된다. all 은 인벤토리 파일 내에 모든 주소로 전달해주는 옵션이다
Zone 상관 없이 web 서버들로부터만 응답을 받게 해보자
[web]
211.183.3.147
[root@ansible-server ~]
211.183.3.145 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
211.183.3.147 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
- Section 과 인벤토리 파일들을 지정하면 된다
Zone 상관 없이 web 서버들만 업데이트
ansible web -i seoul.lst -i busan.lst -m shell -a 'yum -y update'
- 위와 같이 shell 모듈을 이용하여 shell 명령을 내리면 된다
Shell 의 단점
211.183.3.147 | CHANGED | rc=0 >>
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.kakao.com
* extras: mirror.kakao.com
* updates: mirror.kakao.com
No packages marked for update
211.183.3.145 | CHANGED | rc=0 >>
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.kakao.com
* extras: mirror.kakao.com
* updates: mirror.kakao.com
No packages marked for update
- 만약 모듈 Shell 을 이용하여 특정 패키지를 설치했고, 다시 한번 동일 명령을 실행하면?
- Shell 모듈을 이용하면 무조건 명령이 실행이 된다. 따라서 Shell 모듈을 이용하여 설치 명령을 내리면 무조건 설치를 진행한다. 물론 설치가 되어 있으므로 결과에서는 " 이미 설치되어 있다 " 가 출력된다. 이는 좋은 것이 아니다
이를 yum 모듈을 이용하면, 이미 설치가 된 상태에서는 다시 한번 설치를 진행하면 skip 된다
- 결국, Shell 은 Ansible 에서 사용하기에 적절한 모듈은 아니다