ansible은 자기가 알고있는 서버만 관리가 가능하기 때문에 인벤토리 파일에 서버리스트를 설정후 서버 정보를 제공해줘야한다.
인벤토리 파일?
앤서블 컨트롤러 노드 서버가 명령을 전달할 원격 서버들의 목록을 작성해놓는 곳으로 앤서블을 이용하여 작업을 진핼할 서버의 정보와 작업 내용, 변수 정도를 저장해둔다
yml 파일 또는 ini 파일로 사용 가능하다.
먼저 앞서 만든 실행중인 vm서버를 앤서블이 식별할 수 있도록 이름이 필요하므로 testserver라고 지칭하여 인벤토리 파일로 사용해보겠다.
#hosts
testserver ansible_host=127.0.0.1 ansible_port=2222 ansible=vagrant \
ansibel_private_key_file=.vagrant/machines/vmware_desktop/private_key \
ansible_python_interpreter=/usr/bin/python3
\ 역슬래쉬는 다음줄로 넘길수 있다. ansible 2.7버전 부터 가능하다.
$ ansible testserver -i hosts -m ping
ping 모듈을 사용해 아까 hosts파일에 지정해둔 testserver에게 ping을 전달
SUCCESS가 나온다면 성공했다
앤서블은 테스트 서버에 알려주기 위해 hosts파일에 많은 테스크를 입력해야 했지만 ansible.cfg파일을 통해 인벤토리파일에 공통적으로 들어가는 테스크를 줄일수 있다.
$ vi ansible.cfg
#ansible.cfg
[defaults]
inventory = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/vmware_desktop/private_key
host_key_checking = False #ssh 호스트키 확인 비활성화
hosts파일은 전보다 더 단순화된것을 확인할 수 있다.
$ ansible testserver -m command -a uptime
$ ansible testserver -a uptime
command 모듈은 생략이 가능하다.