一、roles的作用
1、对比Includes功能,Roles更适合于大项目playbook的编排架构,简而言之,Ad-Hoc适用于临时命令的执行,Playbook合适中小项目,而大项目一定使用Roles。
2、Roles主要依赖于目录的命名和摆放,默认tasks/main.yml是所有任务的主入口,所以使用roles的过程可以理解为目录规范化命名的过程,如nginx项目:
[root@Ansible roles]# tree nginx
[root@Ansible roles]# pwd
/etc/ansible/roles
You have new mail in /var/spool/mail/root
[root@Ansible roles]# ll
total 4
drwxr-xr-x 6 root root 61 Jan 25 01:21 nginx
-rw-r--r-- 1 root root 80 Jan 25 02:12 nginx.yml
[root@Ansible roles]# tree nginx
nginx
├── files #用于存放需要复制到对端的服务哭喊
│ └── nginx.conf
├── tasks
│ ├── file.yml #用于指定那些文件需要复制到对端
│ ├── install_nginx.yml #安装nginx
│ ├── main.yml #主程序,可用于调整每个文件的执行顺序
│ └── restarted_nginx.yml #重启nginx服务
├── templates
└── vars
二、/etc/ansible/roles/nginx.yml 执行文件代码
---
- hosts: test
remote_user: cedar
become: yes
roles:
- role: nginx
三、nginx目录下各个文件的代码
mail.yml
[root@Ansible nginx]# cat tasks/main.yml
---
- include: install_nginx.yml
- include: file.yml
- include: restarted_nginx.yml
file.yml
[root@Ansible nginx]# cat tasks/file.yml
---
- name: "传输配置文件到nginx服务器"
copy: src={{'nginx.conf'}} dest=/etc/nginx/nginx.conf
install_nginx.yml
[root@Ansible nginx]# cat tasks/install_nginx.yml
---
- name: "nginx安装"
yum: name=nginx state=latest
- name: "设置开机启动"
service: name=nginx enabled=yes
restarted_nginx.yml
[root@Ansible nginx]# cat tasks/restarted_nginx.yml
---
- name: "重启nginx"
service: name=nginx state=restarted
nginx.conf配置文件
server {
listen 81 default_server; #修改位置,监听81
listen [::]:81 default_server; #修改位置,监听81
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
四、运行结果
[root@Ansible roles]# ansible-playbook nginx.yml
PLAY [test] *****************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [10.3.153.8]
TASK [nginx安装] **************************************************************************************************************************************************
changed: [10.3.153.8]
TASK [nginx : 设置开机启动] *******************************************************************************************************************************************
changed: [10.3.153.8]
TASK [传输配置文件到nginx服务器] ******************************************************************************************************************************************
changed: [10.3.153.8]
TASK [重启nginx] **************************************************************************************************************************************************
changed: [10.3.153.8]
PLAY RECAP ******************************************************************************************************************************************************
10.3.153.8 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0