becool

20210806 (금) 과제 본문

ansible

20210806 (금) 과제

gusalstm 2021. 8. 6. 18:35
반응형

##### remind: 풀이에 따른 변경 필요 !! ##### 

 

 

1. 가상호스트 파일구성

 패키지 설치 후

 /usr/share/doc/httpd-2.4.6/vhost.conf 샘플파일을 src 디렉터리로 이동

 ( cp vhost.conf /home/user/work/20210806/ 기본 앤서블 작업디렉터리 )

 해당파일을 배포하고, 편집하는 순서로 진행

 배포 : copy모듈

 편집 : lineinfile 모듈 해당 줄을 찾고, 줄에 맞는 내용으로 수정하는 방식 

특이점 : 8080/tcp 에 대한 방화벽을 오픈해야하므로 firewalld service가 아닌 port로 열어주게 된다.

 

2. 과제2 크론 작업 설정

 `` 백쿼테이션을 사용하게 되면 명령어를 인용해올 수 있다.

 굳이 쉘스크립트 작성 필요없었음. 변수를 불러올것도 없었고...

 crond에서 %기호는 개행의 의미를 갖기 때문에 yaml에서 표현 시, \\ 백슬러쉬를 두개 붙여준다.

 (일반적인 환경에서는 한개이지만 yaml에서는 두개)

 

##### 가상호스트 #####

[user@ansible-server 20210806]$ cat 0810_exam1.yaml
---
- name: Q1
  hosts: all
  become: true

  tasks:
  - name: Install Webserver package
    yum:
      name: httpd
      state: latest
#  - name: Webserver configuration #1
#    lineinfile:
#      path: /etc/httpd/conf.d/{{ ansible_fqdn }}vhost.conf
#      line:
#      - "<VirtualHost *:8080>"
#      - "ServerName {{ ansible_fqdn }}"
#      - 'DocumentRoot "/var/www/{{ ansible_fqdn }}"'
#      create: true
#      owner: "ansible_user"
#      group: "ansible_user"
#      mode: "0766"
#      notify: Start web service
#   changed_when: true
  - name: deploy
    copy:
      src: vhost.conf
      dest: /etc/httpd/conf.d/
  - name: Configure vhost
    lineinfile:
      path: /etc/httpd/conf.d/vhost.conf
      regexp: "{{ item.regexp }}"
      line: "{{ item.line }}"
    loop:
    - { regexp: "^<VirtualHost", line: "<VirtualHost *:8080>" }
    - { regexp: "^    ServerName dummy-host" , line: "    ServerName {{ ansible_fqdn }}" }
    - { regexp: "^    DocumentRoot" , line: "    DocumentRoot /var/www/{{ ansible_fqdn}}" }
    - { regexp: "^    ServerAdmin" , line: "    ServerAdmin admin@localhost" }
    notify: Start web service
    changed_when: true

  handlers:
  - name: Start web service
    service:
      name: httpd
      state: restarted
      enabled: true
    notify: allow web service
  - name: allow web service
    firewalld:
      port: 8080/tcp
      state: enabled
      permanent: true
      immediate: true




##### 크론작업예약 #####
[user@ansible-server 20210806]$ cat 0810_exam2.yaml
---
- name: Q2
  hosts: all
  become: true

  tasks:
  - name: create a backup directory
    file:
      path: /tmp/backup/
      state: directory
  - name: backup web service data
    cron:
      name: backup web service data
      user: root
      job: "tar zcf /tmp/backup/backup_`date +\\%Y\\%m\\%d\\%H\\%M`.tar.gz /var/www/html/*"
      hour: '0'
      minute: '0'
      weekday: '6'
      cron_file: backup_webservice

 

[root@ansible-node01 backup]# tar tvf backup_202108101757.tar.gz
-rw-r--r-- root/root        25 2021-08-06 16:27 var/www/html/index.html

 

728x90
Comments