Cloud/Ansible

Ansible) Ansible 환경 구성해보기 3편

Adım Kim 2022. 9. 21. 20:16
실습 환경 : AWS Console (2022/09), Visual Studio Code
실습 목적 : 여러 모듈을 통해 Control Node에서 Managed Node 관리

[Managed Node 관리 1] 사용자 추가/삭제


User 생성

 

ansible managed -m user -a "name=worker state=present" --become -i ./work-ansible/hosts
# ansible [host패턴] -m user -a "name=[사용자명] state=present" --become -i [hosts 경로]
  • 다음 명령이 정상적으로 적용되면, 각 호스트에 changed : true가 나타난다.

 


User 생성 확인

 

ansible managed -m shell -a "tail -n 2 /etc/passwd" -i ./work-ansible/hosts
# ansible [host패턴] -m shell -a "tail -n 2 /etc/passwd" -i [hosts파일 경로]
  • EC2 instance 기본 유저인 ec2-user 외에 worker가 생성된 것을 확인할 수 있다.

 


User 삭제

 

ansible managed -m user -a "name=worker state=absent" --become -i ./work-ansible/hosts
# ansible [host패턴] -m user -a "name=[사용자명] state=absent" --become -i [hosts파일 경로]

 


User 삭제 확인

 

ansible managed -m shell -a "tail -n 2 /etc/passwd" -i ./work-ansible/hosts
# ansible [host패턴] -m shell -a "tail -n 2 /etc/passwd" -i [hosts파일 경로]
  • 각 managed node에 worker 사용자가 없어진 것을 확인

 


 


 


[Managed Node 관리 2] 아주 간단한 웹 환경 구현


yum 모듈로 httpd 설치

 

ansible host1 -m yum -a "name=httpd state=installed" --become -i ./work-ansible/hosts
# ansible [host패턴] -m yum -a "name=http state=installed" --become -i [hosts파일 경로]

 


service 모듈로 httpd 실행

 

ansible public -m service -a "name=http state=started" --become -i ./work-ansible/hosts
# ansible [host패턴] -m service -a "name=http state=started" --become -i [hosts파일 경로]

 


shell 모듈로 httpd 상태 확인

 

ansible public -m shell -a "systemctl status httpd" -i ./work-ansible/hosts
# ansible [host패턴] -m shell -a "systemctl status httpd" -i [hosts파일 경로]

 


copy 모듈로 index.html 파일 전달

 

ansible public -m copy -a "src=index.html dest=/var/www/html/index.html" --become -i ./work-ansible/hosts
# ansible [host패턴] -m copy -a "src=index.html dest=/var/www/html/index.html" --become -i [hosts파일 경로]

 


페이지 확인

 

  • public 그룹에 지정된 두 Managed node의 퍼블릭 주소로 접속
  • index.html의 화면이 정상적으로 나타나는지 확인