secure-web.yml 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ---
  2. # start of secure web server playbook
  3. - name: create secure web service
  4. hosts: webservers
  5. remote_user: devops
  6. become: true
  7. vars:
  8. random_var: This is colon test
  9. tasks:
  10. - block:
  11. - name: install web server packages
  12. yum:
  13. name: "{{ item }}"
  14. state: latest
  15. notify:
  16. - restart services
  17. loop:
  18. - httpd
  19. - mod_ssl
  20. - name: install httpd config files
  21. copy:
  22. src: vhosts.conf
  23. dest: /etc/httpd/conf.d/vhosts.conf
  24. backup: yes
  25. owner: root
  26. group: root
  27. mode: 0644
  28. register: vhosts_config
  29. notify:
  30. - restart services
  31. - name: create ssl certificate
  32. command: openssl req -new -nodes -x509 -subj "/C=US/ST=North Carolina/L=Raleigh/O=Example Inc/CN=serverb.lab.example.com" -days 120 -keyout /etc/pki/tls/private/serverb.lab.example.com.key -out /etc/pki/tls/certs/serverb.lab.example.com.crt -extensions v3_ca
  33. args:
  34. creates: /etc/pki/tls/certs/serverb.lab.example.com.crt
  35. - name: start and enable web services
  36. service:
  37. name: httpd
  38. state: started
  39. enabled: yes
  40. - name: deliver content
  41. copy:
  42. dest: /var/www/vhosts/serverb-secure
  43. src: html/
  44. - name: check httpd syntax
  45. command: /sbin/httpd -t
  46. register: httpd_conf_syntax
  47. failed_when: "'Syntax OK' not in httpd_conf_syntax.stderr"
  48. - name: httpd_conf_syntax variable
  49. debug:
  50. msg: "The httpd_conf_syntax variable value is {{ httpd_conf_syntax }}"
  51. - name: check httpd status
  52. command: systemctl is-active httpd
  53. register: httpd_status
  54. changed_when: httpd_status.rc != 0
  55. notify:
  56. - restart services
  57. rescue:
  58. - name: recover original httpd config
  59. file:
  60. path: /etc/httpd/conf.d/vhosts.conf
  61. state: absent
  62. notify:
  63. - restart services
  64. - name: email notification of httpd config status
  65. mail:
  66. to: student@serverb.lab.example.com
  67. subject: 'httpd config is not correct'
  68. body: "httpd syntax is {{httpd_conf_syntax.stdout}}"
  69. when: httpd_conf_syntax.stdout != 'Syntax OK'
  70. handlers:
  71. - name: restart services
  72. service:
  73. name: httpd
  74. state: restarted
  75. # end of secure web play