Contents

RHCE - 模擬考 創建 Roles 並使用

練習環境
可以使用 HowHow 的創建 Lab 練習環境,來操作多台機器練習 Ansible,使用方式請參閱 HowHow 網站

題目

在控制主機上 :

  • 創建 /home/student/ansible/install_httpd.yml 檔案,並使用 httpd role 來安裝 httpd 套件 。
  • 上述檔案內容安裝 Role 角色至 /home/student/ansible/roles 目錄內。
  • 安裝httpd並將其啟動及開機後自動啟動。
  • 防火牆規則開放 http 規則。
  • 使用 template 名稱為 index.html.j2 用來建立網站,其内容為 HOSTNAME - IPADDRESS
  • install_httpd.yml 執行在 webserver 群組。
提示
  • 考試期間是可以查閱 Ansible DOC 文件。
  • Ansible 模組要多使用 ansible-doc 指令內 example 範例來複製修改。
  • Ansible 大多數內容都是上下關連,一個步驟沒有做出會影響其它 LAB 。

參考作法

Ansible - Doc


參考解答

  1. 創建 httpd Role
1
ansible-galaxy init roles/httpd
  1. 編輯 roles/httpd/tasks/main.yml
 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
---
# tasks file for roles/httpd
- name: install http
  yum:
    name: httpd
    state: present

- name: "start {{ item }}  service"
  service:
    name: "{{ item }}"
    state: started
    enabled: yes
  loop:
    - httpd
    - firewalld

- name: firewall rule

firewalld:
    service: http
    permanent: yes
    immediate: yes
    state: enabled

- name: use templates
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
  1. 創建 roles/httpd/templates/index.html.j2
1
{{ ansible_facts.hostname }} - {{ ansible_default_ipv4.address }}
  1. 創建 /home/student/ansible/install_httpd.yml
1
2
3
4
5
---
- name: use httpd role
  hosts: webservers
  roles:
    - httpd


如果你還沒有註冊 Like Coin,你可以在文章最下方看到 Like 的按鈕,點下去後即可申請帳號,透過申請帳號後可以幫我的文章按下 Like,而 Like 最多可以點五次,而你不用付出任何一塊錢,就能給我寫這篇文章的最大的回饋!