47/120

김건호·2022년 4월 18일
0

apt

ubuntu에서 centos의 yum 대신 사용하는 패키지 관리자

yum과의 차이점

  • httpd 패키지 --> apache2
  • mariadb --> mysql

apt 명령 옵션

  • list - 패키지 목록 출력
  • search - 패키지 찾기
  • show - 패키지 상세정보 출력
  • install - 패키지 설치
  • remove - 패키지 제거
  • autoremove - 사용하지 않는 패키지 자동으로 삭제
  • update - 패키지 설치 가능 목록 업데이트
  • upgrade - 패키지 업그레이드

네트워크 관리

Netplan 도구

/etc/netplan/\*.yaml

vagrant@node3:~$ ls /etc/netplan
50-cloud-init.yaml  50-vagrant.yaml

변경사항 적용

netplan apply

기타

보안

  • RHEL: SELinux
  • Debian: AppArmor

방화벽

  • RHEL: FirewallD
  • Debian: UFW

Kernel(Netfilter) <-- iptables 대신 사용

ansible 반복문

작업(Task)에서 loop, with_, until 반복문 구성
2.5부터 loop 가 추가 되었다. 그전에는 with_<lookup> 사용했음

ansible.builtin.user
-> 2.8부터 모듈 표기가 이원화 되었음

리스트(목록) 반복문

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

list of hashes(dictionary)

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 }}'

neted lists

- name: Give users access to multiple databases
  community.mysql.mysql_user:
    name: "{{ item[0] }}"
    priv: "{{ item[1] }}.*:ALL"
    append_privs: yes
    password: "foo"
  loop: "{{ ['alice', 'bob'] | product(['clientdb', 'employeedb', 'providerdb']) | list }}"

random_choice

- name: with_random_choice
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_random_choice: "{{ my_list }}"

- name: with_random_choice -> loop (No loop is needed here)
  ansible.builtin.debug:
    msg: "{{ my_list|random }}"
  tags: random

ansible 조건문

조건문 사용법 when
playbook test
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"

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

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

ansible 핸들러

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

24/7 서비스 같은 경우 설정이 변할때 마다 재시작하면 문제가 발생

문제를 해결하기 위한 코드

- 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

태스그에서 실패하여 플레이가 끝나더라도 강제로 핸들러를 실행하게 하는 설정

ansible 블록

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

블록의 기능
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

ansible 태그

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

- 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
Ken, 🔽🔽 거노밥 유튜브(house icon) 🔽🔽

0개의 댓글