diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000000000000000000000000000000000000..905c8bbe4f14c1d47c7bb8ffa0aee63110389858 --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 160 \ No newline at end of file diff --git a/.gitignore b/.gitignore index dce4445b76ce79b39ab9cdfbd62a21a5aae430f4..785a5dfa7358d3e79b18c56de52156f738080686 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,8 @@ testsite/ssh/*_key.pub testsite/ssh/*.example.com testsite/ssh/*.example.com.pub testsite/backup_keyring/ + +# Ignore Molecule artefacts. +.molecule +.vagrant +.cache \ No newline at end of file diff --git a/roles/preseed/molecule.yml b/roles/preseed/molecule.yml new file mode 100644 index 0000000000000000000000000000000000000000..b525320c4bcc19e2cc59a61c957d2f3404caf178 --- /dev/null +++ b/roles/preseed/molecule.yml @@ -0,0 +1,29 @@ +--- + +dependency: {} + +driver: + name: vagrant + +vagrant: + + platforms: + - name: debian-jessie64 + box: debian/contrib-jessie64 + - name: debian-stretch64 + box: debian/contrib-stretch64 + + providers: + - name: virtualbox + type: virtualbox + options: + memory: 512 + cpus: 1 + + instances: + - name: parameters-mandatory + - name: parameters-optional + - name: parameters-optional-with-overrides + +verifier: + name: testinfra diff --git a/roles/preseed/playbook.yml b/roles/preseed/playbook.yml new file mode 100644 index 0000000000000000000000000000000000000000..3711cfafc5b85a8ee6071c74a7b0511fd7ef854d --- /dev/null +++ b/roles/preseed/playbook.yml @@ -0,0 +1,53 @@ +--- + +- hosts: parameters-mandatory + roles: + - role: preseed + +- hosts: parameters-optional + roles: + - role: preseed + ansible_key: CUSTOMKEY + preseed_country: RS + preseed_directory: /tmp/custom_preseed_files_location + preseed_dns: 1.1.1.1 + preseed_domain: example.com + preseed_gateway: 2.2.2.2 + preseed_hostname: testing + preseed_ip: 3.3.3.3 + preseed_keymap: sv + preseed_language: sr + preseed_locale: en_UK.UTF-8 + preseed_mirror_directory: / + preseed_mirror_hostname: ftp.de.debian.org + preseed_mirror_proxy: http://proxy.local + preseed_netmask: 255.255.0.0 + preseed_network_auto: no + preseed_network_interface: eth1 + preseed_root_password: myrootpassword + preseed_timezone: Europe/Belgrade + +- hosts: parameters-optional-with-overrides + roles: + - role: preseed + preseed_directory: /tmp/custom_preseed_files_location + ansible_key: CUSTOMKEY + preseed_server_overrides: + parameters-optional-with-overrides: + country: RS + dns: 1.1.1.1 + domain: example.com + gateway: 2.2.2.2 + hostname: testing + ip: 3.3.3.3 + keymap: sv + language: sr + locale: en_UK.UTF-8 + mirror_directory: / + mirror_hostname: ftp.de.debian.org + mirror_proxy: http://proxy.local + netmask: 255.255.0.0 + network_auto: no + network_interface: eth1 + root_password: myrootpassword + timezone: Europe/Belgrade diff --git a/roles/preseed/tests/test_parameters_mandatory.py b/roles/preseed/tests/test_parameters_mandatory.py new file mode 100644 index 0000000000000000000000000000000000000000..8740af04c206f1ad07ec31e9a243f9e0e3e2e64f --- /dev/null +++ b/roles/preseed/tests/test_parameters_mandatory.py @@ -0,0 +1,70 @@ +import os + +import testinfra.utils.ansible_runner + + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + '.molecule/ansible_inventory').get_hosts('parameters-mandatory') + + +def test_preseed_directory(File, Sudo): + """ + Tests presence and permissions on created preseed directory. + """ + + with Sudo(): + + # Preseed directory created at same level as inventory. + preseed_directory = File(os.path.join(os.getcwd(), ".molecule", 'preseed_files')) + + assert preseed_directory.is_directory + assert preseed_directory.mode == 0o750 + + +def test_preseed_configuration_files(File, Sudo): + """ + Tests presence and permissions on created preseed configuration files. + """ + + with Sudo(): + + # Preseed directory created at same level as inventory. + preseed_directory_path = os.path.join(os.getcwd(), ".molecule", 'preseed_files') + + # Verify that preseed configuration files are created for all hosts. + for host in testinfra_hosts: + + preseed_file = File(os.path.join(preseed_directory_path, "%s.cfg" % host)) + + assert preseed_file.is_file + assert preseed_file.mode == 0o640 + + +def test_preseed_configuration_file_content(File, Sudo, TestinfraBackend): + """ + Tests content of generated preseed configuration file. + """ + + with Sudo(): + preseed_file = File(os.path.join(os.getcwd(), ".molecule", "preseed_files", "%s.cfg" % TestinfraBackend.get_hostname())) + preseed_file_content = preseed_file.content_string + ssh_public_key = open(os.path.join(os.path.expanduser("~"), ".ssh", "id_rsa.pub")).read().strip() + + assert "d-i debian-installer/language string en" in preseed_file_content + assert "d-i debian-installer/country string SE" in preseed_file_content + assert "d-i debian-installer/locale string en_US.UTF-8" in preseed_file_content + assert "d-i keyboard-configuration/xkb-keymap select us" in preseed_file_content + assert "d-i netcfg/choose_interface select eth0" in preseed_file_content + + assert """# DHCP network configuration. +d-i netcfg/disable_autoconfig boolean false +d-i netcfg/get_hostname string ignored-value +d-i netcfg/get_domain string ignored-value""" in preseed_file_content + + assert "d-i mirror/http/hostname string ftp.se.debian.org" in preseed_file_content + assert "d-i mirror/http/directory string /debian" in preseed_file_content + assert "d-i mirror/http/proxy string " in preseed_file_content + assert "d-i passwd/root-password password root" in preseed_file_content + assert "d-i passwd/root-password-again password root" in preseed_file_content + assert "d-i time/zone string Europe/Stockholm" in preseed_file_content + assert ssh_public_key in preseed_file_content diff --git a/roles/preseed/tests/test_parameters_optional.py b/roles/preseed/tests/test_parameters_optional.py new file mode 100644 index 0000000000000000000000000000000000000000..42f2103a104ad92c166e4be5c6ef7da52050ed7d --- /dev/null +++ b/roles/preseed/tests/test_parameters_optional.py @@ -0,0 +1,76 @@ +import os + +import testinfra.utils.ansible_runner + + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + '.molecule/ansible_inventory').get_hosts('parameters-optional') + + +PRESEED_DIRECTORY = '/tmp/custom_preseed_files_location' + + +def test_preseed_directory(File, Sudo): + """ + Test presence and permissions of preseed directory. + """ + + with Sudo(): + + preseed_directory = File(PRESEED_DIRECTORY) + + assert preseed_directory.is_directory + assert preseed_directory.mode == 0o750 + + +def test_preseed_configuration_files(File, Sudo, TestinfraBackend): + """ + Tests presence and permissions on created preseed configuration files. + """ + + with Sudo(): + + # Verify that preseed configuration files are created for all hosts. + for host in testinfra_hosts: + + preseed_file = File(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % host)) + + assert preseed_file.is_file + assert preseed_file.mode == 0o640 + + +def test_preseed_configuration_file_content(File, Sudo, TestinfraBackend): + """ + Tests content of generated preseed configuration file. + """ + + with Sudo(): + preseed_file = File(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % TestinfraBackend.get_hostname())) + preseed_file_content = preseed_file.content_string + ssh_public_key = "CUSTOMKEY" + + assert "d-i debian-installer/language string sr" in preseed_file_content + assert "d-i debian-installer/country string RS" in preseed_file_content + assert "d-i debian-installer/locale string en_UK.UTF-8" in preseed_file_content + assert "d-i keyboard-configuration/xkb-keymap select sv" in preseed_file_content + assert "d-i netcfg/choose_interface select eth1" in preseed_file_content + + assert """# Manual network configuration. +d-i netcfg/disable_autoconfig boolean true +d-i netcfg/get_ipaddress string 3.3.3.3 +d-i netcfg/get_netmask string 255.255.0.0 +d-i netcfg/get_gateway string 2.2.2.2 +d-i netcfg/get_nameservers string 1.1.1.1 +d-i netcfg/confirm_static boolean true + +# Hostname and domain configuration. +d-i netcfg/get_hostname string testing +d-i netcfg/get_domain string example.com""" in preseed_file_content + + assert "d-i mirror/http/hostname string ftp.de.debian.org" in preseed_file_content + assert "d-i mirror/http/directory string /" in preseed_file_content + assert "d-i mirror/http/proxy string http://proxy.local" in preseed_file_content + assert "d-i passwd/root-password password myrootpassword" in preseed_file_content + assert "d-i passwd/root-password-again password myrootpassword" in preseed_file_content + assert "d-i time/zone string Europe/Belgrade" in preseed_file_content + assert ssh_public_key in preseed_file_content diff --git a/roles/preseed/tests/test_parameters_optional_with_overrides.py b/roles/preseed/tests/test_parameters_optional_with_overrides.py new file mode 100644 index 0000000000000000000000000000000000000000..4490affe9bf1b758e374887601eb5e389848dfd4 --- /dev/null +++ b/roles/preseed/tests/test_parameters_optional_with_overrides.py @@ -0,0 +1,60 @@ +import os + +import testinfra.utils.ansible_runner + + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + '.molecule/ansible_inventory').get_hosts('parameters-optional-with-overrides') + + +PRESEED_DIRECTORY = '/tmp/custom_preseed_files_location' + + +def test_preseed_directory(File, Sudo, Ansible): + """ + Test presence and permissions of preseed directory. + """ + + with Sudo(): + + preseed_directory = File(PRESEED_DIRECTORY) + + assert preseed_directory.is_directory + assert preseed_directory.mode == 0o750 + + +def test_preseed_configuration_file_content(File, Sudo, TestinfraBackend): + """ + Tests content of generated preseed configuration file. + """ + + with Sudo(): + preseed_file = File(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % TestinfraBackend.get_hostname())) + preseed_file_content = preseed_file.content_string + ssh_public_key = "CUSTOMKEY" + + assert "d-i debian-installer/language string sr" in preseed_file_content + assert "d-i debian-installer/country string RS" in preseed_file_content + assert "d-i debian-installer/locale string en_UK.UTF-8" in preseed_file_content + assert "d-i keyboard-configuration/xkb-keymap select sv" in preseed_file_content + assert "d-i netcfg/choose_interface select eth1" in preseed_file_content + + assert """# Manual network configuration. +d-i netcfg/disable_autoconfig boolean true +d-i netcfg/get_ipaddress string 3.3.3.3 +d-i netcfg/get_netmask string 255.255.0.0 +d-i netcfg/get_gateway string 2.2.2.2 +d-i netcfg/get_nameservers string 1.1.1.1 +d-i netcfg/confirm_static boolean true + +# Hostname and domain configuration. +d-i netcfg/get_hostname string testing +d-i netcfg/get_domain string example.com""" in preseed_file_content + + assert "d-i mirror/http/hostname string ftp.de.debian.org" in preseed_file_content + assert "d-i mirror/http/directory string /" in preseed_file_content + assert "d-i mirror/http/proxy string http://proxy.local" in preseed_file_content + assert "d-i passwd/root-password password myrootpassword" in preseed_file_content + assert "d-i passwd/root-password-again password myrootpassword" in preseed_file_content + assert "d-i time/zone string Europe/Belgrade" in preseed_file_content + assert ssh_public_key in preseed_file_content