---
- name: Main playbook
 gather_facts: no
 hosts: 127.0.0.1
 strategy: free
 tasks:
  - name: Create csv file and html file
   file:
    path: "{{ item }}"
    state: touch
   delegae_to: localhost
   become_user: awx
   become: no
   with_items:
    - /tmp/apache.csv
    - /tmp/apache.html
  - include_vars: apache_sever_list.yaml
  - include_tasks: apache_task.yaml
   with_items: '{{ apacheSevers }}'
  - name: Run the csv2html script
   shell: |
    echo "<h3>List of failed Apache servers</h3>"
    echo "<table>" ;
    echo "<table><tr><th>Hostname</th><th>Port</th></tr>"
    while read INPUT; do
    echo "<tr><td>${INPUT//,/</td><td>}</td></tr>";
    done < /tmp/apache.csv
    echo "</table>"
   delegae_to: localhost
   become_user: awx
   become: no    Â
  - name: append
   lineinfile:
    dest: /tmp/apache.html
    line: "{{ output.stdout }}"
    insertafter: EOF
   delegae_to: localhost
   become_user: awx
   become: no
  - name: Check the apache server status
   uri:
    url: "{{ item.hostname }}:{{ item.port }}"
    method: GET
    status_code: 200
    body_format: raw
    follow_redirects: all
    return_content: yes
    validate_certs: no
    force: yes
   delegae_to: localhost
   become_user: awx
   become: no
  - name: append output to file
   lineinfile:
    dest: /tmp/apache.csv
    line: "{{ item.hostname }},{{ item.port }}"
    insertafter: EOF
   delegae_to: localhost
   become_user: awx
   become: no
apache_sever_list.yaml
# file: apache_hosts
# this is an ansible inventory file (using 'ini' format but can use yaml)
# see https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html for more about inventory
[apache]
host1 ap_port=8081
host2 ap_port=80
host3 ap_port=8080
host4 ap_port=8008
host5 ap_port=8123# ansible template file: apache.html.j2
# see https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts
# for how to use variables in templates.
<html>
<body>
<h3>List of failed Apache servers</h3>
<table>
<table><tr><th>Hostname</th><th>Port</th></tr>
<%for host_result in apache_check_result %>
<tr><td>{{ examine apache_check_result to find hostname from results }}</td>{{ again use apache_check_result to get at the port used | default('Failed' }}</tr>
</table>
</body>
</html># playbook: apache_check.yml
- name: report on apache status
 gather_facts: yes
 hosts: apache
Â
 tasks:
  - name: Check the apache server status
   uri:
    url: "{{ ansible_hostname }}:{{ ap_port }}"
    method: GET
    status_code: 200
    body_format: raw
    follow_redirects: all
    return_content: yes
    validate_certs: no
    force: yes
   register: apache_check_result
   ignore_errors: yes
   delegate_to: localhost Â
  Â
  - name: show results for debugging purposes
   debug:
     var: apache_check_result
Â
  - name: template out the results
   template:
    src: apache_html.j2
    dest: /var/apache.html
   delegate_to: localhost
   ansible-playbook -i apache_hosts apache_check.yml