https://holywat2r.tistory.com/163
[Linux] Ansible 실습 - Playbook
https://holywat2r.tistory.com/162 [Linux] Ansible 실습 - AD-HOC와 모듈 사용법 https://docs.ansible.com/ansible/2.9/user_guide/intro_adhoc.html Introduction to ad-hoc commands — Ansible Documentatio..
holywat2r.tistory.com
# 앞선 내용에서 여럿 모듈들을 배워보았다. 이 모듈들을 이용하여 예제를 풀어보자.
예제 1
- 새로운 작업 디렉토리 ansible_example을 생성하고 구성파일과 인벤토리를 생성한다. (study 사용자로 권한상승없이)
mkdir ansible_example
cd ansible_example
vim ansible.cfg
[defaults]
inventory = inventory
remote_user = study
ask_pass = false
host_key_checking = false
[privilege escalation]
# become = true
# become_ask_pass = false
예제 2
- 인벤토리 생성.
- 작업디렉토리에 inventory라는 이름을 만들고 host1, host2는 web그룹 host3, host4는 db그룹으로 설정
vim inventory
[web]
host1
host2
[db]
host3
host4
예제 3
- AD-HOC 명령으로 인벤토리의 모든 호스트 목록을 확인하고 ping 모듈로 연결상태 확인 및 command 모듈로 사용자 정보 (id 명령)을 확인
ansible --list-hosts all # 모든 대상들 목록 확인
ansible -m ping all # 핑을 확인
ansible -m command -a id all # 사용자 정보 확인
예제 4
- file.yml 플레이북을 작성한다 (file 모듈 사용)
- web 그룹에 대해서 /tmp/fileA 를 생성한다.
---
- name: new file create
hosts: web
tasks :
- name: file modules
file:
path:/tmp/fileA
state: touch
mode: 0666
...
gather_facts: no
ansible-playbook file.yml
- WEB 그룹의 host1 과 host2 에 fileA가 생성됨을 볼 수 있다. 그럼 db 그룹의 host3과 host4는 어떨까?
# 생성되지 않음을 볼 수 있다.
예제 5
- fetch.yml 플레이북 작성 (fetch 모듈 사용)
- 3번에서 만든 fileA를 홈 디렉토리로 복사를 하자
vim fetch.yml
---
- name: file copy from remote host
gather_facts: no
hosts: web
tasks :
- name: fetch modules
fetch:
src:/tmp/fileA
dest: /root
...
ansible-playbook fetch.yml
예제 6
- copy.yml 플레이북 작성 (Copy 모듈 사용)
- 4번에서 복사해온 파일을 db그룹의 사용자 홈 디렉토리에 복사하기
vim copy.yml
---
- name: file copy to remote host
gather_facts: no
hosts: db
tasks:
- name: copy modules
copy:
src:/root/host1/tmp/fileA
dest: /home/study
...
예제 7
- user.yml 플레이북 작성(user 모듈 사용)
- 권한 상승을 설정하고 모든 호스트에 ansible이라는 이름의 사용자를 추가
vim user.yml
---
- name: create new user
hosts: all
become: true
tasks:
- name: user modules
user:
name: ansible
state: present
- 아래의 명령어로 ansible user가 잘 설치되었는지 확인해보자.
ansible -m command -a "id ansible" all
예제 8
- sudo.yml 플레이북 작성 (lineinfile 모듈 사용)
- 각 호스트에 ansible 사용자가 패스워드 없이 sudo 명령어가 사용가능하도로 설정
vim lineinfile.yml
---
- name: add to new sudo user
gather_facts: no
become: true
hosts: all
tasks:
- name: line modules
lineinfile:
line: "ansible ALL=(ALL) NOPASSWD: ALL"
path: /etc/sudoers.d/ansible
create: yes
'DevOps > Ansible' 카테고리의 다른 글
[Ansible] Facts란 (0) | 2021.09.10 |
---|---|
[Ansible] Ansible 실습 - Playbook에 변수사용하기 (0) | 2021.09.10 |
[Ansible] Ansible 실습 - Playbook (0) | 2021.09.09 |
[Ansible] Ansible 실습 - AD-HOC와 모듈 사용법 (0) | 2021.09.07 |
[Ansible] Ansible 실습 - 구성파일 설정 (0) | 2021.09.07 |