1. 考题
创建和使用角色
根据下列要求,在 /home/student/ansible/roles 中创建名为 apache 的角色:
- httpd 软件包已安装,设为在系统启动时启用并启动
- 防火墙已启用并正在运行,并使用允许访问 Web 服务器的规则
模板文件 index.html.j2 已存在,用于创建具有以下输出的文件
/var/www/html/index.html :
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的完全限定域名, IPADDRESS 则是受管节点的 IP 地址。
创建一个名为 /home/student/ansible/apache.yml 的 playbook:
该 play 在 webservers 主机组中的主机上运行并将使用 apache 角色
2. 解题
2.1 创建apache角色
ansible-galaxy init /home/student/ansible/roles/apache
2.2 配置/home/student/ansible/roles/apache/tasks/main.yml
---
# tasks file for roles/apache
- name: install httpd
yum:
name: httpd
- name: config firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: template index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
setype: httpd_sys_content_t
- name: start httpd service
service:
name: httpd
state: started
enabled: yes
2.3 配置模板文件
vi /home/student/ansible/roles/apache/template/index.html.j2
Welcome to {
{
ansible_fqdn }} on {
{
ansible_default_ipv4.address }}
2.4 配置apache.yml
vi /home/student/ansible/apache.yml
---
- hosts: webservers
roles:
- apache
2.5 执行apache.yml
ansible-playbook /home/student/ansible/apache.yml
3. 确认本题是否成功
没有报错即成功
curl serverc
curl serverd