verify.yml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ---
  2. # This is an example playbook to execute inspec tests.
  3. # Tests need distributed to the appropriate ansible host/groups
  4. # prior to execution by `inspec exec`.
  5. - name: Verify
  6. hosts: all
  7. become: true
  8. vars:
  9. inspec_download_source_dir: /usr/local/src
  10. inspec_package_name: "{{ inspec_download_url.split('/')[-1] }}"
  11. inspec_bin: /opt/inspec/bin/inspec
  12. inspec_test_directory: "/tmp/molecule/inspec"
  13. tasks:
  14. - name: Install system dependencies for Inspec
  15. package:
  16. name: "{{ item }}"
  17. state: present
  18. with_items:
  19. - lsof
  20. - iproute
  21. - net-tools
  22. - name: Download Inspec
  23. get_url:
  24. url: "{{ inspec_download_url }}"
  25. dest: "{{ inspec_download_source_dir }}"
  26. sha256sum: "{{ inspec_download_sha256sum }}"
  27. mode: 0755
  28. register: inspec_download
  29. - name: Install Inspec
  30. yum:
  31. name: "{{ inspec_download.dest }}"
  32. state: latest
  33. when: ansible_pkg_mgr == 'yum'
  34. - name: Install Inspec
  35. dnf:
  36. name: "{{ inspec_download.dest }}"
  37. state: latest
  38. when: ansible_pkg_mgr == 'dnf'
  39. - name: Install Inspec
  40. apt:
  41. deb: "{{ inspec_download.dest }}"
  42. state: present
  43. when: ansible_pkg_mgr == 'apt'
  44. - name: Create Molecule directory for test files
  45. file:
  46. path: "{{ inspec_test_directory }}"
  47. state: directory
  48. - name: Copy Inspec tests to remote
  49. copy:
  50. src: "{{ item }}"
  51. dest: "{{ inspec_test_directory }}/{{ item | basename }}"
  52. with_fileglob:
  53. - "{{ playbook_dir }}/tests/test_*.rb"
  54. - name: Register test files
  55. shell: "ls {{ inspec_test_directory }}/test_*.rb"
  56. register: test_files
  57. - name: Execute Inspec tests
  58. command: "{{ inspec_bin }} exec {{ item }} --no-color --reporter progress"
  59. register: test_results
  60. with_items: "{{ test_files.stdout_lines }}"
  61. ignore_errors: true
  62. - name: Display details about the Inspec results
  63. debug:
  64. msg: "{{ item.stdout_lines }}"
  65. with_items: "{{ test_results.results }}"
  66. - name: Fail when tests fail
  67. fail:
  68. msg: "Inspec failed to validate"
  69. when: item.rc != 0
  70. with_items: "{{ test_results.results }}"