beaniejoy / dongne-cafe-api

☕️ kotlin & spring boot application (toy project) / siren order service for local cafe
2 stars 1 forks source link

Integrated management of different OS in ansible playbook(기록용) #74

Closed beaniejoy closed 1 year ago

beaniejoy commented 1 year ago

서로 다른 OS server에 대한 ansible playbook 범용 관리

hosts

[ec2]
[ssh_server_ip_addr]    ansible_user=ec2-user   ansible_ssh_private_key_file=/home/ec2-user/.ssh/[ssh_server_private_key]

[rasp_pi]
[domain]    ansible_port=[port_forwarding]      ansible_user=pi     ansible_ssh_private_key_file=/home/ec2-user/.ssh/[ssh_server_private_key]

여러 host 등록

install package

# roles/package/vars/main.yml
java_package:
  Amazon: java-17-amazon-corretto
  Debian: openjdk-17-jre
nginx_package:
  Amazon: nginx
  Debian: nginx

os별 vars 관리

# roles/package/tasks/main.yml
---
- name: Check OS
  ansible.builtin.debug:
    msg: "OS: {{ ansible_distribution }} / OS version: {{ ansible_distribution_version }}"
  tags:
    - always

- name: Install java
  action: >
    {{ ansible_pkg_mgr }} name={{ java_package[ansible_distribution] }} state=present
  tags:
    - init

- name: Install nginx
  action: >
    {{ ansible_pkg_mgr }} name={{ nginx_package[ansible_distribution] }} state=present
  tags:
    - init

ansible_pkg_mgr: os별로 package manager 적용
ansible_distribution: ansible host에 대한 os version check(gather_facts: true에서 가능)

Debian nginx 설정 중 주의사항 기록 (링크)

global vars

# remote server
remote_server_user:
  Amazon: ec2-user
  Debian: pi
remote_server_group:
  Amazon: ec2-user
  Debian: pi
remote_user_workspace: "/home/{{ remote_server_user[ansible_distribution] }}"

...

remote server user, group 둘 다 OS별로 관리