keyscan.stdout_lines: keyscan 명령 실행 결과를 with_items를 이용해 지속적으로 출력함
-> 출력 결과는 다시 ~/.ssh/known_hosts로 입력됨
---
- name: Create authority between server and nodes
hosts: nodes
connection: local
serial: 1
gather_facts: no
tasks:
- name: ssh-keyscan for known_hosts file
command: /usr/bin/ssh-keyscan -t ecdsa {{ ansible_host }}
register: keyscan
- name: input key
lineinfile:
path: ~/.ssh/known_hosts
line: "{{ item }}"
create: yes
with_items:
- "{{ keyscan.stdout_lines }}"
vag_reconf.bat 실행
- known_hosts 자동 등록을 위해 생성한 auto_pass.yml 파일과 수정한 Vagranfile이 제대로 적용되는지 vag_reconf.bat 파일을 실행하여 테스트 진행
- 파일 실행 결과: 모든 가상 머신이 삭제되고 재생성되어 결과 마지막 부분에서 known_hosts를 자동으로 등록한 것을 확인할 수 있음
[ 배치 파일의 LF, CR ]
- 유닉스/리눅스 OS의 경우 유닉스 타입의 LF( Line Feed: 커서를 현재 행의 다음 행으로, 즉 아래로 이동 )를 사용함
- 윈도우 OS의 경우 CR( Carriage Return: 커서를 현재 행의 제일 좌측으로 이동 )과 LF를 동시에 사용함 > 따라서 해당 파일을 실행하려면 CRLF( 커서를 다음 줄, 그 줄의 시작 부분으로 이동) 타입으로 변경하고 저장해야 함
@echo off
setlocal
set /p name=어느 vagrant를 재구성 및 시작하시겠습니까? ^
모두는 그냥 enter를 입력 하세요: || set name=default
if %name%==default (
vagrant halt && vagrant destroy -f && vagrant up
) else (
vagrant halt %name% && vagrant destroy %name% -f && vagrant up %name%
)
PS C:\HashiCorp\udemy-ansible> vagrant ssh ansible-server
Last login: Wed Jun 8 19:11:49 2022 from 10.0.2.2
[vagrant@ansible-server ~]$ ans all -m ping -k
[vagrant@ansible-server ~]$ cat /etc/exports
[vagrant@ansible-server ~]$
[vagrant@ansible-server ~]$ exportfs
exportfs: could not open /var/lib/nfs/.etab.lock for locking: errno 13 (Permission denied)
플레이북 실행 후: 각각의 노드의 /home/vagrant/nfs 경로에 호스트 이름으로 파일 생성
# ansible-node01
PS C:\HashiCorp\udemy-ansible> vagrant ssh ansible-node01
[vagrant@ansible-node01 ~]$ ls
nfs
[vagrant@ansible-node01 ~]$ cd nfs
[vagrant@ansible-node01 nfs]$ ls
[vagrant@ansible-node01 nfs]$ touch $HOSTNAME
[vagrant@ansible-node01 nfs]$ ls
ansible-node01
# ansible-node02
PS C:\HashiCorp\udemy-ansible> vagrant ssh ansible-node02
[vagrant@ansible-node02 ~]$ ls
nfs
[vagrant@ansible-node02 ~]$ cd nfs
[vagrant@ansible-node02 nfs]$ ls
ansible-node01
[vagrant@ansible-node02 nfs]$ touch $HOSTNAME
[vagrant@ansible-node02 nfs]$ ls
ansible-node01 ansible-node02
앤서블 서버의 공유 디렉토리에 마운트 확인: 앤서블 서버의 nfs 공유 디렉토리에서 파일 확인
[vagrant@ansible-server nfs_shared]$ ls
ansible-node01 ansible-node02
# ansible-node01의 timezone
[vagrant@ansible-server ~]$ ssh vagrant@172.30.1.110
vagrant@172.30.1.110's password:
Last login: Wed Jun 8 19:45:46 2022 from 172.30.1.100
[vagrant@ansible-node01 ~]$ date
Wed Jun 8 19:46:59 KST 2022
# ansible-node02의 timezone
[vagrant@ansible-server ~]$ ssh vagrant@172.30.1.120
vagrant@172.30.1.120's password:
Last login: Wed Jun 8 19:45:46 2022 from 172.30.1.100
[vagrant@ansible-node02 ~]$ date
Wed Jun 8 19:57:56 KST 2022
앤서블 서버의 timezone 설정
# timezone 변경 전
[vagrant@ansible-server ~]$ date
Wed Jun 8 10:45:49 UTC 2022
[vagrant@ansible-server ~]$ sudo ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
# timezone 변경 후
[vagrant@ansible-server ~]$ date
Wed Jun 8 19:50:08 KST 2022