AWS Cloud School

[05.13] Ubuntu 설치, Ubuntu환경에서의 playbook, register, when, ansible로 slack메세지 보내기

채소기 2024. 5. 13. 22:22

Ansible

여러개의 서버를 효율적으로 관리하고, 서버 설정을 자동화하고, 인프라를 프로비저닝하는데 주로 사용되는 오픈소스이다.

 

 

- name: debug playbook
  hosts: ubuntu1
  tasks:
  - name: show facts
    debug:
      var: ansible_facts

 

 

[root@ansible ans]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@211.183.3.250

# 공개키 복사

 

- name: debug playbook

  hosts: ubuntu1

  tasks:

  - name: show facts

    debug:

      var: ansible_facts

 

 

 

- name: debug playbook

  hosts: ubuntu1

  gather_facts: true

  tasks:

  - name: show distribution

    debug:

      var: ansible_distribution

 

 

 

실습) 위의 yaml 파일에 내용을 추가하여 배포판에 따라 적절하게 웹서버를 동작시켜보시고, 결과값도 출력해보세요

 

- name: setup for webserver
  hosts: web
  gather_facts: true
  become: true
  tasks:
    - name: httpd install
      yum:
        name: httpd
        state: present
      when: ansible_distribution == 'CentOS'
      register: result
    - debug:
        var: result
      when: ansible_distribution == 'Ubuntu'
    - name: httpd start
      service:
        name: httpd
        state: started
        enabled: yes
      when: ansible_distribution == 'CentOS'
      register: result
    - debug:
        var: result
      when: ansible_distribution == 'Ubuntu'
    - name: nginx install
      apt:
        name: nginx
        state: present
      when: ansible_distribution == 'Ubuntu'
      register: result
    - debug:
        var: result
      when: ansible_distribution == 'Ubuntu'
    - name: nginx start
      service:
        name: nginx
        state: started
        enabled: yes
      when: ansible_distribution == 'Ubuntu'
      register: result
    - debug:
        var: result
      when: ansible_distribution == 'Ubuntu'



실습2) 하나의 플레이북에서 centos면 nfs-client, ubuntu면 nfs-server로 구성해보세요.

 

- name: Setup for NFS
  hosts: two
  gather_facts: true
  become: yes
  tasks:
    - name: Install nfs-utils
      yum:
        name: nfs-utils
        state: present
      when: ansible_distribution == 'CentOS'
    - name: Install nfs-utils
      apt:
        name: nfs-kernel-server
        state: present
      when: ansible_distribution == 'Ubuntu'

    - name: selinux disable
      selinux:
        state: disabled
      when: ansible_distribution == 'CentOS'

    - name: stop firewall
      service:
        name: firewalld
        state: stopped
        enabled: false
      when: ansible_distribution == 'CentOS'

    - name: make nfs_shared directory
      file:
        path: /shared
        state: directory
        mode: 0777
      when: ansible_distribution == 'Ubuntu'

    - name: configure /etc/exports
      lineinfile:
        path: /etc/exports
        line: /shared 211.183.3.*(rw)
      when: ansible_distribution == 'Ubuntu'

    - name: nfs service restart
      become: yes
      service:
        name: nfs-server
        state: restarted
      when: ansible_distribution == 'Ubuntu'

    - name: make nfs_client directory
      file:
        path: /remote
        state: directory
      when: ansible_distribution == 'CentOS'
    - name: mount directory
      become: yes
      mount:
        name: /remote
        src: 211.183.3.250:/shared
        fstype: nfs
        state: mounted
      when: ansible_distribution == 'CentOS'

 

실습3) centos의 경우에는 ssh 접속기록이 /var/log/secure 에 있다

ubuntu의 경우에는 ssh 접속기록이 /var/log/auth.log 에 남는다.

 

앤서블 플레이북을 실행시켰을때 내 매니지드 노드들의 ssh log를 /root/log 디렉토리로 받아오는 플레이북을 만들어보세요. fetch 모듈을 사용하세요.

 

copy 모듈 = 앤서블 제어노드 혹은 앤서블 매니지드 노드내에서 파일을 복사.

fetch 모듈 = 앤서블 매니지드 노드에 존재하는 파일을 제어노드로 가져올 수 있다.

 

- name: send log
  hosts: two
  gather_facts: true
  tasks:
  - name: fetch /var/log/secure
    fetch:
      src: /var/log/secure
      dest: /root/log/cent.log
      flat: yes
    when: ansible_distribution == 'CentOS'
 
  - name: fetch /var/log/auth.log
    fetch:
      src: /var/log/auth.log
      dest: /root/log/ubun.log
      flat: yes
    when: ansible_distribution == 'Ubuntu'

[root@ansible ans]# vi handler.yml



- name: handler test
  gather_facts: true
  tasks:
  - name: install httpd
    yum:
      name: httpd
      state: present
    notify:
    - start_handler

  handlers:
  - name: start_handler
    service:
      name: httpd
      state: restarted
      enabled: yes

 

 

실습3) 새로운 ubuntu 템플릿을 하나 복사해서 211.183.3.252(u2)로 만드세요. u2에서 웹서버가 동작하고 있다. 해당 서버로 파일이 복사 되어 index.html 파일에 변경사항이 발생했을때 웹서버를 재시작 시키는 플레이북을 한번 만들어보세요.

 

[root@ansible ans]# cat copy-handler.yml

- name: play handler
  hosts: u2
  become: yes
  gather_facts: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
    - name: start nginx
      service:
        name: nginx
        state: restarted
        enabled: yes
    - copy:
        src: httpd-index.html
        dest: /var/www/html/index.html
      notify:
        - restart_handler

  handlers:
    - name: restart_handler
      service:
        name: httpd
        state: restarted

 

[root@ansible ans]# echo copy test > httpd-index.html

 

 

 

 

 

 

 

 

[root@ansible ans]# vi slack.yml

# webhook 서버에 메세지를 전달할 플레이북을 만들어보자.

 

- name: slack test playbook
  hosts: u2
  tasks:
  - name: slack test
    slack:
      channel: '#ansible-test'
      token: T031MASNWH5/B073GUZ8ULR/jZ6apeI2grNAfhRvMcPiAGKe
      msg: 'slack webhook test from ansible'