调用多个角色

1、在模拟一个http的role

调用多个角色_第1张图片

 1 [root@linux-node1 ansible]# cat httpd_role.yml 
 2 - hosts: date
 3   remote_user: root
 4 
 5   roles:
 6     - httpd
 7 [root@linux-node1 tasks]# cat main.yml 
 8 - include: user.yml
 9 - include: copyfile.yml
10 [root@linux-node1 tasks]# cat copyfile.yml 
11 - name: copy files
12   copy: src=httpd.conf dest=/data1/ owner=apache
13 [root@linux-node1 tasks]# cat user.yml 
14 - name: create user
15   user: name=apache
16 
17 #httpd.conf 模拟的测试文件
roles
1 [root@linux-node1 ansible]# cat more_role.yml 
2 - hosts: date
3   remote_user: root
4   roles:
5     - role: httpd
6     - role: nginx
more_role.yml

调用多个角色_第2张图片

 2、夸项目调用别的角色(nginx调用httpd的copyfile.yml)

1 [root@linux-node1 ansible]# cat roles/nginx/tasks/main.yml 
2 - include: group.yml
3 - include: user.yml
4 - include: yum.yml
5 - include: templ.yml
6 - include: start.yml
7 - include: roles/httpd/tasks/copyfile.yml
main.yml

调用多个角色_第3张图片

 3、角色写标签

1 [root@linux-node1 ansible]# cat more_role.yml 
2 - hosts: date
3   remote_user: root
4   roles:
5     - { role: httpd, tags: ['web','httpd'] }
6     - { role: nginx, tags: ['web','nginx'] }
more_role.yml

调用多个角色_第4张图片

 1 [root@linux-node1 ansible]# ansible-playbook -t httpd -C more_role.yml
 2 
 3 PLAY [date] *************************************************************************************************************************************************************************************************************
 4 
 5 TASK [Gathering Facts] **************************************************************************************************************************************************************************************************
 6 ok: [192.168.56.12]
 7 ok: [192.168.56.11]
 8 ok: [192.168.56.13]
 9 
10 TASK [httpd : create user] **********************************************************************************************************************************************************************************************
11 ok: [192.168.56.11]
12 ok: [192.168.56.13]
13 ok: [192.168.56.12]
14 
15 TASK [httpd : copy files] ***********************************************************************************************************************************************************************************************
16 ok: [192.168.56.11]
17 ok: [192.168.56.12]
18 ok: [192.168.56.13]
19 
20 PLAY RECAP **************************************************************************************************************************************************************************************************************
21 192.168.56.11              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
22 192.168.56.12              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
23 192.168.56.13              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
24 
25 [root@linux-node1 ansible]# ansible-playbook -t web -C more_role.yml
26 
27 PLAY [date] *************************************************************************************************************************************************************************************************************
28 
29 TASK [Gathering Facts] **************************************************************************************************************************************************************************************************
30 ok: [192.168.56.12]
31 ok: [192.168.56.11]
32 ok: [192.168.56.13]
33 
34 TASK [httpd : create user] **********************************************************************************************************************************************************************************************
35 ok: [192.168.56.12]
36 ok: [192.168.56.13]
37 ok: [192.168.56.11]
38 
39 TASK [httpd : copy files] ***********************************************************************************************************************************************************************************************
40 ok: [192.168.56.11]
41 ok: [192.168.56.12]
42 ok: [192.168.56.13]
43 
44 TASK [nginx : create group] *********************************************************************************************************************************************************************************************
45 ok: [192.168.56.11]
46 ok: [192.168.56.12]
47 ok: [192.168.56.13]
48 
49 TASK [nginx : create user] **********************************************************************************************************************************************************************************************
50 ok: [192.168.56.11]
51 ok: [192.168.56.12]
52 ok: [192.168.56.13]
53 
54 TASK [nginx : install package] ******************************************************************************************************************************************************************************************
55 ok: [192.168.56.12]
56 ok: [192.168.56.11]
57 ok: [192.168.56.13]
58 
59 TASK [nginx : copy conf] ************************************************************************************************************************************************************************************************
60 ok: [192.168.56.11]
61 ok: [192.168.56.12]
62 ok: [192.168.56.13]
63 
64 TASK [nginx : start service] ********************************************************************************************************************************************************************************************
65 ok: [192.168.56.12]
66 ok: [192.168.56.11]
67 ok: [192.168.56.13]
68 
69 TASK [nginx : copy files] ***********************************************************************************************************************************************************************************************
70 ok: [192.168.56.11]
71 ok: [192.168.56.12]
72 ok: [192.168.56.13]
73 
74 PLAY RECAP **************************************************************************************************************************************************************************************************************
75 192.168.56.11              : ok=9    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
76 192.168.56.12              : ok=9    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
77 192.168.56.13              : ok=9    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
-t 标签,结果不同

 4、角色写标签姐when条件

[root@linux-node1 ansible]# vim more_role.yml 

- hosts: date
  remote_user: root
  roles:
    - { role: httpd, tags: ['web','httpd'] }
    - { role: nginx, tags: ['web','nginx'],when: ansible_distribution_major_version == "7" }

  

你可能感兴趣的:(调用多个角色)