使用ansible 完成yum安装lamp环境

使用ansible 完成yum安装lamp环境

[root@node2 ~]# cd /etc/ansible/playbook/
[root@node2 playbook]# ls
lamp
[root@node2 playbook]# tree lamp/
lamp/
├── group_vars
│   └── lamp-vars
├── hosts
├── roles
│   ├── init_sys
│   │   └── tasks
│   │   └── mail.yml
│   └── install
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   ├── install_apache.yml
│   │   ├── install_mysql.yml
│   │   ├── install_php.yml
│   │   └── main.yml
│   └── templates
│   ├── httpd.conf
│   └── phpinfo.php
└── site.yml

8 directories, 11 files

变量文件

[root@node2 lamp]# cat group_vars/lamp-vars
pkg1: httpd
pkg2: mariadb
pkg3: mariadb-server
pkg4: php
pkg5: php-mysql

系统初始化(禁用SElinux,停止防火墙,配置yum源)

[root@node2 lamp]# cat roles/init_sys/tasks/mail.yml
---
- name: configure yum.repo
shell: yum instal yum install http://mirrors.163.com/centos/7.4.1708/extras/x86_64/Packages/epel-release-7-9.noarch.rpm -y
- name: stop firewalld
shell: systemctl stop firewalld
- name: disable firewalld
shell: systemctl disable firewalld
- name: stop selinux
shell: sed 's/=permissive/=disabled/' /etc/selinux/config | setenforce 0

安装apache

[root@node2 lamp]# cat roles/install/tasks/install_apache.yml
---

- name: install apache
yum: name=httpd state=installed
- name: start apache
command: systemctl start httpd
- name: deploy apache rules
template: src=/etc/ansible/playbook/lamp/roles/install/templates/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart apache

- name: wait for apache to start
wait_for: port=80

安装mysql

[root@node2 lamp]# cat roles/install/tasks/install_mysql.yml
---
- nmae: install mysql
yum: pkg={{ pkg2 }} state=latest
- name: install mysql-sever
yum: pkg={{ pkg3 }} state=latest
- name: start mysql
command: systemctl start mariadb

 安装php

[root@node2 lamp]# cat roles/install/tasks/install_php.yml
---
- name: install php
yum: pkg={{ pkg4 }} state=latest
- name: install php-mysql
yum: pkg={{ pkg5 }} state=latest
- name: /var/www/html
template: src=/etc/ansible/playbook/lamp/roles/install/templates/phpinfo.php dest=/var/www/html/phpinfo.php
notify: restart mysql

- name: wait for mysql to start
wait_for: port=3306

 调用notify,当notify中有触发动作时调用

[root@node2 lamp]# cat roles/install/handlers/main.yml
---
- name: restart apache
service: name=apache state=restarted

- name: restart mysql
service: name=mysql state=restarted

模板文件

1)cat roles/install/templates/httpd.conf

#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName 192.168.138.13:80

.....................................................

.........................省略

 

2)[root@node2 lamp]# cat roles/install/templates/phpinfo.php
======================================================================
this is a test web

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

入口文件

[root@node2 lamp]# cat site.yml
---
- hosts: lamp-vars
remote_user: root

roles:
- init_sys
- install

 

转载于:https://www.cnblogs.com/sxchengchen/p/7765329.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值