note

Asinble_playbook

ref: https://github.com/istar0me/linux-note/blob/107-2/Ansible.md#ansible-playbooks

ansible playbook

ansible的playbook的格式是使用YAML的格式(很多伺服器檔案都是使用這種格式),下面是一個基本範例

send message to all connector

1

YAML的寫法通常都是value: key

hello.yml: 這個playbook可以傳送給所有ansible機台

---  # 代表文件開始
- hosts: server1 # 應用在哪個主機,-後面記得要空白
  remote_user: root # 遠端使用哪個使用者操作 # 這個可寫可不寫

  tasks:
    - name: hello world # 工作名稱  # 不能使用tab,要用四個空白
      command: /usr/bin/wall hello world # 執行的指令

/usr/bin/wall: 用來傳送文字給所有連線的人

Gathering fact很像是setup模組,用來讀取主機訊息,可以用這個判斷目前是哪個作業系統和其他基本訊息

ansible server1 -m setup | grep -A 3 -i "IP"  # 取得IP位置資訊

撰寫建立httpd伺服器的腳本

echo "Hello" > a.html
vim setup_httpd.yml

set up httpd with ansible

2

setup_httpd.yml: 自動產生網頁的ansible book

---
- hosts: server1
  remote_user: root
  tasks:
    - name: create new file  # 產生新檔案 (測試用)
      file: name=/tmp/newfile state=touch  # 使用file模組產生
    - name: create new user  # 產生使用者 (測試用)
      user: name=test2 system=yes shell=/sbin/nologin  # 生成系統使用者
    - name: install package # 安裝 httpd 套件
      yum: name=httpd
    - name: copy html # 將檔案複製到 app2 的指定資料夾
      copy: src=a.html dest=/var/www/html
    - name: start service # 啟動 httpd 服務
      service: name=httpd state=started

run

ansible-playbook -C setup_httpd.yml

ignore error stdout

3

test.yml: 透過/bin/true,忽視錯誤輸出,並繼續執行

---
- hosts: server1
  remote_user: root
  tasks:
    - name: show date
      command: date
    - name: run a shell script  # 省略前方的 tasks
      shell: /usr/bin/somecommand || /bin/true  # 忽略錯誤
    - name: show hostname
      command: hostname
ansible-playbook -C test.yml

Notify and Handler

針對不同的環境可以貼合,假設有機器已經裝了httpd並啟動伺服器,我們使用把設定檔案給機器並啟動時,就會因為它已經啟動了,所以不做更改,這邊就需要使用Notify and Handler,在完成特定事件後,強制觸發某個程序。

把遠端的httpd的port從80改成8080,使用playbook的方式完成

scp root@centos7-2:/etc/httpd/conf/httpd.conf  # 複製另一台的httpd設定檔到本地,這邊是先做測試
# 修改本地的 /etc/httpd/conf/httpd.conf 改裡面的 Listen到8080
vim /etc/httpd/conf/httpd.conf
Listen 8080

copy config to remote

4

httpd_setport.yml: 拷貝本地端的httpd設定檔,並傳到遠端

---
- hosts: servers
  remote_user: root
  tasks:
  # 編輯 copy html 的 task
    - name: install package # 安裝 httpd 套件
      yum: name=httpd
    - name: copy html # 將檔案複製到 server1 的指定資料夾
      copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf
      notify: restart httpd # 完成這邊就要執行下面的內容
    # 省略 start service 的 task
  handlers: # 明確告訴電腦一定要執行這件事情
    - name: restart httpd # 名字必須要和notify一樣
      service: name=httpd state=started # new line
ansible-playbook httpd_setport.yml

使用netstat -tunlp | grep http ,查看http跑的狀況

3 way to setup param

5

input_param.yml: 輸入指令時裝入參數的寫法

---
- hosts: app2
  remote_user: root
  tasks:
  - name: install new package
    yum: name=  # 這邊放入變數

  handlers:
    - name: restart httpd
      service: name=httpd state=started

-e: 指定environment

ansible-playbook -e pkgname=vsftpd test.yml

custom_param.yml: 自定義變數,變數定義在文件vars裡面

---
- hosts: app2
  remote_user: root
  vars:
  - pkgname1: vsftpd
  - pkgname2: joe
  tasks:
  # 省略前方的 tasks
  - name: install new package
    yum: name=
  - name: install new package
    yum: name=

  handlers:
    - name: restart httpd
      service: name=httpd state=started

在host裡面設定參數,在文件裡面就可以直接使用

vim /etc/ansible/hosts
[server1]
192.168.42.136 

[server2]
192.168.42.138

[servers]
192.168.56.102 pkgname=httpd
192.168.56.104 pkgname=vsftpd

這三種方式的優先順序是: 自定義var>指令輸入>設定在hosts裡面

install muti package

6

安裝多個軟體

- hosts: server1
  vars:
  - pkgname1: vsftpd
  - pkgname2: joe
  tasks:
  - name: install muti package
    yum: 
      name:
        - 
        - 
      state: present

store param in other file

7

安裝文件,變數存在外面

- hosts: server1
  vars_files: ./vars_public.yml
  tasks:
  - name: install  and 
    yum: 
      name:
        - 
        - 
      state: present

vars_public.yml

app1: sshd
app2: httpd

install throught the file

8

透過文件來安裝

- hosts: server1
  tasks:
  - name: install  and 
    yum: 
      name:
        - 
        - 
      state: present

server1對應到group_vars裡面的server1 (group_vars/server1),server1裡面寫變數名稱

show the log to screen

9

顯示變數到螢幕上

- hosts: server1
  tasks:
  - name: install httpd server
    yum: name=httpd state: present
  
  - name: install httpd server
    copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf
    notify: restart httpd 
    
  - name: install httpd server
    service: name=httpd state=start
  
  - name: install httpd server
    shell: ps aux|grep httpd
    register: check_httpd  # 上面輸出的結果存到變數中
   
  - name: install httpd server
    debug:  # 顯示變數到螢幕上
      msg: ""
  
  handlers:
    - name: restart httpd
      service: name=httpd state=started

use template for different server

10

配置伺服器常會用到template,針對不同的伺服器做不同的功能,這邊改檔名為httpd.conf.j2 ,就是專門寫模板,讓目標有不同配置

- hosts: servers
  tasks:
  - name: install httpd server
    yum: name=httpd state: present
  
  - name: configure httpd server
    template: ./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf 
    notify: restart httpd 
    
  - name: start httpd server
    service: name=httpd state=start enable=yes
  
  handlers:
    - name: restart httpd
      service: name=httpd state=started

httpd.conf.j2裡面有一個很重要的變數ansible fqdn,讓不同主機顯示不同名稱

ServerAdmin root@

ansible fqdn在每台主機上會有不同的名稱

ansible servers -m setup | grep ansible fqdn

緩存伺服器中,可以根據不同的機器,配置不同的容量,template就會用在這裡

http-SQL連線架構

http -- memery cache(memory) -- sql (hard disk)

像是設定下面參數

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE=""
OPTIONS=""