基本文件目录如下:
httpd所需软件:httpd mariadb mariadb-server php php-mysql gd php-gd
[root@ansible role]# ls
httpd site_nginx.retry site.retry
mariadb php-apache site_apache.yaml
[root@ansible role]# cat site_apache.yaml
---
- hosts: group
roles:
- httpd
- php-apache
- mariadb
1.httpd目录
[root@ansible role]# cd httpd/
[root@ansible httpd]# ls
files handlers tasks templates vars
[root@ansible httpd]# cat files/index.html
hello world
[root@ansible httpd]# cat handlers/main.yaml
---
- name: reload httpd.service
service: name=httpd state=reloaded
[root@ansible httpd]# cat tasks/main.yaml
---
- name: install httpd
yum: name=httpd state=present
- name: copy httpd configure file
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: reload httpd.service
- name: copy index.html
copy: src=index.html dest=/var/www/html/index.html
- name: start and enable httpd
service: name=httpd state=started enabled=yes
[root@ansible httpd]# ls templates/
httpd.conf.j2
在httpd.conf.j2中定义变量
Listen {{ httpd_port }}
[root@ansible httpd]# cat vars/main.yaml
httpd_port: 80
2.mariadb目录
[root@ansible role]# cd mariadb/
[root@ansible mariadb]# ls
handlers tasks templates vars //mariadb 不需要测试首页可以不写
[root@ansible mariadb]# cat handlers/main.yaml
---
- name: restart mariadb.service
service: name=mariadb state=restarted
[root@ansible mariadb]# cat handlers/main.yaml
---
- name: restart mariadb.service
service: name=mariadb state=restarted
[root@ansible mariadb]# cat tasks/main.yaml
---
- name: install mariadb
yum: name={{ item }} state=present
with_items:
- mariadb
- mariadb-server
- name: copy mariadb configure file
template: src=my.cnf.j2 dest=/etc/
notify: restart mariadb.service
- name: start and enable mariadb
service: name=mariadb state=started enabled=yes
[root@ansible mariadb]# ls templates/
my.cnf.j2
[root@ansible mariadb]# cat vars/main.yaml
datadir: /var/lib/mysql
socket: /var/lib/mysql/mysql.sock
3.apache中php目录
[root@ansible php-apache]# ls
files handlers tasks templates
[root@ansible php-apache]# cat handlers/main.yaml
---
- name: restart httpd
service: name=httpd state=restarted
[root@ansible php-apache]# cat handlers/main.yaml
---
- name: restart httpd
service: name=httpd state=restarted
[root@ansible php-apache]# cat tasks/main.yaml
---
- name: install php-fpm
yum: name={{ item }} state=present
with_items:
- php
- php-gd
- php-mysql
- gd
- name: copy php configure file
template: src=php.ini.j2 dest=/etc/php.ini
notify: restart httpd
- name: copy index.php
copy: src=index.php dest=/var/www/html/index.php
notify: restart httpd
[root@ansible php-apache]# ls templates/
php.ini.j2 //不定义变量可以不创建vars目录