安装nginx并更改其端口
创建目录
mkdir /etc/ansible/playbook
编辑配置文件
vim /etc/ansible/palybook/nginx.yml
---
- hosts: s
remote_user: root
tasks:
- name: 卸载httpd
yum: name=httpd state=absent
- name: 安装nginx
yum: name=nginx state=present
- name: 修改资源文件
shell: echo 'i am nginx' > /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
...
执行测试
ansible-playbook /etc/ansible/playbook/nginx.yml
roles的目录结构
创建roles的目录结构
[root@mo ~]# cd /etc/ansible/roles/
[root@mo roles]# ls
[root@mo roles]# mkdir nginx
[root@mo roles]# ls
nginx
[root@mo roles]# cd nginx/
[root@mo nginx]# mkdir files
[root@mo nginx]# mkdir tasks
[root@mo nginx]# mkdir handlers
[root@mo nginx]# mkdir templates
[root@mo nginx]# mkdir vars
[root@mo nginx]# mkdir meta
[root@mo nginx]# tree
.
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
创建文件
[root@mo nginx]# touch handlers/main.yml
[root@mo nginx]# touch tasks/main.yml
[root@mo nginx]# touch vars/main.yml
[root@mo nginx]# touch meta/main.yml
[root@mo nginx]# tree
.
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
编写配置文件
[root@mo nginx]# vim /etc/ansible/playbook/a.yml
---
- hosts: s
remote_user: root
roles:
- nginx
...
[root@mo nginx]# ansible-playbook /etc/ansible/playbook/a.yml
[root@mo nginx]# ansible s -m yum -a 'name=nginx state=abs
ent'
使用剧本安装nginx
[root@mo nginx]# 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@mo nginx]# ansible-playbook /etc/ansible/roles/nginx/tasks/main.yml
使用role实现lnmp
nginx改配置8080
mariadbz中创建eleme数据库
创建表user
[root@mo ~]# vim /etc/ansible/roles/nginx/tasks/main.yml
---
- name: 卸载mariadb
yum: name=mariadb,mariadb-server,mariadb-devel state=absent
- name: 安装mariadb
yum: name=nginx,mariadb-server,mariadb-devel state=present
- name: 启动mariadb服务
service: name=nginx state=started enabled=yes
- name: 执行脚本,创建数据库和数据表
script: /etc/ansible/script/dbtable.sh
...
[root@mo ~]# cd /etc/ansible/
[root@mo ansible]# mkdir script
[root@mo script]# vim dbtable.sh
[root@mo script]# cd
[root@mo ~]# vim /etc/ansible/playbook/test003.yml
[root@mo ~]# ansible-playbook /etc/ansible/playbook/test003.yml