当前位置:首页 > 站长资讯 > 建站
Ansible的roles功能(五)
时间:2016-12-29 22:08:32   来源:Char   编辑:涛涛   点击:  

A-A+

针对大型项目使用Roles进行编排,更便利

目录结构编排

wKiom1hlF-SjFR-QAAHrlrracRc046.png-wh_50

一键安装httpd并提供服务

创建目录结构

[root@centos6-1 ansible]# mkdir -p roles/httpd/{tasks,files,vars}

[root@centos6-1 ansible]# cd roles/httpd/tasks

[root@centos6-1 roles]#touch httpd/{files/main.yml,tasks/{groupadd.yml,install.yml,main.yml,restart.yml,useradd.yml},vars/main.yml}

wKioL1hlGAPC6d7dAAAWn-l58cw101.png-wh_50

目录结构创建好了,接下来开始配置

[root@centos6-1 ~]# cd /root/ansible/roles/httpd/tasks

首先配置main.yml将所有的配置全部包含进来

[root@centos6-1 tasks]# cat main.yml

---

- include: groupadd.yml

- include: useradd.yml

- include: install.yml

- include: restart.yml

配置添加用户组

[root@centos6-1 tasks]# cat groupadd.yml

---

- name: add group httpd

group: name=web state=present

配置添加用户

[root@centos6-1 tasks]# cat useradd.yml

---

- name: add group httpd

user: name=httpd groups=web system=yes state=present

配置安装httpd

[root@centos6-1 tasks]# cat install.yml

---

- name: Install Httpd

yum: name=httpd state=present

配置restart httpd

[root@centos6-1 tasks]# cat restart.yml

---

- name: Restart Httpd

service: name=httpd state=restarted

配置主yml文件

[root@centos6-1 roles]# pwd

/root/ansible/roles

[root@centos6-1 roles]# cat installhttpd.yml

---

- hosts: test

remote_user: root

roles:

- role: httpd

测试

wKioL1hlGDGyYvrVAABJ4zKZS1Q115.png-wh_50

wKiom1hlGDLxN-uzAABcJ2oXM1U514.png-wh_50

提供配置网站文件

[root@centos6-1 files]# pwd

/root/ansible/roles/httpd/files

[root@centos6-1 files]# cat index.html

<h1> test </h1>

创建copy模块的文件

[root@centos6-1 tasks]# cat filecp.yml

---

- name: Cp DocumentFile

copy: src=index.html dest=/var/www/html/index.html

将文件包含至main

[root@centos6-1 tasks]# cat main.yml

---

- include: groupadd.yml

- include: useradd.yml

- include: install.yml

- include: restart.yml

- include: filecp.yml

测试:

wKioL1hlGEeyQRC5AABoCL_CzYc798.png-wh_50

roles还可以引用

例:

[root@centos6-1 roles]# pwd

/root/ansible/roles

[root@centos6-1 roles]# cp httpd/ nginx -rf

[root@centos6-1 roles]# cd nginx/tasks/

[root@centos6-1 tasks]# rm -rf *

[root@centos6-1 roles]# cd nginx/tasks/

[root@centos6-1 tasks]# cat main.yml

---

- include: httpd/tasks/restart.yml

[root@centos6-1 roles]# cp installhttpd.yml installnginx.yml

[root@centos6-1 roles]# cat installnginx.yml

---

- hosts: test

remote_user: root

roles:

- role: nginx

测试:

wKiom1hlGGGSv4m5AAAxcfFki-8847.png-wh_50

拓展:

1、一次性执行多个roles

wKiom1hlGHvxmBGnAAAVX8Llvik818.png-wh_50

2、对roles进行打标签,可单独执行

wKioL1hlGIizmAwoAAAYgSuYthY638.png-wh_50

测试

wKioL1hlGJvjcnqmAAAyb2X2HYo500.png-wh_50

3、多重标签

wKiom1hlGLKASxGmAAAznxhViSQ830.png-wh_50

执行web项目

wKioL1hlGMLTPfB5AABzXBheCLY179.png-wh_50








本文出自 “Char” 博客,谢绝转载!

  踩 1
分享到:
精彩评论
看过本文的人还看过
专栏
  • 240期
    一个文化艺术垂直电子商务网站。
  • 240期
    一个文化艺术垂直电子商务网站。