Ansible Playbook
上一個章節介紹給大家ansible單行指令使用方式, 只是一個入門介紹而已哦.
如果你認為單行指令沒什麼, 就是一個for loop可以做到的事, 那playbook就至少可以做到 10 個 for loop的程度哦~
喂~ 這樣沒特別厲害吧? 沒有啦, playbook 主要厲害在於好閱讀, 而且可以更靈活的運用各種模塊的功能哦!
Playbook 長什麼樣, 怎麼用?
直接看範例囉~
這是我的hosts

然後編寫一個playbook

執行吧!

是不是很好懂阿~~~ 這樣就做了四件事情
主角:LAB_PROXY10
- Ping
- 從yum安裝ntp套件
- 從資源服務器下載預先放置的配置文件, 到指定的目錄
- 啟動ntp服務並且enable
更實際一點的應用?
各位運維的兄弟們, 部屬新機器的時候肯定是很多套件要裝, 很多文件要配置. 雖然說不難, 但就是麻煩,瑣碎.
只要用playbook, 部屬新機器, 甚至新環境, 只要先把思路理清楚就是一個文件的配置活.
業務場景:作業系統 Centos7 ; 一台Web, 一台Proxy
必須掛載額外硬碟
兩台都要安裝好各式運維工具 (htop, ntp, snmp, iftop, etc..)
並且也要做好運維配置 ( ntp.conf, snmpd.conf )
WEB 安裝Nginx, 必須從源安裝, 並且配置好啟動腳本
Proxy 安裝Haproxy, 必須配置代理到 Web
思路如下
主角: LAB 全員
做的事情
- 創建慣用資料夾
- 切割磁區
- 格式化成ext4
- 掛載硬碟
- 清除iptables
- 安裝運維常用工具
- 關閉SELinux
- 下載snmpd配置文件
- 下載ntp配置文件
- 重新啟動snmpd
- 重新啟動ntp
主角: LAB_WEB
做的事情
- 下載指定版本Nginx和必要依賴
- 配置編譯文件
- 編譯安裝Nginx
- 下載Nginx啟動文件
- 重新啟動Nginx服務
主角: LAB_PROXY
做的事情
- 安裝 Haproxy
- 下載Haproxy配置文件
- 將配置文件替換成 LAB_WEB1 IP
- 重新啟動Haproxy服務
來配置吧
hosts 配置如下
[LAB]
LAB_WEB1
LAB_PROXY10
[LAB_WEB]
LAB_WEB1
[LAB_PROXY]
LAB_PROXY10
[LAB:vars]
project_name = LAB
external_disk = sdc
app_root = /data
asset_server = "https://asset.tomme.me/ansible-lab"
labweb1ip = 192.168.40.43
labproxy10ip = 192.168.40.47
[LAB_WEB:vars]
nginx_version = nginx-1.13.1
pcre_version = pcre-8.40
zlib_version = zlib-1.2.11
openssl_version = openssl-1.1.0f
[LAB_PROXY:vars]
haproxy_version = haproxy-1.5.18
lab_setup.yaml 配置如下
######
# 全局基礎配置
######
- hosts:
  - LAB
  tasks:
    # 創建慣用資料夾
    - name: Create directory
      file:
        path: "{{ app_root }}"
        state: directory
    # 切割磁區
    - name: Partition disk /dev/{{ external_disk }}
      parted:
        device: /dev/{{ external_disk }}
        number: 1
        state: present
    # 格式化成ext4
    - name: Create a ext4 filesystem on /dev/{{ external_disk }}1
      filesystem:
        fstype: ext4
        dev: /dev/{{ external_disk }}1
        
    # 掛載硬碟
    - name: Mount /dev/{{external_disk}}1 on /data
      mount:
        path: "{{ app_root }}"
        src: /dev/{{ external_disk }}1
        fstype: ext4
        state: mounted
    # 清除iptables
    - name: Flush existing firewall rules
      iptables:
        flush: true
    # 安裝運維常用工具
    - name: Install all OPS tools
      yum:
        name: 
          - nc
          - ntp
          - gcc
          - gcc-c++
          - ncdu
          - htop
          - iotop
          - iftop
          - net-snmp
          - net-tools
        state: present
        update_cache: yes
    # 關閉SELinux
    - name: Disable SELinux
      selinux:
        state: disabled
    # 下載snmpd配置文件
    - name: Download snmpd config
      get_url:
        url: "{{ asset_server }}/snmpd.conf"
        dest: /etc/snmp/snmpd.conf
        force: yes
        backup: yes
    # 下載ntp配置文件
    - name: Download snmpd config
      get_url:
        url: "{{ asset_server }}/ntp.conf"
        dest: /etc/ntp.conf
        force: yes
        backup: yes
    # 重新啟動snmpd服務
    - name: Start and enable snmpd
      systemd:
        name: snmpd
        state: restarted
        enabled: True
    # 重新啟動ntp服務
    - name: Start and enable ntp
      systemd:
        name: ntpd
        state: restarted
        enabled: True
