---
# name : le nom du playbook (apporte de la clarté au code)
- name: "Apache Installation"
  # hosts : la liste des machines sur lesquels nous allons travailler
  hosts:
    - "{{targethost}}"
  vars:
    targethost: 8-1.practice-k8s.cloud
  # tasks : une liste d’instructions à dérouler
  tasks:
    - name: "Install apache package"
      # module pour l’installation du package
      ansible.builtin.dnf:
        # Les differentes options
        name: "httpd"
        state: "present"
    - name: "Start apache service"
      ansible.builtin.service:
        name: "httpd"
        state: "started"
        enabled: yes
    - name: "Copy www.html"
      ansible.builtin.copy:
        src: "www.html"
        dest: "/var/www/html"
        owner: "apache"
        group: "apache"
    - name: "Test web server connection"
      ansible.builtin.uri:
        url: http://{{targethost}}/www.html
        status_code: 200
      register: response
      ignore_errors: true
    - name: show response
      ansible.builtin.debug:
        var: response
    - name: Check result code
      ansible.builtin.assert:
        that: response.status == 200
        success_msg: "OK"
        fail_msg: "NOK"
