xpdojo / ansible

Ansible Playbook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ansible Playbook

설치

Ubuntu

sudo apt update
sudo apt install ansible
ls /usr/bin/ansible*
# /usr/bin/ansible             /usr/bin/ansible-galaxy
# /usr/bin/ansible-config      /usr/bin/ansible-inventory
# /usr/bin/ansible-connection  /usr/bin/ansible-playbook
# /usr/bin/ansible-console     /usr/bin/ansible-pull
# /usr/bin/ansible-doc         /usr/bin/ansible-vault

python3 -m pip install --user argcomplete
> ip -br -c a
lo               UNKNOWN        127.0.0.1/8 ::1/128
enp1s0           DOWN
wlo1             UP             192.168.219.105/24 fe80::9a7a:7d69:7088:3522/64
# /etc/ansible/hosts
[local]
192.168.219.105 ansible_connection=local

SSH로 접속하기 위해 SSH 서버 데몬이 필요하다.

sudo apt install openssh-serverd
sudo systemctl status sshd
ansible all -m ping

MacOSX

sudo easy_install pip
python3 -m pip install --user ansible
# .bashrc
PATH=$PATH:/Users/markruler/Library/Python/3.10/bin
source .bashrc

SSH로 접속하기 위해 SSH 서버 데몬이 필요하다.

MacOS SSHD

Playbook

ansible-playbook main.yaml --inventory hosts.ini

Entity-relationship diagram of a ansible playbook

Entity-relationship diagram of a ansible playbook - Ansible: Up & Running

inventory

Ansible 스크립트를 실행할 대상 호스트를 설정한다. 설정한 호스트와 연결하기 위해서는 해당 호스트에 SSH Daemon을 설치 및 실행한다.

roles

vars, files, tasks, handlers 과 같은 Ansible artifacts를 한꺼번에 정의하고 읽는다.

vars

재사용할 수 있는 변수를 지정한다.

tasks

playbook으로 실행할 모듈 작업을 설정한다. role을 사용한다면 이 항목은 필수다.

handlers

Handlers are tasks that only run when notified. handler는 설정된 상황에만 실행하는 task다. 예를 들어, 지정한 태스크가 정상적으로 이루어진 경우에 notify를 통해 수행된다.

files

노드에 배포할 파일들을 가리킨다.

meta

의존하는 다른 role을 선언한다.

templates

Jinja 2 파일을 가리킨다.

play recap

# https://devops.stackexchange.com/questions/14542/
- hosts: localhost
  tasks:
    - name: Task will be SKIPPED
      debug:
        msg: You will never see this message.
      when: false

    - name: Failed command will be IGNORED
      command: /usr/bin/false
      ignore_errors: true

    - name: Failed command will be RESCUED
      block:
        - command: /usr/bin/false
      rescue:
        - debug:
            msg: "{{ ansible_failed_result }}"

Galaxy

공개된 Role을 검색하고 사용할 수 있다.

Role 검색

# 명령어로 검색하는 것은 불편하다.
ansible-galaxy search --galaxy-tags go

Role 설치

ansible-galaxy install mdelapenya.go

참조

  • Tips and tricks
  • Roles
  • 앤서블 철저 입문 - 히로가와 히데토시 외
  • 앤서블 시작과 실행 - 로린 혹스테인, 르네 모저
  • 우아하게 앤서블 - 조훈, 김정민
  • Continuous Delivery with Docker and Jenkins - Rafal Leszko

About

Ansible Playbook

License:GNU General Public License v3.0