#####
# WEB 安裝 Nginx
#####
- hosts:
  - LAB_WEB
  tasks:
    # 下載指定版本Nginx和必要依賴
    - name: Download and unarchive Nginx / Pcre / Zlib / Openssl
      unarchive:
        src: "{{ item }}"
        dest: "{{ app_root }}"
        remote_src: yes
      loop:
        - https://nginx.org/download/{{ nginx_version }}.tar.gz
        - https://ftp.pcre.org/pub/pcre/{{ pcre_version }}.tar.gz
        - https://www.openssl.org/source/{{ openssl_version }}.tar.gz
        - http://www.zlib.net/{{ zlib_version }}.tar.gz
    # 配置編譯文件
    - name: Configuring NGINX source with custom modules
      command: > 
        ./configure 
        --with-pcre={{ app_root }}/{{ pcre_version }} 
        --with-openssl={{ app_root }}/{{ openssl_version }} 
        --with-zlib={{ app_root }}/{{ zlib_version }}
        --pid-path=/run/nginx.pid
        --sbin-path=/usr/sbin/nginx
      args:
        chdir: "{{ app_root }}/{{ nginx_version }}"
    # 編譯安裝Nginx
    - name: Installing NGINX
      shell: make && make install
      args:
        chdir: "{{ app_root }}/{{ nginx_version }}"
        
    # 下載Nginx啟動文件
    - name: Download Nginx systemd script
      get_url:
        url: "{{ asset_server }}/nginx.service"
        dest: /lib/systemd/system/nginx.service
        force: yes
        backup: yes
    # 重新啟動Nginx服務
    - name: Start and enable Nginx
      systemd:
        name: nginx
        state: restarted
        enabled: True
#####
# PROXY 安裝 HAPROXY
#####
- hosts:
  - LAB_PROXY
  tasks:
    # 安裝 Haproxy
    - name: Install Haproxy
      yum:
        name: 
          - "{{ haproxy_version}}"
        state: present
        update_cache: yes
    # 下載Haproxy配置文件
    - name: Download Haproxy config
      get_url:
        url: "{{ asset_server }}/haproxy.cfg"
        dest: /etc/haproxy/haproxy.cfg
        force: yes
        backup: yes
    # 將配置文件替換成 LAB_WEB1 IP
    - name: Fill up web1 ip
      replace:
        path: /etc/haproxy/haproxy.cfg
        regexp: "#==LAB_WEB1==#"
        replace: "LAB_WEB1 {{ labweb1ip }}:80 check"
    # 重新啟動Haproxy服務
    - name: Start and enable Haproxy
      systemd:
        name: haproxy
        state: restarted
        enabled: True
運行結果
Duang Duang Duang, 全部都好啦~


這不就分分鐘的事, 完成以後心裡就是一句話 我要打一百個 !!
尾聲
好啦~有些同學可能兔槽, Haproxy 配置 改成 LAB_WEB1 那邊很不實際阿, 這樣不能動態增加機器不是?
哎唷~之後會在 templates 和 role 再做介紹啦~ 再見啦各位~