반응형

hosts

  • 경로 : ansible-playbook/inventories/dev/hosts
[webserver]
192.168.56.116

all

  • 경로 : ansible-playbook/inventories/dev/group_vars/all
root_directory: /home/ubuntu
temp_directory: "{{ root_directory }}/temp"
app_directory: "{{ root_directory }}/app"
log_directory: "{{ root_directory }}/log"

webserver

  • 경로 : ansible-playbook/inventories/dev/group_vars/webserver
pcre_version: 8.43
zlib_version: 1.2.11
nginx_version: 1.17.1

nginx_config_file: nginx.conf.j2
nginx_server_name: webserver.example.com

configure.sh.j2

  • 경로 : ansible-playbook/roles/nginx/templates/configure.sh.j2
#!/bin/bash

./configure \
--prefix={{ app_directory }}/nginx-{{ nginx_version }} \
--with-pcre=../pcre-* \
--with-zlib=../zlib-* \
--with-http_ssl_module

nginx.conf.j2

  • 경로 : ansible-playbook/roles/nginx/templates/nginx.conf.j2
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  {{ nginx_server_name }};

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

main.yml

  • 경로 : ansible-playbook/roles/nginx/tasks/main.yml
---

- name: install nginx
  tags: install
  block:
#    - name: install essential packages
#      apt:
#        name: "{{ item }}"
#        state: present
#      with_items:
#        - build-essential
#        - libssl-dev
#        - openssl

    - name: install essential packages
      become: true
      apt:
        name:
          - build-essential
          - libssl-dev
          - openssl
        state: present

    - name: mkdir temp
      file:
        path: "{{ temp_directory }}"
        state: directory

    - name: download pcre
      get_url:
        url: "https://ftp.pcre.org/pub/pcre/pcre-{{ pcre_version }}.tar.gz"
        dest: "{{ temp_directory }}"

    - name: unarchive pcre
      unarchive:
        src: "{{ temp_directory }}/pcre-{{ pcre_version }}.tar.gz"
        dest: "{{ temp_directory }}"
        copy: "no"

    - name: download zlib
      get_url:
        url: "http://zlib.net/zlib-{{ zlib_version }}.tar.gz"
        dest: "{{ temp_directory }}"

    - name: unarchive zlib
      unarchive:
        src: "{{ temp_directory }}/zlib-{{ zlib_version }}.tar.gz"
        dest: "{{ temp_directory }}"
        copy: "no"

    - name: download nginx
      get_url:
        url: "http://nginx.org/download/nginx-{{ nginx_version }}.tar.gz"
        dest: "{{ temp_directory }}"

    - name: unarchive nginx
      unarchive:
        src: "{{ temp_directory }}/nginx-{{ nginx_version }}.tar.gz"
        dest: "{{ temp_directory }}"
        copy: "no"

    - name: remove tar.gz
      shell: "rm -rf *.tar.gz"
      args:
        chdir: "{{ temp_directory }}"
        warn: false

    - name: template configure.sh
      template:
        src: configure.sh.j2
        dest: "{{ temp_directory }}/nginx-{{ nginx_version }}/configure.sh"
        mode: 0700

    - name: run configure.sh
      shell: "./configure.sh"
      args:
        chdir: "{{ temp_directory }}/nginx-{{ nginx_version }}"

    - name: make install nginx
      shell: "make && make install"
      args:
        chdir: "{{ temp_directory }}/nginx-{{ nginx_version }}"

    - name: symbolink link nginx
      file:
        src: "{{ app_directory }}/nginx-{{ nginx_version }}"
        dest: "{{ app_directory }}/nginx"
        state: link

    - name: remove temp
      file:
        path: "{{ temp_directory }}"
        state: absent

- name: stop nginx
  tags: stop
  block:
    - name: stop nginx
      shell: "sudo {{ app_directory }}/nginx/sbin/nginx -s stop"
      ignore_errors: yes

- name: config nginx
  tags: config
  block:
    - name: template config files
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      loop:
        - { src: "{{ nginx_config_file }}", dest: "{{ app_directory }}/nginx/conf/nginx.conf" }

- name: start nginx
  tags: start
  block:
    - name: restart nginx
      shell: "sudo {{ app_directory }}/nginx/sbin/nginx"

nginx.yml

  • 경로 : ansible-playbook/nginx.yml
---

- hosts: webserver
  gather_facts: no
  remote_user: ubuntu
  roles:
    - nginx

실행

  • 경로 : ansible-playbook
ansible-playbook -i inventories/dev nginx.yml --tags "install, stop, config, start"
  • 디버깅 모드로 실행
ANSIBLE_DEBUG=y ansible-playbook -i inventories/dev nginx.yml --tags "install, stop, config, start"
반응형

'Development > Ansible' 카테고리의 다른 글

[Ansible-Playbook] ping  (0) 2019.07.06
[Ansible] 명령어  (0) 2019.05.26
[Ansible] Ansible 서버와 원격 서버 설정  (0) 2019.05.26
[Ansible] 설치하기  (0) 2019.05.26

+ Recent posts