role/
├── nginx
│ ├── files
│ │ └── index.html //存放测试首页文件(index.html)
│ ├── handlers
│ │ └── main.yaml //存放触发一个事件之后需要做的操作,如:配置文件修改,需要再重启服务
│ ├── tasks
│ │ └── main.yaml //存放基本的一些命令操作
│ ├── templates //下面存放相应的一些配置文件,以便文件作修改
│ └── vars
│ └── main.yaml //存放配置文件中定义的变量以及对应的值
└── site.yaml 存放对哪些角色进行操作
[root@ansible role]# ls
nginx php-nginx site_nginx.retry site.retry
mariadb php-apache site_nginx.yaml
[root@ansible role]# cat site_nginx.yaml
--- // -顶格写
- hosts: group //对哪一个或多个主机的组进行操作
roles:
- nginx
- php-nginx
- mariadb //定义有哪些角色
[root@ansible nginx]# ls
files handlers tasks templates vars
[root@ansible nginx]# cat files/index.html
welcome to nginx
[root@ansible nginx]# cat handlers/main.yaml
---
- name: reload nginx
service: name=nginx state=reloaded
[root@ansible nginx]# ls templates
nginx.conf.j2 //做成金加文件,后面使用template 可以不使用绝对路径
[root@ansible nginx]# cat vars/main.yaml
worker_connections: 10240
nginx_port: 80
nginx_web_dir: /usr/share/nginx/html
[root@ansible nginx]# cat tasks/main.yaml
---
- name: install nginx
yum: name={{ item }} state=present
with_items:
- epel-release
- nginx
- name: copy nginx configure file
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: reload nginx
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: remove httpd
yum: name=httpd state=absent
- name: start and enable nginx
service: name=nginx state=started enabled=yes
[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 //mariadb 不能用reloaded,只能用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
[root@ansible role]# cd php-nginx/
[root@ansible php-nginx]# ls
files handlers tasks templates vars
[root@ansible php-nginx]# cat files/index.php
phpinfo();
?>
[root@ansible php-nginx]# cat handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted
[root@ansible php-nginx]# cat tasks/main.yaml
---
- name: install php-nginx
yum: name={{ item }} state=present //有多个需要安装,可以定义一个组
with_items:
- php
- php-gd
- php-mysql
- gd
- php-fpm //lnmp环境必须安装php-fpm
- name: start php-fpm service
service: name=php-fpm state=started enabled=yes
- name: copy php.ini file
template: src=php.ini.j2 dest=/etc/php.ini
notify: restart nginx
- name: copy index.php
copy: src=index.php dest=/usr/share/nginx/html/index.php
[root@ansible php_nginx]# ls templates/
php.ini.j2 www.conf.j2 //其中www.conf这个配置文件不是必需的,可以没有
在php.ini.j2中配置
memory_limit = {{ num1 }}
post_max_size = {{ num2 }}
upload_max_filesize = {{ num3 }}
在www.conf.j2中配置
listen.owner = {{ user }}
listen.group = {{ user }}
[root@ansible php_nginx]# cat vars/main.yaml
user: apache
num1: 2048M
num2: 2048M
num3: 2048M
[root@ansible role]# ansible-playbook site_nginx.yaml --syntax-check //可以检测是否有语法错误
[root@ansible role]# ansible-playbook site_nginx.yaml //执行进行部署lnmp环境