创建目录,编写剧本下载nginx:
184 mkdir /etc/ansible/playbook
185 vim /etc/ansible/playbook/nginx.yml
---
- hosts: group
remote_user: root
tasks:
- name: 卸载httpd
yum: name=httpd state=absent
- name: 安装nginx
yum: name=nginx state=present
- name: 修改配置文件
shell: echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html
- name: 修改端口
command: sed -i '/listen/s/80/8080/g' /etc/nginx/nginx.conf
notify:
- restart nginx
- name: 启动服务
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=restarted
186 ansible-playbook /etc/ansible/playbook/nginx.yml
roles
mariadb nginx
[root@nat ~]# ls /etc/ansible/roles/nginx
files handlers meta tasks templates vars
[root@nat ~]# ls /etc/ansible/roles/mariadb/
files handlers meta tasks templates vars
用roles(角色)来下载nginx
创建roles的目录结构:
[root@nat ~]# cd /etc/ansible/roles
[root@nat roles]# ls
[root@nat roles]# mkdir nginx
[root@nat roles]# ls
nginx
[root@nat roles]# cd nginx/
[root@nat nginx]# mkdir files
[root@nat nginx]# mkdir tasks
[root@nat nginx]# mkdir handlers
[root@nat nginx]# mkdir templates
[root@nat nginx]# mkdir vars
[root@nat nginx]# mkdir meta
创建文件:
[root@nat nginx]# touch handlers/main.yml
[root@nat nginx]# touch tasks/main.yml
[root@nat nginx]# touch vars/main.yml
[root@nat nginx]# touch meta/main.yml
[root@nat nginx]# tree
.
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
6 directories, 4 files
编辑文件:
[root@nat ~]# vim /etc/ansible/playbook/test002.yml
---
- hosts: group
remote_user: root
roles:
- nginx
...
[root@nat ~]# ansible-playbook /etc/ansible/playbook/test002.yml
[root@nat ~]# tree /etc/ansible/roles
/etc/ansible/roles
└── nginx
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
[root@nat ~]# vim /etc/ansible/roles/nginx/tasks/main.yml
---
- name: 卸载httpd
yum: name=httpd state=absent
- name: 卸载nginx
yum: name=nginx state=absent
- name: 安装nginx
yum: name=nginx state=present
- name: 启动服务
service: name=nginx state=started enabled=yes
...
[root@nat ~]# ansible-playbook /etc/ansible/playbook/test002.yml
使用roles实现lnmp
nginx改配置8080
mariadb中创建eleme数据库,创建表user
[root@nat ~]# cd /etc/ansible/
[root@nat ansible]# mkdir script
[root@nat ansible]# cd script/
[root@nat script]# vim dbtable.sh
[root@nat script]# vim /etc/ansible/playbook/test003.yml
[root@nat script]# cd /etc/ansible/roles
[root@nat roles]# ls
nginx
[root@nat roles]# cp -r nginx mariadb
[root@nat roles]# ls
mariadb nginx
[root@nat roles]# cd mariadb
[root@nat mariadb]# ls
files handlers meta tasks templates vars
[root@nat mariadb]# vim /etc/ansible/roles/mariadb/tasks/main.yml
[root@nat]# ansible-playbook /etc/ansible/playbook/test003.yml