nayoungs
항상 끈기있게
nayoungs
  • 분류 전체보기 (274)
    • Cloud (21)
      • AWS (15)
      • Azure (3)
      • NCP (2)
      • GCP (1)
    • DevOps (68)
      • Docker (16)
      • Kubernetes (50)
      • CICD (2)
    • IaC (25)
      • Ansible (17)
      • Terraform (8)
    • Certification (4)
    • 금융 IT (5)
    • AI (3)
    • Linux (47)
    • 미들웨어 (5)
    • Programming (7)
      • GoLang (3)
      • Spring (4)
    • CS (25)
      • 네트워크 (17)
      • 운영체제 (5)
      • Web (1)
      • 개발 상식 (2)
      • 데이터베이스 (0)
    • Algorithm (59)
      • 프로그래머스 (36)
      • 백준 (18)
      • 알고리즘 정리 (5)
    • ETC (5)

블로그 메뉴

  • 홈
  • 방명록

공지사항

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
nayoungs

항상 끈기있게

IaC/Ansible

[Ansible] 반복문(loop)과 조건문(when)

2022. 4. 18. 23:47
728x90

📌INDEX

  • 반복문(loop)
    • 리스트(목록) 반복문
    • 사전 반복문
    • 프로세스 관점
  • 조건문(when)



✔️ 반복문(loop)

  • 공식 문서
  • 키워드를 사용 : 키워드 문서
  • 반복은 기본적으로 task 레벨에서 진행함 : play level에서는 불가, (loop)키워드가 있는 레벨
  • 작업(Task)에서 loop, 'with_', 'until'키워드로 반복문 구성
  • 2.5버전부터 loop 추가됨: 글쓴이가 사용하는 건 2.9
  • loop 지시어에 리스트가 들어가 있음
    • 반드시 이름이 item이어야함 .
      • for i에서 i와 비슷한 것

 

🔹 리스트(목록) 반복문

loop 대신, with_items, with_list

  • with_items, with_list
  • 리스트를 사용해 다양한 방법으로 활용 가능
- hosts: 192.168.100.11
  gather_facts: no

  tasks:
    - debug:
        msg: "{{ item }}"
      with_items:
        - apple
        - banana
        - carrot
- hosts: 192.168.100.11
  gather_facts: no
  vars:
    fruits:
      - apple
      - banana
      - carrot

  tasks:
    - debug:
        msg: "{{ item }}"
      with_items:
        "{{ fruits }}"
- hosts: 192.168.100.11
  gather_facts: no
  vars:
    fruits:
      - apple
      - banana
      - carrot

  tasks:
    - debug:
        msg: "{{ item }}"
      loop:
        "{{ fruits }}"
  • 결과 : playbook 실행
[vagrant@controller 02_loop]$ ansible-playbook test.yaml -b

PLAY [192.168.100.11] **********************************************************

TASK [debug] *******************************************************************
ok: [192.168.100.11] => (item=apple) => {
    "msg": "apple"
}
ok: [192.168.100.11] => (item=banana) => {
    "msg": "banana"
}
ok: [192.168.100.11] => (item=carrot) => {
    "msg": "carrot"
}

PLAY RECAP *********************************************************************
192.168.100.11             : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

🔹 사전 반복문

loop 대신 with_dict

  • with_dict
  • 리스트가 아닌 딕셔너리 형식의 변수도 활용 가능
  • 딕셔너리는 중괄호 {}로 묶어야한다는 것 주의
- name: Add several users
  user:
    name: "{{ item.name }}"
    state: present
    groups: "{{ item.groups }}"
  loop:
    - name: 'testuser1'
      groups: 'wheel'
    - name: 'testuser2'
      groups: 'root'
    #[ {name: 'testuser1', groups: 'wheel'}, {name: 'testuser2', group: 'root'} ]
- hosts: 192.168.100.11
  gather_facts: no
  vars:
    fruits:
      - name: apple
        count: 2
      - name: banana
        count: 3

  tasks:
    - debug:
        msg: "{{ item.name }} / {{ item.count }}"
      loop:
        '{{ fruits }}'

 

🔹 프로세스 관점

  • (경우1) 명령을 한번에
    • 프로세스를 한번만 실행하면 됨
sudo yum install httpd,php,mariadb-server
  • (경우2) 명령을 반복해서
    • 프로세스를 3번을 실행해야함
sudo yum install httpd
sudo yum install php
sudo yum install mariadb-server
  • 한번 작업하는 것보다 3번 작업하는 것이 시간적으로 오래걸림
  • 즉, 패키지 설치와 같은 형태에서는 반복문을 쓰는 것이 효율적으로 더 떨어짐
  • systemctl과 같은 경우에는 어차피 여러개의 서비스를 동시에 제어할 수 없기 때문에, 반복문을 통해 코드양을 줄이는 것이 더 효율적
  • replace도 반복문을 통해 코드 양을 줄일 수는 있지만, template을 사용하는 것이 좋음



✔️ 조건문(when)

  • basic-conditionals-with-when
  • playbooks-tests
  • playbooks-filters
  • 작업에서 when 키워드 사용, 조건을 정의 test, filter 사용
  • 조건 정의시 {{ }} 중괄호 사용 X
    • even 변수를 참조하는 경우에도
  • 참고) shell 모듈 : 정말 모듈이 없어서 명령어라도 써야할 때
  • skipping : 조건을 만족하지 않아서 skip

 

예시

- hosts: 192.168.100.11
  vars:
    switch: "on"
  tasks:
    - debug:
        msg: "hello switch on"
      when: switch == "on"
    - debug:
        msg: "hello switch off"
      when: switch == "off"

 

조건문에 많이 사용하는 팩트 변수 : commonly-used-facts

  • 팩트 변수 수집
ansible [대상] -m setup
  • ansible_facts["distribution"] == ansible_facts['os_family'] == ansible_distribution
- hosts: wp
  tasks:
    - debug:
        msg: "hello CentOS"
      when: ansible_facts["distribution"] == "CentOS"
    - debug:
        msg: "hello Ubuntu"
      when: ansible_facts["distribution"] == "Ubuntu"

 

 

 

728x90
저작자표시 (새창열림)
    'IaC/Ansible' 카테고리의 다른 글
    • [Ansible] 블록(Block), 태그(Tag)와 작업제어(Step)
    • [Ansible] 핸들러(Handler)
    • [Ansible] Playbook으로 WordPress 구성부터 원복까지
    • [Ansible] Ad-Hoc으로 WordPress 구성부터 원복까지
    nayoungs
    nayoungs
    안되면 될 때까지

    티스토리툴바