编写playbook,实现安装配置httpd服务,使用模板生成httpd的主页文件/var/www/html/index.html主页显示的内容是Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipaddress }}。
时间: 2024-02-03 16:15:52 浏览: 29
以下是一个实现安装配置httpd服务并使用模板生成主页文件的Playbook:
```yaml
---
- name: Install and configure httpd
hosts: all
become: true
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: Start httpd and enable at boot
service:
name: httpd
state: started
enabled: yes
- name: Configure firewall for httpd
firewalld:
service: http
permanent: true
state: enabled
immediate: true
- name: Generate index.html file
template:
src: templates/index.html.j2
dest: /var/www/html/index.html
```
在此Playbook中,我们首先使用`yum`模块安装httpd。然后,我们使用`service`模块启动httpd并将其设置为开机启动。接下来,我们使用`firewalld`模块配置防火墙以允许http流量。最后,我们使用`template`模块从模板文件`index.html.j2`生成主页文件,并将其放置在`/var/www/html/index.html`位置。
以下是`index.html.j2`模板文件的内容:
```html
<html>
<head>
<title>Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}</title>
</head>
<body>
<h1>Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}</h1>
</body>
</html>
```
该模板文件中,我们使用了`{{ ansible_fqdn }}`和`{{ ansible_default_ipv4.address }}`变量,这些变量分别对应主机的完全域名和IP地址,并将它们插入到HTML文件中。