1. 程式人生 > >用ansible劇本搭建lnmp

用ansible劇本搭建lnmp

首先在主伺服器上搭建ansible直接用雲yum裝就可以,

yum -y install ansible

如果copy報錯一下的語句 "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

需要在被分發的伺服器上安裝支援包

[[email protected] ~]# mount /dev/sr0 /media/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
[
[email protected]
~]# yum -y install libselinux-python

 

然後建立roles的標準化模組相應的目錄

[[email protected] myroles]# tree /myroles/
/myroles/
├── nginx.yaml  #nginx模組入口配置檔案
└── roles
    └── nginx   #nginx原型模組目錄
        ├── files
        ├── handlers
        ├── tasks
        │   └── main.yaml   #nginx模組的tasks任務配置檔案
        ├── templates
        └── vars
7 directories, 2 files

Nginx的劇本書寫,在myroles裡,跟roles同級

---
- hosts: all   
  gather_facts: True    
  roles:         
  - nginx   

然後在Nginx目錄下files目錄裡放置安裝包和,安裝指令碼檔案

nginx的搭建指令碼

#!/bin/bash
mkdir -p /media/cdrom
umount /dev/sr0 &>/dev/null
mount /dev/sr0 /media/cdrom &>/dev/null
dir=/etc/yum.repos.d
[ 
-d $dir ] || mkdir -p $dir cd $dir mv * /tmp/ cat >/etc/yum.repos.d/local.repo << KOF [local] name=localrepo baseurl=file:///media/cdrom/ KOF yum -y clean all &>/dev/null [ $? -eq 0 ] || echo "clean erro" yum makecache &>/dev/null || echo "erro cache" which "wget" [ $? -eq 0 ] || /usr/bin/yum -y install wget &>/dev/null /usr/bin/wget http://mirrors.aliyun.com/repo/epel-6.repo [ $? -eq 0 ] || (/bin/echo "yun源出錯" && exit) /usr/bin/yum -y clean all &>/dev/null /usr/bin/yum makecache &>/dev/null [ $? -eq 0 ] || (/bin/echo "yun快取錯誤" && exit) /usr/bin/yum -y install pcre-deved openssl-devel &>/dev/null [ $? -eq 0 ] || /bin/echo "pcre error" useradd -M -s /sbin/nologin nginx &>dev/null cd ~ tar xf nginx-1.10.2.tar.gz -C /usr/src/ cd /usr/src/nginx-1.10.2/ ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.10.2/ --with-http_stub_status_module --with-http_ssl_module && make && make install &>/dev/null

在tasks裡建立個main.yaml主要任務劇本

- name: t1
  copy: src=nginx-1.10.2.tar.gz dest=/root/
  register: ttt
- debug: var=ttt
- name: t2
  script: nginx.sh
  register: rrr
- debug: var=rrr
           

回到mysoles裡執行nginx.yaml劇本

[[email protected] myroles]# ansible-playbook nginx.yaml 

PLAY [all] *************************************************************************************************

TASK [Gathering Facts] *************************************************************************************
ok: [web1]
ok: [web2]

TASK [nginx : t1] ******************************************************************************************
ok: [web2]
ok: [web1]

TASK [nginx : debug] ***************************************************************************************
ok: [web1] => {
    "ttt": {
        "changed": false, 
        "checksum": "1bafb1557b8d5f992714c0dcbde77036bde98547", 
        "dest": "/root/nginx-1.10.2.tar.gz", 
        "diff": {
            "after": {
                "path": "/root/nginx-1.10.2.tar.gz"
            }, 
            "before": {
                "path": "/root/nginx-1.10.2.tar.gz"
            }
        }, 
        "failed": false, 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "path": "/root/nginx-1.10.2.tar.gz", 
        "secontext": "unconfined_u:object_r:admin_home_t:s0", 
        "size": 910812, 
        "state": "file", 
        "uid": 0
    }
}
ok: [web2] => {
    "ttt": {
        "changed": false, 
        "checksum": "1bafb1557b8d5f992714c0dcbde77036bde98547", 
        "dest": "/root/nginx-1.10.2.tar.gz", 
        "diff": {
            "after": {
                "path": "/root/nginx-1.10.2.tar.gz"
            }, 
            "before": {
                "path": "/root/nginx-1.10.2.tar.gz"
            }
        }, 
        "failed": false, 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "path": "/root/nginx-1.10.2.tar.gz", 
        "secontext": "unconfined_u:object_r:admin_home_t:s0", 
        "size": 910812, 
        "state": "file", 
        "uid": 0
    }
}

