使用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