Skip to content

Ansible 모듈 치트 시트

Ansible은 강력한 자동화 운영 도구입니다. 이 문서에서는 자주 사용되는 모듈의 사용법을 소개합니다.

형식 (Format)

기본 파일 구조

---
- hosts: production
  remote_user: root
  tasks:
  - ···

모듈은 tasks 아래에 배치하십시오.

작업 형식 (Task Format)

한 줄 형식

- apt: pkg=vim state=present

매핑 형식 (Mapping)

- apt:
    pkg: vim
    state: present

접힌 스칼라 형식 (Folded Scalar)

- apt: >
    pkg=vim
    state=present

위의 형식 중 하나를 사용하여 작업을 정의할 수 있습니다. 짧은 선언에는 한 줄 형식을, 긴 선언에는 매핑 형식을 권장합니다.

모듈

Aptitude

패키지 관리

- apt:
    pkg: nodejs
    state: present # absent | latest
    update_cache: yes
    force: no

Deb 패키지 파일

- apt:
    deb: "https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb"

저장소 관리 (소프트웨어 소스)

- apt_repository:
    repo: "deb https://··· raring main"
    state: present

저장소 키

- apt_key:
    id: AC40B2F7
    url: "http://···"
    state: present

Git 관련

- git:
    repo: git://github.com/
    dest: /srv/checkout
    version: master
    depth: 10
    bare: yes

참고: git 모듈

Git 설정

- git_config:
    name: user.email
    scope: global # local | system
    value: hi@example.com

참고: git_config 모듈

사용자 관리

- user:
    state: present    # 상태: 존재함
    name: git        # 사용자명
    system: yes      # 시스템 사용자
    shell: /bin/sh   # 로그인 셸
    groups: admin    # 소속 그룹
    comment: "Git Version Control"  # 주석

참고: user 모듈

서비스 관리

- service:
    name: nginx      # 서비스 이름
    state: started   # 상태: 시작됨
    enabled: yes     # 부팅 시 자동 시작 여부

참고: service 모듈

Shell 관련

shell 명령

- shell: apt-get install nginx -y

추가 옵션

- shell: echo hello
  args:
    creates: /path/file  # 파일이 존재하면 건너뜀
    removes: /path/file  # 파일이 존재하지 않으면 건너뜀
    chdir: /path        # 실행 전 이 디렉터리로 이동

여러 줄 명령 예시

- shell: |
    echo "hello there"
    echo "multiple lines"

참고: shell 모듈

스크립트 실행

- script: /x/y/script.sh
  args:
    creates: /path/file  # 파일이 존재하면 건너뜀
    removes: /path/file  # 파일이 존재하지 않으면 건너뜀  
    chdir: /path        # 실행 전 이 디렉터리로 이동

참고: script 모듈

파일 작업

파일 관리

- file:
    path: /etc/dir
    state: directory # 유형: 디렉터리|파일|링크|하드링크|touch|삭제

    # 선택적 매개변수:
    owner: bin      # 소유자
    group: wheel    # 그룹
    mode: 0644      # 권한
    recurse: yes    # 재귀적 생성
    force: yes      # 심볼릭 링크 강제 생성

참고: file 모듈

파일 복사

- copy:
    src: /app/config/nginx.conf   # 원본 파일
    dest: /etc/nginx/nginx.conf   # 대상 위치

    # 선택적 매개변수:
    owner: user     # 소유자
    group: user     # 그룹
    mode: 0644      # 권한
    backup: yes     # 백업 여부

참고: copy 모듈

템플릿

- template:
    src: config/redis.j2       # 템플릿 원본 파일
    dest: /etc/redis.conf      # 대상 위치

    # 선택적 매개변수:
    owner: user     # 소유자
    group: user     # 그룹
    mode: 0644      # 권한
    backup: yes     # 백업 여부

참고: template 모듈

로컬 작업

로컬 실행

- name: 로컬에서 작업 실행
  local_action: shell echo hello

디버그 출력

- debug:
    msg: "Hello {{ var }}"

참고: debug 모듈