TASK [nginx : t2] ******************************************************************************************
changed: [web2]
changed: [web1]

TASK [nginx : debug] ***************************************************************************************
ok: [web1] => {
    "rrr": {
        "changed": true, 
        "failed": false, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.200.131 closed.\r\n", 
        "stderr_lines": [
            "Shared connection to 192.168.200.131 closed."
        ], 
        "stdout": "/usr/bin/wget\r\n--2018-11-12 19:02:03--  http://mirrors.aliyun.com/repo/epel-6.repo\r\nResolving mirrors.aliyun.com... failed: Name or service not known.\r\nwget: unable to resolve host address “mirrors.aliyun.com”\r\nyun源出錯\r\n", 
        "stdout_lines": [
            "/usr/bin/wget", 
            "--2018-11-12 19:02:03--  http://mirrors.aliyun.com/repo/epel-6.repo", 
            "Resolving mirrors.aliyun.com... failed: Name or service not known.", 
            "wget: unable to resolve host address “mirrors.aliyun.com”", 
            "yun源出錯"
        ]
    }
}
ok: [web2] => {
    "rrr": {
        "changed": true, 
        "failed": false, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.200.133 closed.\r\n", 
        "stderr_lines": [
            "Shared connection to 192.168.200.133 closed."
        ], 
        "stdout": "/usr/bin/wget\r\n--2018-11-12 16:03:20--  http://mirrors.aliyun.com/repo/epel-6.repo\r\nResolving mirrors.aliyun.com... 122.72.3.220, 122.72.3.219, 122.72.3.221, ...\r\nConnecting to mirrors.aliyun.com|122.72.3.220|:80... connected.\r\nHTTP request sent, awaiting response... 200 OK\r\nLength: 664 [application/octet-stream]\r\nSaving to: “epel-6.repo”\r\n\r\n\r 0% [                                       ] 0           --.-K/s              \r100%[======================================>] 664         --.-K/s   in 0s      \r\n\r\n2018-11-12 16:03:21 (62.4 MB/s) - “epel-6.repo” saved [664/664]\r\n\r\n", 
        "stdout_lines": [
            "/usr/bin/wget", 
            "--2018-11-12 16:03:20--  http://mirrors.aliyun.com/repo/epel-6.repo", 
            "Resolving mirrors.aliyun.com... 122.72.3.220, 122.72.3.219, 122.72.3.221, ...", 
            "Connecting to mirrors.aliyun.com|122.72.3.220|:80... connected.", 
            "HTTP request sent, awaiting response... 200 OK", 
            "Length: 664 [application/octet-stream]", 
            "Saving to: “epel-6.repo”", 
            "", 
            "", 
            " 0% [                                       ] 0           --.-K/s              ", 
            "100%[======================================>] 664         --.-K/s   in 0s      ", 
            "", 
            "2018-11-12 16:03:21 (62.4 MB/s) - “epel-6.repo” saved [664/664]", 
            ""
        ]
    }
}

PLAY RECAP *************************************************************************************************
web1                       : ok=5    changed=1    unreachable=0    failed=0   
web2                       : ok=5    changed=1    unreachable=0    failed=0 

這樣Nginx服務就先簡單的搭建成功,需要主要目前沒有任何的配置檔案和啟動服務

接下來我搭建mysql