ENCORE CLOUD ARCHITECTURE TIL 4/8 Ansible 파일관리

신민창·2021년 4월 8일
0

TIL

목록 보기
38/46

Ansible 파일 관리

파일 관리 관련 모듈

file module

다른 파일 관리 모듈들의 기본 동작
파일 자체에 대한 속성 설정
소유권
권한
SELinux 컨텍스트
Timestamp
기본적인 파일 생성/제거 용도로 사용

---
- name: File module test
  hosts: webservers
  gather_facts: no
  tasks:
    - name: file module test - /tmp/test
      file:
        path: /tmp/test
        state: touch                 touch 명령어로 파일 생성
---
- name: File module test
  hosts: webservers
  gather_facts: no
  tasks:
    - name: file module test - /tmp/test
      file:
        path: /tmp/test2
        state: touch
        owner: user
        group: wheel
        mode: 0640
---
- name: File module test
  hosts: webservers
  gather_facts: no
  tasks:
    - name: file module test - /tmp/test
      file:
        path: /tmp/test3
        state: touch
        owner: root
        group: root
        mode: 0600
        setype: default_t

참고: SELinux 컨텍스트 규칙 변경 모듈 - sefcontext
file 모듈의 setype은 chcon 과 같이 파일의 컨텍스트 타입을 직접 지정
규칙을 지정할 경우 sefcontext 모듈 사용
규칙이 지정되어도, 즉시 반영되지는 않음
규칙을 반영하기 위해서는 command, shell 등의 모듈을 사용하여 restorecon 명령을 직접 수행하여야 함

EXAMPLES:

- name: Allow apache to modify files in /srv/git_repos
  sefcontext:
    target: '/srv/git_repos(/.*)?'
    setype: httpd_git_rw_content_t
    state: present

- name: Apply new SELinux file context to filesystem
  command: restorecon -irv /srv/git_repos

파일 복사 모듈: copy, fetch

copy
ansible 이 실행되는 control node의 파일을 managed host에게 복사

# src로 특정 파일을 지정하거나, content로 파일 내용을 작성
---
- name: File module test
  hosts: webservers
  gather_facts: no
  become: true
  tasks:
    - name: copy module test - /etc/hosts
      copy:
        src: /etc/hosts          webservers에 있는 노드의 /etc/hosts에서 파일을 복사해서
        dest: /etc/hosts         /etc/hosts에 복사
---
- name: File module test
  hosts: webservers
  gather_facts: no
  become: true
  tasks:
    - name: copy module test - /etc/motd
      copy:
        dest: /etc/motd
        content: "Hello my Server!!!"

fetch

copy와 반대로 각 managed host에 있는 파일을 control node로 복사

---
- name: File module test
  hosts: webservers
  gather_facts: no
  become: true
  tasks:
    - name: fetch module test
      fetch:
        src: /etc/passwd
        dest: /tmp

lineinfile

#지정한 파일에 입력한 텍스트 라인을 추가
---
- name: File module test
  hosts: all
  gather_facts: no
  become: true
  tasks:
    - name: lineinfile test
      lineinfile:
        path: /etc/hosts
        line: '192.168.100.23    managed3 managed3.example.local'
        state: present

blockinfile

#지정한 파일에 입력한 텍스트 블록을 추가
---
- name: File module test
  hosts: all
  gather_facts: no
  become: true
  tasks:
    - name: blockinfile test
      blockinfile:
        path: /etc/hosts
        block: |
          192.168.100.24   managed4 managed4.example.local
          192.168.100.25   managed5 managed5.example.local
          192.168.100.26   managed6 managed6.example.local
        state: present

stat

파일의 상태 정보 조회 (= 리눅스 명령어 stat)

- name: File module test
  hosts: all
  gather_facts: no
  become: true
  tasks:
    - name: stat module test
      stat:
        path: /tmp/test
      register: result
    - name: print file stat
      debug:
        var: result['stat']['checksum']

template

템플릿: 모든 값이 fix 되어 있지 않고, 일부 항목을 변경할 수 있는 파일
jinja2: template에서 사용하는 유형
{{ 변수명 }} : 변수의 값을 입력하여 템플릿 생성
{# 주석 #} : 템플릿 내에 주석 사용
{% 논리구조 등 %} : jinja2 템플릿의 고급 사용
템플릿에서 사용가능한 변수: ansible 내부 변수, 팩트 변수

사용예: template.j2

$ vi template.j2
System name : {{ ansible_facts.fqdn }}
System OS : {{ ansible_distribution }}
System version : {{ ansible_facts['distribution_version'] }}
System administrator : {{ system_admin }}
---
- name: create motd file
  hosts: all
  become: true
  tasks:
    - name: create motd file using template
      template:
        src: ./template.j2
        dest: /etc/motd
        owner: root
        group: root
        mode: 0644
$ mkdir host_vars
$ cat >> host_vars/managed1.example.local
system_admin: admin1@example.local
$ cat >> host_vars/managed2.example.local
system_admin: admin2@example.local

암호화 기능

Playbook, vars_files 등 평문 형태로 되어 있는 파일들을 보호하기 위한 방법
Secret = Ansible Vault
AES256 방식 암호화 수행

서브커맨드 종류

create : 새로운 암호화 된 파일 생성
view : 암호화 된 파일 보기
edit : 암호화 된 파일 수정
encrypt : 기존 파일을 암호화
decrypt : 암호화 된 파일을 해제
rekey : 암호 키 변경
암호화 된 파일을 ansible-playbook 명령으로 사용할 경우
--ask-vault-pass : 암호 직접 입력
--vault-id @prompt : 암호 직접 입력
--vault-password-file : 파일 형태로 저장되어 있는 암호를 사용

$ ansible-vault create secret_file.yml


---
- name: secret file
  hosts: all
  tasks:
    - name: echo hello
      debug:
        msg: "Hello"
...
$ cat secret_file.yml

$ANSIBLE_VAULT;1.1;AES256
33333939396365323638396263393130343231653766333061326635666463356530393762633864
6235626166316630393563323736353630646232623864370a646239363863356130666438643166
32633732383866666365653864393137303132373834343162613733613865353061613633333230
3961383565656366320a613336393936353366373939333066303063383766653139356535363833
64316139323866363061393861633230626533383666663433393939383563313936653031646333
39376638343436653765393136373532313937313734393766306162396261313433373539363262
63646163666263353566323334313438386639356232336632343363333639653562313562383738
39393663666131316439383631613738316161303539373030313362343132363831373566666265
37343934356666373862376566396366613234616635333636346262386237633339    --> 이런 식으로 암호화됨

$ ansible-vault view secret_file.yml

Vault password:  ------------> 설정한 암호 입력
---
- name: secret file
  hosts: all
  tasks:
    - name: echo hello
      debug:
        msg: "Hello"
...

0개의 댓글