Post

Ansible(4)

Ansible(4)

관리 호스트에 파일 배포

  1. 파일 관련 모듈들
    • blockinfile
    • lineinfile
    • copy
    • fetch
    • file
    • sefcontext

파일 복사 및 호스트로 복사

SElinux 파일 컨텍스트 확인 디렉토리

  • /etc/selinux/targeted/contexts/files/*

JINJA2 템플릿을 사용하여 사용자 지정 파일 배포

  1. 정적 파일 배포 ( 변수를 쓰지 않고 )

    • copy module
    • 단순한 파일 복사 == 정적파일
  2. 동적 파일 배포( 변수를 써서 )

    • template module
    • 템플릿 파일 복사
    • ansible 템플릿 파일은 Jinja2 템플릿 시스템을 사용한다.
      • Ansible은 Jinja 구문을 사용하여 플레이북에서 변수를 참조할 수 있다.
      • 이는 /etc/hosts 파일에 많이 쓰임(hosts.j2)
    • 확장자는 .j2이다.
  • Jinja2 구문
    • 주석처리 = {# 주석 처리 #}
    • 조건문, 반복문 == {\% 조건문, 반복문 \%}
      • 조건문 == {\% if \%} {\% endif \%}
      • 반복문 == {\% for i in A \%} {\% endfor \%}
    • 변수 ==
  • 템플릿 적용법
1
2
3
4
- name: Test Template
  template:
    src: templates/vsftpd.conf.j2
    dest: /etc/vsftpd/vsftp.conf
  • 추가적으로 validate를 쓸 수 있다.(문법 점검기)

    서비스 이름체크 방법
    SSHsshd -t -f /etc/ssh/sshd_config
    HTTPhttpd -t -f /etc/httpd/conf/httpd.conf
    DNSnamed-checkconf /etc/named.conf
    named-checkzone example.com example.zone
    SMBtestparm -s /etc/samba/smb.conf
    • for문을 사용하여 반복문 기능을 제공한다.
     변수 정의
    users: [user01,user02,use03]

    출력 결과
    user01
    user02
    user03
      

[참고] inventory 파일 안에 변수 정의하기

  • 호스트 변수 정의 방법
    ansible1.example.com VAR=VALUE
  • 호스트 그룹 변수 정의 방법
    [webservers:vars]
    VAR=VALUE
This post is licensed under CC BY 4.0 by the author.