Skip to content

Ansible 모듈 요약표 (Cheat Sheet)

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

형식

기본 파일 구조

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

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

작업 형식 (Task Format)

단일 행 형식

- apt: pkg=vim state=present

매핑 형식

- 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: [email protected]

참조: 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 # 유형: directory|file|link|hard|touch|absent

    # 선택적 매개변수:
    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 모듈