import files
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
---
|
||||
# file: tests/goss.yml
|
||||
|
||||
- name: tests - create temporary directory
|
||||
command: mktemp -d
|
||||
register: tests_mktemp
|
||||
|
||||
- name: tests - register goss installation
|
||||
environment:
|
||||
PATH: "/usr/local/bin:{{ansible_env.PATH}}"
|
||||
command: which goss
|
||||
register: tests_goss_installed
|
||||
|
||||
- name: tests - register specific OS goss files
|
||||
set_fact:
|
||||
goss_file:
|
||||
- "goss/main_{{ansible_distribution|lower}}-{{ansible_distribution_major_version|lower}}.yml" # main_centos-6.yml main_centos-7.yml
|
||||
- "goss/main_{{ansible_distribution|lower}}.yml" # main_centos.yml main_ubuntu.yml
|
||||
- "goss/main_{{ansible_os_family|lower}}.yml" # main_redhat.yml main_debian.yml
|
||||
- "goss/main_{{ansible_system|lower}}.yml" # main_linux.yml
|
||||
- "goss/main.yml" # main.yml
|
||||
|
||||
- name: tests - register goss file
|
||||
set_fact:
|
||||
tests_goss_file: "{{lookup('first_found', goss_file)}}"
|
||||
|
||||
- name: tests - copy test files
|
||||
copy: src=goss/ dest="{{tests_mktemp.stdout}}"
|
||||
|
||||
- name: tests - launch tests
|
||||
environment:
|
||||
PATH: "/usr/local/bin:{{ansible_env.PATH}}"
|
||||
goss: path="{{tests_mktemp.stdout}}/{{tests_goss_file|basename}}" format=rspecish
|
||||
register: tests_goss_results
|
||||
ignore_errors: true
|
||||
become: yes
|
||||
|
||||
- name: tests - remove temporary directory
|
||||
file: path="{{tests_mktemp.stdout}}" state=absent
|
||||
|
||||
- name: tests - failure message
|
||||
fail: msg="{{tests_goss_results.msg}}"
|
||||
when: tests_goss_results|failed
|
||||
@@ -0,0 +1,4 @@
|
||||
group:
|
||||
docker:
|
||||
exists: true
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
gossfile:
|
||||
package.yml: {}
|
||||
service.yml: {}
|
||||
group.yml: {}
|
||||
@@ -0,0 +1,4 @@
|
||||
gossfile:
|
||||
package_centos-6.yml: {}
|
||||
service.yml: {}
|
||||
group.yml: {}
|
||||
@@ -0,0 +1,4 @@
|
||||
gossfile:
|
||||
package_centos-7.yml: {}
|
||||
service.yml: {}
|
||||
group.yml: {}
|
||||
@@ -0,0 +1,4 @@
|
||||
gossfile:
|
||||
package_debian.yml: {}
|
||||
service.yml: {}
|
||||
group.yml: {}
|
||||
@@ -0,0 +1,3 @@
|
||||
package:
|
||||
docker:
|
||||
installed: true
|
||||
@@ -0,0 +1,3 @@
|
||||
package:
|
||||
docker-io:
|
||||
installed: true
|
||||
@@ -0,0 +1,3 @@
|
||||
package:
|
||||
docker-latest:
|
||||
installed: true
|
||||
@@ -0,0 +1,3 @@
|
||||
package:
|
||||
docker-engine:
|
||||
installed: true
|
||||
@@ -0,0 +1,5 @@
|
||||
service:
|
||||
docker:
|
||||
enabled: true
|
||||
running: true
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
from ansible.module_utils.basic import *
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: goss
|
||||
author: Mathieu Corbin
|
||||
short_description: Launch goss (https://github.com/aelsabbahy/goss) test
|
||||
description:
|
||||
- Launch goss test. Always changed = False if success.
|
||||
options:
|
||||
path:
|
||||
required: true
|
||||
description:
|
||||
- Test file to validate. Must be on the remote machine.
|
||||
format:
|
||||
required: false
|
||||
description:
|
||||
- change the output goss format.
|
||||
- Goss format list : goss v --format => [documentation json junit nagios rspecish tap].
|
||||
- Default: rspecish
|
||||
output_file:
|
||||
required: false
|
||||
description:
|
||||
- save the result of the goss command in a file whose path is output_file
|
||||
examples:
|
||||
- name: test goss file
|
||||
goss:
|
||||
path: "/path/to/file.yml"
|
||||
|
||||
- name: test goss files
|
||||
goss:
|
||||
path: "{{ item }}"
|
||||
format: json
|
||||
output_file : /my/output/file-{{ item }}
|
||||
with_items: "{{ goss_files }}"
|
||||
'''
|
||||
|
||||
|
||||
# launch goss validate command on the file
|
||||
def check(module, test_file_path, output_format):
|
||||
cmd = ""
|
||||
if output_format is not None:
|
||||
cmd = "goss -g {0} v --format {1}".format(test_file_path, output_format)
|
||||
else:
|
||||
cmd = "goss -g {0} v".format(test_file_path)
|
||||
return module.run_command(cmd)
|
||||
|
||||
|
||||
# write goss result to output_file_path
|
||||
def output_file(output_file_path, out):
|
||||
if output_file_path is not None:
|
||||
with open(output_file_path, 'w') as output_file:
|
||||
output_file.write(out)
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
path=dict(required=True, type='str'),
|
||||
format=dict(required=False, type='str'),
|
||||
output_file=dict(required=False, type='str'),
|
||||
),
|
||||
supports_check_mode=False
|
||||
)
|
||||
|
||||
test_file_path = module.params['path'] # test file path
|
||||
output_format = module.params['format'] # goss output format
|
||||
output_file_path = module.params['output_file']
|
||||
|
||||
if test_file_path is None:
|
||||
module.fail_json(msg="test file path is null")
|
||||
|
||||
test_file_path = os.path.expanduser(test_file_path)
|
||||
|
||||
# test if access to test file is ok
|
||||
|
||||
if not os.access(test_file_path, os.R_OK):
|
||||
module.fail_json(msg="Test file %s not readable" % (test_file_path))
|
||||
|
||||
# test if test file is not a dir
|
||||
if os.path.isdir(test_file_path):
|
||||
module.fail_json(msg="Test file must be a file ! : %s" % (test_file_path))
|
||||
|
||||
(rc, out, err) = check(module, test_file_path, output_format)
|
||||
|
||||
if output_file_path is not None:
|
||||
output_file_path = os.path.expanduser(output_file_path)
|
||||
# check if output_file is a file
|
||||
if output_file_path.endswith(os.sep):
|
||||
module.fail_json(msg="output_file must be a file. Actually : %s "
|
||||
% (output_file_path))
|
||||
|
||||
output_dirname = os.path.dirname(output_file_path)
|
||||
|
||||
# check if output directory exists
|
||||
if not os.path.exists(output_dirname):
|
||||
module.fail_json(msg="directory %s does not exists" % (output_dirname))
|
||||
|
||||
# check if writable
|
||||
if not os.access(os.path.dirname(output_file_path), os.W_OK):
|
||||
module.fail_json(msg="Destination %s not writable" % (os.path.dirname(output_file_path)))
|
||||
# write goss result on the output file
|
||||
output_file(output_file_path, out)
|
||||
|
||||
if rc is not None and rc != 0:
|
||||
error_msg = "err : {0} ; out : {1}".format(err, out)
|
||||
module.fail_json(msg=error_msg)
|
||||
|
||||
result = {}
|
||||
result['stdout'] = out
|
||||
result['changed'] = False
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
# file: tests/main.yml
|
||||
|
||||
- include: goss.yml
|
||||
tags:
|
||||
- tests
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
# file: tests/playbook.yml
|
||||
|
||||
- hosts: '{{ target | default("all") }}'
|
||||
tasks:
|
||||
- import_tasks: main.yml
|
||||
Reference in New Issue
Block a user