Ansible을 활용한 자동화 구성 관리(22.04.18)

박민선·2022년 4월 18일
0

반복문

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#playbooks-loops

작업(Task)에서 loop, with_, until 반복문 구성

리스트(목록) 반복문

loop 대신, 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 }}"

사전 반복문

loop 대신 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 }}'

조건문

https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#basic-conditionals-with-when
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#playbooks-tests
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#playbooks-filters

작업에서 when 키워드 사용, 조건을 정의 test, filter 사용

조건 정의시 {{ }} 중괄호 사용 X

- 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"

조건문에 많이 사용하는 팩트 변수

https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#commonly-used-facts

  • ansible_facts["distribution"]
  • ansible_distribution
- hosts: wp
  tasks:
    - debug:
        msg: "hello CentOS"
      when: ansible_facts["distribution"] == "CentOS"
    - debug:
        msg: "hello Ubuntu"
      when: ansible_facts["distribution"] == "Ubuntu"

핸들러

Idempotent: 멱등

가능하면 멱등성을 만족

모든 모듈, 모듈의 파라미터가 멱등성을 만족 하지는 않음

문제가 있는 코드

- hosts: 192.168.100.11
  become: yes
  vars:
    web_svc_port: "80"
  tasks:
    - yum:
        name: httpd
        state: installed
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen'
        line: 'Listen {{ web_svc_port }}' 
    - service:
        name: httpd
        state: restarted
        enabled: yes

문제를 해결하기 위한 코드

- hosts: 192.168.100.11
  become: yes
  vars:
    web_svc_port: "80"
  tasks:
    - yum:
        name: httpd
        state: installed
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen'
        line: 'Listen {{ web_svc_port }}' 
      register: result
    - service:
        name: httpd
        state: started
        enabled: yes
    - service:
        name: httpd
        state: restarted
        enabled: yes
      when: result is changed

플레이, 작업의 이름

- name: Name Test Playbook
  hosts: 192.168.100.11
  tasks:
    - name: task1
      debug:
        msg: hello world
    - name: task2
      debug:
        msg: hello world
    - name: task3
      debug:
        msg: hello world
    - debug:
        msg: hello world
      name: task4

핸들러

이유? 특정 작업이 변경사항을 발생하는 경우에만 실행하기 위한 작업을 지정

핸들러의 작업은 반드시 이름이 있어야 함

핸들러가 실행되는 순서?

  • 알림을 받은 핸들러 작업만 순서대로 실행
  • 모든 작업(tasks)이 완료되어야 핸들러가 실행
  • 알림을 받은 회수와 상관 없이 한번만 실행

예제)

- name: handler example
  hosts: 192.168.100.11
  
  tasks:
    - name: task1
      file:
        path: /tmp/test1
        state: touch
      notify:
        - handle2
        - handle1
    #- name: error
    #  command: ls -P
    - name: task2
      file:
        path: /tmp/test2
        state: touch
      notify:
        - handle1

  handlers:
    - name: handle1
      debug:
        msg: "handle1"
    - name: handle2
      debug:
        msg: "handle2"
- name: Handler Example
  hosts: 192.168.100.11
  become: yes
  vars:
    web_svc_port: "80"
  
  tasks:
    - name: Install httpd Package
      yum:
        name: httpd
        state: installed
    - name: Reconfigure httpd service port
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen'
        line: 'Listen {{ web_svc_port }}' 
      notify:
        - Restart httpd Service
    - name: Start httpd Service
      service:
        name: httpd
        state: started
        enabled: yes 
  handlers:
    - name: Restart httpd Service
      service:
        name: httpd
        state: restarted
        enabled: yes

핸들러가 실행되지 않고 후속 작업에서 실패 한경우

결론: 핸들러가 실행되지 않음

- name: Flush handlers
  meta: flush_handlers
ansible-playbook test.yaml --force-handlers

강제로 핸들러를 실행하게 하는 설정


블록

블록 = 여러 작업을 묶어 놓은 그룹

블록의 기능
1. 여러 작업에 공통의 키워드를 부여할 수 있음(ex: 조건문)
2. block, rescue, always 블록을 이용해 오류 처리를 할 수 있음

block 블록은 항상 실행
rescueblock 블록의 오류가 있을 때만 실행
always 는 항상 실행

- hosts: 192.168.100.11

  tasks:
    - block:
        - debug:
            msg: hello world
        - command: /usr/bin/false
        - debug:
            msg: hello world2
      ignore_errors: yes

      rescue:
        - debug:
            msg: It's rescue

      always:
        - debug:
            msg: It's Always

태그

작업에 태그를 부여하고, 특정 태그의 작업만 실행할 수 있음

- hosts: 192.168.100.11
  gather_facts: no

  tasks:
    - debug:
        msg: "web server stage"
      tags:
        - stage
    - debug:
        msg: "web server product"
      tags:
        - prod
    - debug:
        msg: "web server all"

all 태그: 모든 작업이 속함
untagged 태그: 태그가 설정되어 있지 않는 작업 속함

ansible-playbook test.yaml --tags=stage,prod

태그 관련 확인

ansible-playbook test.yaml --list-tasks
ansible-playbook test.yaml --list-tags

작업 제어

step

ansible-playbook test.yaml --step

특정 작업 부터 시작

ansible-playbook test.yaml --start-at-task="task3"
ansible-playbook test.yaml --start-at-task="task3" --limit 192.168.100.12
profile
클라우드신생아

0개의 댓글