用ansible剧本搭建lnmp

Posted cash-su

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用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.
", 
        "stderr_lines": [
            "Shared connection to 192.168.200.131 closed."
        ], 
        "stdout": "/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源出错
", 
        "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.
", 
        "stderr_lines": [
            "Shared connection to 192.168.200.133 closed."
        ], 
        "stdout": "/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]

", 
        "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

 

以上是关于用ansible剧本搭建lnmp的主要内容,如果未能解决你的问题,请参考以下文章

你是自己的主角,写个ansible剧本,让架构搭建更容易

使用Ansible搭建LNMP

Ansible项目实战搭建lnmp

Ansible项目实战搭建lnmp

大环境

ansible的管理与剧本