0%

RHCE (EX294) - 创建和使用角色

创建和使用角色

httpd 软件包已安装,设为在 系统启动时启用启动

防火墙 已启用并正在运行,并使用允许访问 Web 服务器的规则

1
Welcome to HOSTNAME on IPADDRESS

其中,HOSTNAME 是受管节点的 完全限定域名IPADDRESS 则是受管节点的 IP 地址。

创建一个名为 /home/greg/ansible/apache.yml 的 playbook:

该 play 在 webservers 主机组中的主机上运行并将使用 apache 角色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[greg@control ansible]$ ansible-galaxy role init apache --init-path roles/
- Role apache was created successfully
[greg@control ansible]$ vim roles/apache/tasks/main.yml
---
- name: Install the Apache
yum:
name: httpd
state: present
- name: Start service httpd, if not started
service:
name: httpd
state: started
enabled: yes
- name: Start service firewalld, if not started
service:
name: firewalld
state: started
enabled: yes
- firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
- name: Template a file to /var/www/html/index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
[greg@control ansible]$ ansible all -m setup > facts.yml
[greg@control ansible]$ vim roles/apache/templates/index.html.j2
Welcome to {{ ansible_facts['fqdn'] }} on {{ ansible_facts['default_ipv4']['address'] }}
[greg@control ansible]$ vim /home/greg/ansible/apache.yml
---
- name: 创建和使用角色
hosts: webservers
roles:
- apache
[greg@control ansible]$ ansible-playbook apache.yml
[greg@control ansible]$ curl node3
Welcome to node3.lab.example.com on 172.25.250.11
[greg@control ansible]$ curl node4
Welcome to node4.lab.example.com on 172.25.250.12