RHCE8 练习题:创建和使用角色
根据下列要求在 /home/student/ansible/roles中创建名为 apache的角色:
- httpd软件包已安装,设为在系统启动时自启并启动
- 防火墙已启用并正在运行,并使用 允许访问web服务器的规则
- 模板文件 index.html.j2 用于创建 /var/www/html/index.html 具有以下输出内容:
- Welcome to HOSTNAME on IPADDRESS
- HOSTNAME 是受管节点的完全限定域名,
- IPADDRESS 是受管节点的IP地址
- 按照下方所属,创建一个使用此角色的playbook,/home/student/ansible/newrole.yml
- 该playbook 在 webservers主机组中 的主机上运行
Answer
创建apache角色并编辑
cd /home/student/ansible/roles
ansible-galaxy init apache
vim apache/tasks/main.yml
---
- name: install apache
yum:
name: httpd
- name: start httpd firewalld
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- httpd
- firewalld
- name: firewalld port add
firewalld:
service: http
immediate: yes
permanent: yes
state: enabled
- name: j2 template
template:
src: index.html.j2
dest: /var/www/html/index.html
newrole.yml playbook 编写
vim /home/student/ansible/newrole.yml
---
- name: start apache
hosts: webservers
roles:
- apache
模板文件index.html.j2
vim /home/student/ansible/roles/apache/templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ansible_default_ipv4.address}}
ansible-playbook newrole.yml
验证
curl serverc
curl serverd