| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- ---
- # This is an example playbook to execute inspec tests.
- # Tests need distributed to the appropriate ansible host/groups
- # prior to execution by `inspec exec`.
- - name: Verify
- hosts: all
- become: true
- vars:
- inspec_download_source_dir: /usr/local/src
- inspec_package_name: "{{ inspec_download_url.split('/')[-1] }}"
- inspec_bin: /opt/inspec/bin/inspec
- inspec_test_directory: "/tmp/molecule/inspec"
- tasks:
- - name: Install system dependencies for Inspec
- package:
- name: "{{ item }}"
- state: present
- with_items:
- - lsof
- - iproute
- - net-tools
- - name: Download Inspec
- get_url:
- url: "{{ inspec_download_url }}"
- dest: "{{ inspec_download_source_dir }}"
- sha256sum: "{{ inspec_download_sha256sum }}"
- mode: 0755
- register: inspec_download
- - name: Install Inspec
- yum:
- name: "{{ inspec_download.dest }}"
- state: latest
- when: ansible_pkg_mgr == 'yum'
- - name: Install Inspec
- dnf:
- name: "{{ inspec_download.dest }}"
- state: latest
- when: ansible_pkg_mgr == 'dnf'
- - name: Install Inspec
- apt:
- deb: "{{ inspec_download.dest }}"
- state: present
- when: ansible_pkg_mgr == 'apt'
- - name: Create Molecule directory for test files
- file:
- path: "{{ inspec_test_directory }}"
- state: directory
- - name: Copy Inspec tests to remote
- copy:
- src: "{{ item }}"
- dest: "{{ inspec_test_directory }}/{{ item | basename }}"
- with_fileglob:
- - "{{ playbook_dir }}/tests/test_*.rb"
- - name: Register test files
- shell: "ls {{ inspec_test_directory }}/test_*.rb"
- register: test_files
- - name: Execute Inspec tests
- command: "{{ inspec_bin }} exec {{ item }} --no-color --reporter progress"
- register: test_results
- with_items: "{{ test_files.stdout_lines }}"
- ignore_errors: true
- - name: Display details about the Inspec results
- debug:
- msg: "{{ item.stdout_lines }}"
- with_items: "{{ test_results.results }}"
- - name: Fail when tests fail
- fail:
- msg: "Inspec failed to validate"
- when: item.rc != 0
- with_items: "{{ test_results.results }}"
|