import files

This commit is contained in:
Yann Autissier
2021-02-09 17:05:00 +01:00
parent f5c4576411
commit 44a6d37ba5
425 changed files with 23195 additions and 0 deletions
+43
View File
@@ -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
+10
View File
@@ -0,0 +1,10 @@
file:
/etc/bashrc:
exists: true
mode: "0644"
owner: root
group: root
filetype: file
contains:
- "source /etc/profile.d/bashrc.sh"
@@ -0,0 +1,12 @@
file:
/etc/profile.d/bashrc.sh:
exists: true
mode: "0644"
owner: root
group: root
filetype: file
contains:
- /^function git_branch/
- /^function process_count/
- /^function load_average/
@@ -0,0 +1,13 @@
file:
/etc/bash.bashrc:
exists: true
mode: "0644"
owner: root
group: root
filetype: file
contains:
- "source /etc/profile.d/bashrc.sh"
gossfile:
bash_common.yml: {}
+5
View File
@@ -0,0 +1,5 @@
gossfile:
package.yml: {}
bash.yml: {}
root.yml: {}
# ssh.yml: {}
@@ -0,0 +1,5 @@
gossfile:
package_debian.yml: {}
bash_debian.yml: {}
root.yml: {}
# ssh.yml: {}
@@ -0,0 +1,5 @@
gossfile:
package_redhat.yml: {}
bash.yml: {}
root.yml: {}
# ssh.yml: {}
@@ -0,0 +1,5 @@
gossfile:
package_common.yml: {}
package:
vim:
installed: true
@@ -0,0 +1,11 @@
package:
bash:
installed: true
ca-certificates:
installed: true
screen:
installed: true
rsync:
installed: true
tzdata:
installed: true
@@ -0,0 +1,5 @@
gossfile:
package_common.yml: {}
package:
vim-nox:
installed: true
@@ -0,0 +1,5 @@
gossfile:
package_common.yml: {}
package:
vim-minimal:
installed: true
+9
View File
@@ -0,0 +1,9 @@
file:
/root/.screenrc:
exists: true
mode: "0644"
owner: root
group: root
filetype: file
contains:
- /^hardstatus alwayslastline/
+17
View File
@@ -0,0 +1,17 @@
file:
/etc/ssh/sshd_config:
exists: true
mode: "0644"
owner: root
group: root
filetype: file
contains:
- /^PermitRootLogin prohibit-password/
/root/.ssh/authorized_keys:
exists: true
mode: "0600"
owner: root
group: root
filetype: file
contains:
- "Jpb0EeFEebgvi7Kpp6gpIXKFEeuuE"
+118
View File
@@ -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()
+6
View File
@@ -0,0 +1,6 @@
---
# file: tests/main.yml
- include: goss.yml
tags:
- tests
+6
View File
@@ -0,0 +1,6 @@
---
# file: tests/playbook.yml
- hosts: '{{ target | default("all") }}'
tasks:
- import_tasks: main.yml