반응형
snowman95
코딩수련장
snowman95
전체 방문자
오늘
어제
  • 분류 전체보기 (229)
    • 앱테크 (3)
    • 옵시디언 (5)
    • 드라마, 영화 (1)
    • 개발자 이야기 (23)
    • 프로젝트 (10)
      • 프로젝트 방법론 (7)
      • 프로젝트 기록 (2)
      • Github (1)
    • 개발 지식 (0)
      • 디자인 패턴 (0)
    • 프론트엔드 개발 (5)
      • 테크트리 (2)
      • React.js (19)
      • ReactNative (2)
      • Next.js (6)
      • GraphQL (6)
      • 패키지 매니저 (2)
      • 라이브러리 (3)
      • 상태관리 라이브러리 (4)
      • Web 지식 (3)
      • HTML CSS (26)
      • Javascript (16)
      • 도구 (Tool) (3)
      • 성능 최적화 (1)
      • 디자인시스템 (0)
    • Python (53)
      • 모음집 (1)
      • 문법 (12)
      • 라이브러리 (15)
      • 알고리즘 (10)
      • 백준 문제풀이 (9)
      • 코딩테스트 (2)
      • 도구 (Tool) (3)
    • C++ (20)
      • 알고리즘 (6)
      • 삼성SW기출 (6)
      • 삼성 A형 (6)
    • 데이터사이언스 (1)
    • 인프라 (9)
      • 하드웨어 지식 (4)
      • Ansible (2)
      • Database (2)
      • 쉘스크립트 (1)
    • 주식 (0)
    • 취업 준비 (4)
      • 취업 이야기 (0)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • 기계식키보드 #nuphy
  • 전공 요약 #운영체제
  • 알고리즘
  • 전공요약
  • 공간복잡도
  • 티스토리챌린지
  • GraphQL
  • 백준
  • nextjs
  • 삼성SDS
  • Next.js #graphql #tailwind.css
  • 면접
  • 오블완
  • 언어
  • 전공 요약 #데이터베이스
  • 나의 해방일지
  • 전공 요약 #네트워크
  • A형
  • C++
  • 삼성SW역량테스트

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
snowman95

코딩수련장

Infra IT 자동화 Tool - 앤서블(Ansible) 문법
인프라/Ansible

Infra IT 자동화 Tool - 앤서블(Ansible) 문법

2021. 5. 25. 20:32
728x90
반응형

 


문법

□ 변수 정의 (vars)

tasks 섹션 전에 vars 섹션으로 변수를 정의

vars:
  hello: Hello
tasks:
  - name: Hello World
    debug: msg=“{{ hello }} Ansible”

 

□ 조건 분기 실행 (when)

tasks에서 모듈명 다음 줄에 when 을 기술하여 모듈의 실행 조건을 정의

vars:
  hello: Hello
tasks:
  - name: Hello World
    debug: msg=“{{ hello }} Ansible”

 

□ 루프 실행 (Loops)

tasks에서 모듈명 다음 줄에 with_변수명 기술하고, module에서 {{ 변수명 }} 로 사용

with_items
with_nested
with_dict
with_indexed_items
with_ini
with_flattened
with_file
...
name: add users
user: name={{ item }} state=present groups=user
with_items:
- open
- naru
- admin


□ Include

- include: test/main.yml

 

□ Ad-hoc Task 실행

$ ansible <host-pattern> [options]

$ ansible 192.168.11.3 -m ping -u root --ask-pass
SSH password:
192.168.23.14 | success >> {
"changed": false,
"ping": "pong"
}

$ ansible web01.opennaru.com -m command -a ‘/sbin
/reboot’ --ask-pass

 

□ 템플릿

  • YAML 파일 뿐만 아니라 모든 파일에서 활용 가능함
  • 일반적으로 파일 확장명을 .j2로 함
    ex) index.php.j2
  • Template task 일 때 jinja2가 적용 가능 (copy task는 적용 안됨)
    → Jinja2 란 Template 엔진임 (웹 프레임워크인 FLASK에 기본내장되어 많이 사용됨)
     {{ ... }} : 변수나 표현식
     {% ... %} : if나 for같은 제어문
     {# ... #} : 주석
    tasks:
      - name: deploy my.cnf
        template: src=my.cnf.j2 dest=/etc/my.cnf
    
    filename: my.cnf.j2
    [mysqld]
    user = {{ mysql_user }}
    port = {{ mysql_port }}
    datadir = /var/lib/mysql
    socket = /var/lib/mysql/mysql.sock
    pid-file = /var/lib/mysqld/mysqld.pid

 

 

□ Playbook 작성 예시

예시1. Apache 설치

  • Playbook : apache_setup.yml 

 

  • Inventory : /etc/ansible/hosts
    [apache] web[01:03].ooo.com 192.168.11.3
  • Intall
    $ ansible-playbook apache_setup.yml

 

  • httpd.conf Templates 환경변수

 

 

반응형
저작자표시 동일조건 (새창열림)

'인프라 > Ansible' 카테고리의 다른 글

Infra IT 자동화 Tool - 앤서블(Ansible) 이란 ?  (1) 2021.05.25
    '인프라/Ansible' 카테고리의 다른 글
    • Infra IT 자동화 Tool - 앤서블(Ansible) 이란 ?
    snowman95
    snowman95
    (17~19) Unity/Unreal Engine 게임 프로그래머 (20~21) System Administrator ___________ (22~) React 웹 프론트앤드 개발자 __________ 깃헙 : https://github.com/snowman95

    티스토리툴바