solution
```
# webcontent.yml
---
- name: Configure restricted web content for dev hosts
hosts: dev
become: true
gather_facts: true
tasks:
# ---------------- SELinux ----------------
- name: Ensure SELinux is enforcing
ansible.posix.selinux:
policy: targeted
state: enforcing
- name: Install SELinux utilities
ansible.builtin.package:
name: policycoreutils-python-utils
state: present
# ---------------- Groups & Users ----------------
- name: Ensure webdev group exists
ansible.builtin.group:
name: webdev
state: present
- name: Add ansible user to webdev group
ansible.builtin.user:
name: ansible
groups: webdev
append: true
# ---------------- Web Content ----------------
- name: Create /webdev directory with setgid permissions
ansible.builtin.file:
path: /webdev
state: directory
owner: root
group: webdev
mode: "2775"
- name: Create index.html using Ansible facts
ansible.builtin.copy:
dest: /webdev/index.html
owner: root
group: webdev
mode: "0644"
content: |
WebDev Host Info
WebDev Page
Hostname: {{ ansible_facts['hostname'] }}
IP Address: {{ ansible_facts['default_ipv4']['address'] }}
# ---------------- Apache + Symlink ----------------
- name: Create symlink from /webdev to /var/www/html/webdev
ansible.builtin.file:
src: /webdev
dest: /var/www/html/webdev
state: link
force: true
# ---------------- SELinux Context ----------------
- name: Allow Apache to read /webdev via SELinux
ansible.builtin.command:
cmd: semanage fcontext -a -t httpd_sys_content_t "/webdev(/.*)?"
register: semanage_result
failed_when: semanage_result.rc not in [0,1]
- name: Apply SELinux context
ansible.builtin.command: restorecon -Rv /webdev
changed_when: false
# ---------------- Firewall ----------------
- name: Ensure firewalld is started and enabled
ansible.builtin.service:
name: firewalld
state: started
enabled: true
- name: Allow HTTP through firewall
ansible.posix.firewalld:
service: http
permanent: true
immediate: true
state: enabled
# ---------------- Apache Access Control ----------------
- name: Restrict access to webdev content to node1 only
ansible.builtin.copy:
dest: /etc/httpd/conf.d/webdev.conf
owner: root
group: root
mode: "0644"
content: |
Options FollowSymLinks
Require all granted
Require ip 127.0.0.1
Require ip {{ ansible_facts['default_ipv4']['address'] }}
# ---------------- Services ----------------
- name: Ensure httpd is started and enabled
ansible.builtin.service:
name: httpd
state: started
enabled: true
- name: Restart httpd to apply configuration
ansible.builtin.service:
name: httpd
state: restarted
```
## Task 11.
**manage file content with templates:**
populate /etc/myhosts using hosts.j2 template and hosts.yml. Do not modify hosts.yml at all, it should handle all of the looping through the hosts in the template file
use a for loop on the j2 template to loop through each host