Today I Learn -37

이정빈·2021년 4월 8일
0

클라우드 엔지니어

목록 보기
38/53
post-thumbnail

파일 관리 관련 모듈

file
copy
fetch
lineinfile
blockinfile
stat

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

  • 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
      dest: /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
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 : 파일 형태로 저장되어 있는 암호를 사용


  • name: secret file
    hosts: all
    tasks:
    - name: echo hello
    debug:
    msg: "Hello"
    ...
profile
WAS Engineer, Cloud Engineer(지망)

0개의 댓글