ansible+playbook 搭建lnmp环境

用三台机器 做ansible+playbook 搭建lnmp环境 

IP分配 

ansible 主机192.168.202.132

lnmp第一台主机 192.168.202.131

lnmp第一台主机 192.168.202.133

1.1 ansible主机安装ansible

[root@localhost ~]# yum -y install ansible
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.jdcloud.com
 * extras: mirror.bit.edu.cn 

1.2 ansible主机生成密钥设置免密码登录

[root@localhost ~]# ssh -keygen
Bad escape character 'ygen'.
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_r
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_
Your public key has been saved in /root/.ssh/id_rsa.
The key fingerprint is:
SHA256:snafmBirD5A4bkRNfs/yraWQ7BqJf4SKpLd0UO5yZyA rhost
The key's randomart image is:
+---[RSA 2048]----+
|   .             |
|  +              |
| . o..           |
|.. +. o          |
|o.E +..oS        |
|oo.*oo+o.        |
|+++o*+*..o       |
|+oo+o*.=++ .     |
| ..++++oo o      |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be i "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.202.131 (192.168.2can't be established.
ECDSA key fingerprint is SHA256:+YOuMKydPwvlPwXHq3Cgj6X6jdlupF1W3A.
ECDSA key fingerprint is MD5:33:f9:54:0f:ce:1e:53:96:96:29:e3:04:7c.
Are you sure you want to continue connecting (yes/no
/usr/bin/ssh-copy-id: INFO: attempting to log in wit key(s), to filter out any that are already installe
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be in- if you are prompted now it is to install the new k
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root202.131'"
and check to make sure that only the key(s) you wantdded.
[root@localhost ~]# ssh-copy-id [email protected]

  

1.3 ansible主机配置

[root@localhost ~]# vim /etc/ansible/hosts 

[lnmp]
192.168.202.131
192.168.202.133

 如图:

 1.4 编写ansible的playbook  

---
        - hosts: lnmp
          tasks:
                - name: "安装需要的环境"
                  yum: name=gcc,gcc-c++,make,php,php-gd,php-mysql,php-fpm,pcre-devel,zlib-devel,mariadb,mariadb-server state=latest
                - name: "导入nginx包"
                  unarchive: src=nginx-1.16.1.tar.gz dest=/root
                - name: "编译安装"
                  shell: cd /root/nginx-1.16.1 && ./configure && make && make install
                - name: "分发配置文件" copy: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf - name: "分发主界面" copy: src=info.php dest=/var/www/html/info.php - name: 启动nginx shell: /usr/local/nginx/sbin/nginx - name: "启动mysql" shell: systemctl restart mariadb - name: "启动php-fpm" shell: systemctl restart php-fpm - name : "关闭防火墙" shell: systemctl stop firewalld - name: "关闭selinux" shell: setenforce 0 ~ 

1.4.2 更改nginx的配置文件

1.4.3 分发一个php的测试界面

1.4.4 执行ansible-playbook

 

1.4.5 展示效果看看lnmp是否搭建成功

猜你喜欢

转载自www.cnblogs.com/gaiting/p/12075043.html
今日推荐