基于ansible Role实现批量部署lamp平台
1、配置好 Inventory 文件# /etc/ansible/hosts
node2.example.com
node3.example.com
director1.example.com
director2.example.com
2、创建对应的目录树
# 执行 tree /root/lamp/roles 查看目录树
/root/lamp/roles
|-- common
| |-- default
| |-- files
| | `-- hosts
| |-- handlers
| |-- meta
| |-- tasks
| | `-- main.yml
| |-- templates
| `-- vars
|-- db
| |-- default
| |-- files
| | `-- my.cnf
| |-- handlers
| | `-- main.yml
| |-- meta
| |-- tasks
| | `-- main.yml
| |-- templates
| `-- vars
|-- db.yml
|-- php
| |-- default
| |-- files
| | `-- php.ini
| |-- handlers
| |-- meta
| |-- tasks
| | `-- main.yml
| |-- templates
| `-- vars
|-- site.yml
|-- web
| |-- default
| |-- files
| | `-- httpd.conf
| |-- handlers
| | `-- main.yml
| |-- meta
| |-- tasks
| | `-- main.yml
| |-- templates
| `-- vars
`-- web.yml
3、各个文件的内容
# 当前所在目录 /root/lamp/roles
# cat db.yml
- name: mysqld servise
remote_user: root
hosts: mysql
roles:
- common
- db
# cat web.yml
- name: wed servise
remote_user: root
hosts: webhosts
roles:
- common
- php
- web
# cat common/files/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.10.22 node2.example.com
172.16.10.33 node3.example.com
172.16.0.50 director1.example.com
172.16.0.51 director2.example.com
# cat common/tasks/main.yml
- name: Configure hostname resolve
copy: src=hosts dest=/etc/hosts
# cat php/tasks/main.yml
- name: install php
yum: name=php state=present
- name: configuration php
copy: src=php.ini dest=/etc/php.ini
# cat web/tasks/main.yml
- name: install httpd
yum: name=httpd state=present
- name: configuration httpd
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: service httpd start
service: name=httpd enabled=no state=started
# cat web/handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
# cat db/tasks/main.yml
- name: install mysql
yum: name=mysql state=present
- name: install mysql-server
yum: name=mysql-server state=present
- name: configuration mysqld
copy: src=my.cnf dest=/etc/my.cnf
notify:
- restart mysqld
- name: service mysqld start
service: name=mysqld enabled=no state=started
# cat db/handlers/main.yml
- name: restart mysqld
service: name=mysqld state=restarted
4、开始批量部署
ansible-playbooks web.yml
ansible-playbooks db.yml
页:
[1]