본문 바로가기

study/Ansible101 1기

Ansible Semaphore

마지막 스터디 공유 시작하겠습니다.

이번에는 오픈 소스 Semaphore에 대해 공부하겠습니다.

세마포어는 Ansible UI를 제공해줍니다.

https://docs.semui.co/

 

Introduction - Semaphore Docs

Semaphore is written in pure Go and available for Windows, macOS and Linux (x64, ARM, ARM64). Semaphore is an open-source project with concise and high-quality code.

docs.semui.co

 

본격적으로 설치부터 시작해 보겠습니다.

# 설치
cd
pwd
wget https://github.com/ansible-semaphore/semaphore/releases/download/v2.9.45/semaphore_2.9.45_linux_amd64.deb
sudo dpkg -i semaphore_2.9.45_linux_amd64.deb

# Setup Semaphore by using the following command
semaphore setup
1. Set up configuration for a MySQL/MariaDB database
2. Set up a path for your playbooks (auto-created)
3. Run database Migrations
4. Set up initial semaphore user & password

What database to use:
   1 - MySQL
   2 - BoltDB
   3 - PostgreSQL
 (default 1): 2

...
db filename (default /home/ubuntu/database.boltdb): 
Playbook path (default /tmp/semaphore): 
Public URL (optional, example: https://example.com/semaphore): 
Enable email alerts? (yes/no) (default no): 
Enable telegram alerts? (yes/no) (default no): 
Enable slack alerts? (yes/no) (default no): yes
Slack Webhook URL: https://hooks.slack.com/services/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
Enable LDAP authentication? (yes/no) (default no): 
Config output directory (default /home/ubuntu/my-ansible):
...

> Username:  gasida   # 각자 자신의 닉네임
> Email: gasida@localhost
> Your name: gasida   # 각자 자신의 닉네임
> Password: qwe123

# ./semaphore server --config /home/ubuntu/config.json
# nohup ./semaphore server --config /home/ubuntu/config.json &
# You can login with admin@localhost or gasida.

# config.json 파일 확인
cat ./config.json | jq

# (터미널 2) Now you can run Semaphore : 태스크 실행 시 로그 확인 용도로 계속 터미널 열어 둠
# nohup semaphore server --config=./config.json &
semaphore service --config=./config.json


# Semaphore will be available via this URL
echo -e "Semaphore Web = http://$(curl -s ipinfo.io/ip):3000"

# CLI 확인
semaphore -h
semaphore version
semaphore user list

# tmp project home
mkdir /tmp/semaphore

 

 

 

공인아이피:3000 으로 접속하면

 

이렇게 접속이 됩니다.

 

본격적으로 사용해 보겠습니다.

웹 로그인 후 Project 생성 : a101-study - Link

 

Projects - Semaphore Docs

This can be useful for managing different teams, infrastructures, environments or applications.

docs.semui.co

- semaphore에서 Project 는 작업 간 분리 환경을 제공

 

프로젝트 생성

 

사전 확인 : 계정 별 로그인 확인

1. ubuntu 계정 ssh key 파일 로그인 확인

#
whoami

#
ls /home/ubuntu/.ssh/
ssh tnode1
ssh tnode2
ssh tnode3

 

ip로 접속확인

ssh 10.10.1.11
ssh 10.10.1.12
ssh 10.10.1.13

 

2. root 계정 암호 입력으로 로그인 확인

#
sudo su -

# 암호는 qwe123
ssh tnode1
ssh tnode2
ssh tnode3

 

Key Store : 암호 저장- Link

  • 용도 : remote hosts, remote Repo, sudo credentials, Ansible vault passwords
  • 종류 : SSH, Login With Password, None, Personal Access Token(?)
    1. SSH : remote hosts, remote Repo에 SSH 접속 시 사용하는 SSH Keys
    2. Login With Password : 로그인 계정에 대한 암호 password 와 토큰 access Token
      • remote hosts 로그인 시 암호
      • remote hosts 에 sudo 사용 시 암호
      • remote Repo over HTTPS 로그인 시 암호
      • Ansible vaults 의 Unlock
    3. None : remote Repo 에 인증이 없는 경우

[실습] 암호 생성

1. ansible-server 의 ubuntu 계정의 SSH 비밀키 복사

cat /home/ubuntu/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAyQn8a/AnStldmeSqbMcs2Ad4qVm7np+LECad0Q7MBFgpwFxkMJKJ
...

 

2. [Key Store] → NEW KEY 후 아래 처럼 생성

ubuntu 계정의 ssh key

 

root 계정의 login password

 

remote Repo 를 위한 None : none-Anonymous

 

 

Repositories : playbooks 와 roles 위치한 곳 - Link

 

Repositories - Semaphore Docs

Set the branch of the Repository, if you are not sure what it should be, it is probably master or main

docs.semui.co

  • 3가지 저장소 지원
    1. 로컬 Git Repo : *git://*
    2. 로컬 파일시스템 : *file://*
    3. 리모트 Git Repo : accessed over HTTPS(*https://*) , or SSH(*ssh://*)
      • 인증 필요 : SSH 사용 시 SSH Key store , 인증 없을 경우 None type Key

실습

1. [Repositories] → NEW REPOSITORY

로컬 파일시스템

 

 

Environment : 인벤토리에 추가 변수 저장하는 곳으로 JSON 포맷으로 작성 - Link

 

Environment - Semaphore Docs

Before you remove an Environment, you must remove all resources tied to it. If you are not sure which resources are being used in an environment, follow steps 1 and 2 below. It will show you which resources are being used, with links to those resources.

docs.semui.co

실습

1. [Environment] → NEW Environment

- Empty : Extra variables에 아래 입력

 

Inventory : playbook가 실행될 호스트 목록, 변수 사용 가능, YAML/JSON/TOML 포맷 - Link

 

Inventory - Semaphore Docs

Before you remove an Inventory, you must remove all resources tied to it. If you are not sure which resources are being used in an environment, follow steps 1 and 2 below. It will show you which resources are being used, with links to those resources.

docs.semui.co

1. [Inventory] → NEW Inventory

- all : sshkey-ubuntu

 

10.10.1.11
10.10.1.12
10.10.1.13

 

env : login-ubuntu

[web]
tnode1
tnode2

[db]
tnode3

[all:children]
web
db

[all:vars]
user=study

 

 

 

Task Templates : 플레이북 실행 정의(템플릿) - Link

 

Task Templates - Semaphore Docs

This type of template should be used to deploy artifacts to the destination servers. Each deploy template is associated with a build template.

docs.semui.co

  • task template 는 3가지 종류 지원 : Task, Build, Deploy
    • Task : Just runs specified playbooks with specified parameters
    • Build : This type of template should be used to create artifacts
      • The start version of the artifact can be specified in a template parameter. Each run increments the artifact version.
      • Semaphore doesn't support artifacts out-of-box, it only provides task versioning.
      • You should implement the artifact creation yourself. Read the article CI/CD to know how to do this.
      https://docs.semui.co/administration-guide/cicd
    • Deploy : This type of template should be used to deploy artifacts to the destination servers.
      • Each deploy template is associated with a build template.
      • This allows you to deploy a specific version of the artifact to the servers.

실습

1. 미리 플레이북 생성

/tmp/semaphore/fact.yml

cat << EOT > /tmp/semaphore/fact.yml
---

- hosts: all

  tasks:
  - name: Print all facts
    ansible.builtin.debug:
      msg: >
        The default IPv4 address of {{ ansible_facts.fqdn }}
        is {{ ansible_facts.default_ipv4.address }}
EOT

 

/tmp/semaphore/user.yml

cat << EOT > /tmp/semaphore/user.yml
---

- hosts: web
  tasks:
  - name: Create User {{ user }}
    ansible.builtin.user:
      name: "{{ user }}"
      state: present
EOT

 

2. [Task Templates] → NEW TEMPLATE

facts : fact.yml , all , local , Empty , Cron(*/5 * * * *) - Cronitor

 

Crontab.guru - The cron schedule expression generator

 

crontab.guru

 

 

users : user.yml , env, local, User-cloudneta

 

 

확인 : facts 태스크 템플릿은 이미 cron에 의해서 태스트가 실행이 되었다!

 

Tasks : Task Template을 통하여 플레이북을 실행 - Link

 

Tasks - Semaphore Docs

When the task is running, or it has finished, you can see the task status and the running log.

docs.semui.co

  • You can create the task from Task Template by clicking the button Run/Build/Deploy for the required template.
  • The Deploy task type allows you to specify a version of the build associated with the task. By default, it is the latest build version.

 

실습

1. facts 는 cron에 의해서 5분 간격으로 자동 실행을 확인 : facts 왼쪽 화살표 클릭하여 펼치기

 

2. fact.yml 파일 수정 후 실행 확인 : msg

/tmp/semaphore/fact.yml

---

- hosts: all

  tasks:
  - name: Print all facts
    ansible.builtin.debug:
      msg: ->
        The default IPv4 address of {{ ansible_facts.fqdn }}
        is {{ ansible_facts.default_ipv4.address }}

 

 

 

실행 시 마다 inventory 파일 생성 확인

 

 

users 실행 : ACTIONS 에 RUN 클릭

 

실행 실패 후 

key-store 수정

 

 

다시 실행

 

생성 확인

 

4. 추가 변수 사용해보기 : 실행 시 파라미터로 변수 넘겨 주기 -e user=aws

템플릿 수정

["-e", "user=aws"]

 

 

 

실행

 

 

추가 학습

팀 : 팀원 계정 추가

- 계정 추가 : 왼쪽 하단 admin 클릭 → Users 클릭

 

사용자 정보 입력 : devops , qwe123

 

 

admin 로그아웃 후 devops 로그인 확인 : admin 권한 유저 정보 확인

 

 

이것으로 ansible 101 1기 스터디를 마치겠습니다

감사합니다.

'study > Ansible101 1기' 카테고리의 다른 글

Ansible101 1기 4주차 첫번째  (0) 2024.02.04
Ansible101 1기 3주차 두번째  (0) 2024.01.28
Ansible101 1기 3주차 첫번째  (0) 2024.01.28
Ansible101 1기 2주차 두번째  (0) 2024.01.14
Ansible101 1기 2주차 첫번째  (0) 2024.01.14