本文主要是介绍roles(角色),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
创建目录,编写剧本下载nginx:
184 mkdir /etc/ansible/playbook
185 vim /etc/ansible/playbook/nginx.yml
---
- hosts: groupremote_user: roottasks:- name: 卸载httpdyum: name=httpd state=absent- name: 安装nginxyum: 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.confnotify:- restart nginx- name: 启动服务service: name=nginx state=started enabled=yeshandlers:- name: restart nginxservice: 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: groupremote_user: rootroles:- 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: 卸载httpdyum: name=httpd state=absent
- name: 卸载nginxyum: name=nginx state=absent
- name: 安装nginxyum: 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
这篇关于roles(角色)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!