diff --git a/roles/backup/meta/main.yml b/roles/backup/meta/main.yml index ba98f0de8b5b1ae276890a75fefe62c6c5d2c181..6d3e2a61165ec06b293df864d787356dc08d55a2 100644 --- a/roles/backup/meta/main.yml +++ b/roles/backup/meta/main.yml @@ -3,4 +3,4 @@ allow_duplicates: yes dependencies: - - backup_client \ No newline at end of file + - backup_client diff --git a/roles/backup/molecule.yml b/roles/backup/molecule.yml deleted file mode 100644 index 00dd4cbd076aa0ebdda8fe5afe6f76db882adaf2..0000000000000000000000000000000000000000 --- a/roles/backup/molecule.yml +++ /dev/null @@ -1,33 +0,0 @@ ---- - -ansible: - ansiblecfg_ssh_connection: - pipelining: True - -dependency: {} - -driver: - name: vagrant - -vagrant: - - platforms: - - name: debian-jessie64 - box: debian/contrib-jessie64 - - name: debian-stretch64 - box: debian/stretch64 - - providers: - - name: virtualbox - type: virtualbox - options: - memory: 512 - cpus: 1 - - instances: - - - name: parameters-mandatory - - name: parameters-optional - -verifier: - name: testinfra diff --git a/roles/backup/molecule/default/create.yml b/roles/backup/molecule/default/create.yml new file mode 100644 index 0000000000000000000000000000000000000000..f8eb37cd4df02c540216c02791d0c50870986202 --- /dev/null +++ b/roles/backup/molecule/default/create.yml @@ -0,0 +1,56 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: False + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Create molecule instance(s) + molecule_vagrant: + instance_name: "{{ item.name }}" + instance_interfaces: "{{ item.interfaces | default(omit) }}" + instance_raw_config_args: "{{ item.instance_raw_config_args | default(omit) }}" + + platform_box: "{{ item.box }}" + platform_box_version: "{{ item.box_version | default(omit) }}" + platform_box_url: "{{ item.box_url | default(omit) }}" + + provider_name: "{{ molecule_yml.driver.provider.name }}" + provider_memory: "{{ item.memory | default(omit) }}" + provider_cpus: "{{ item.cpus | default(omit) }}" + provider_raw_config_args: "{{ item.raw_config_args | default(omit) }}" + + state: up + register: server + with_items: "{{ molecule_yml.platforms }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config dict + set_fact: + instance_conf_dict: { + 'instance': "{{ item.Host }}", + 'address': "{{ item.HostName }}", + 'user': "{{ item.User }}", + 'port': "{{ item.Port }}", + 'identity_file': "{{ item.IdentityFile }}", } + with_items: "{{ server.results }}" + register: instance_config_dict + when: server.changed | bool + + - name: Convert instance config dict to a list + set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + when: server.changed | bool + + - name: Dump instance config + copy: + # NOTE(retr0h): Workaround for Ansible 2.2. + # https://github.com/ansible/ansible/issues/20885 + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool diff --git a/roles/backup/molecule/default/destroy.yml b/roles/backup/molecule/default/destroy.yml new file mode 100644 index 0000000000000000000000000000000000000000..3972a2df8fafe515b30a74f951499b83aae8449c --- /dev/null +++ b/roles/backup/molecule/default/destroy.yml @@ -0,0 +1,36 @@ +--- + +- name: Destroy + hosts: localhost + connection: local + gather_facts: False + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_instance_config: "{{ lookup('env',' MOLECULE_INSTANCE_CONFIG') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Destroy molecule instance(s) + molecule_vagrant: + instance_name: "{{ item.name }}" + platform_box: "{{ item.box }}" + provider_name: "{{ molecule_yml.driver.provider.name }}" + force_stop: "{{ item.force_stop | default(True) }}" + + state: destroy + register: server + with_items: "{{ molecule_yml.platforms }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + set_fact: + instance_conf: {} + + - name: Dump instance config + copy: + # NOTE(retr0h): Workaround for Ansible 2.2. + # https://github.com/ansible/ansible/issues/20885 + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool diff --git a/roles/backup/molecule/default/molecule.yml b/roles/backup/molecule/default/molecule.yml new file mode 100644 index 0000000000000000000000000000000000000000..cab3cf35b8c91b686e4dff2cb5f903b45ded876a --- /dev/null +++ b/roles/backup/molecule/default/molecule.yml @@ -0,0 +1,57 @@ +--- + +dependency: {} + +driver: + name: vagrant + provider: + name: virtualbox + +lint: + name: yamllint + +platforms: + + - name: parameters-mandatory-jessie64 + groups: + - parameters-mandatory + box: debian/contrib-jessie64 + memory: 256 + cpus: 1 + + - name: parameters-optional-jessie64 + groups: + - parameters-optional + box: debian/contrib-jessie64 + memory: 256 + cpus: 1 + + - name: parameters-mandatory-stretch64 + groups: + - parameters-mandatory + box: debian/contrib-stretch64 + memory: 256 + cpus: 1 + + - name: parameters-optional-stretch64 + groups: + - parameters-optional + box: debian/contrib-stretch64 + memory: 256 + cpus: 1 + +provisioner: + name: ansible + config_options: + ssh_connection: + pipelining: "True" + lint: + name: ansible-lint + +scenario: + name: default + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/roles/backup/playbook.yml b/roles/backup/molecule/default/playbook.yml similarity index 89% rename from roles/backup/playbook.yml rename to roles/backup/molecule/default/playbook.yml index 6548f2ba4ca13fcd74aa745c90fb4e7dda60bae5..526735e77492a7ce5b54735a8725b0806b5c274d 100644 --- a/roles/backup/playbook.yml +++ b/roles/backup/molecule/default/playbook.yml @@ -1,14 +1,7 @@ --- -- hosts: all - tasks: - - - name: Update all caches to avoid errors due to missing remote archives - apt: - update_cache: yes - changed_when: False - - hosts: parameters-mandatory + become: yes roles: - role: backup backup_patterns_filename: "test" @@ -25,6 +18,7 @@ - hosts: parameters-optional + become: yes roles: - role: backup backup_patterns_filename: "test" diff --git a/roles/backup/molecule/default/prepare.yml b/roles/backup/molecule/default/prepare.yml new file mode 100644 index 0000000000000000000000000000000000000000..2ca5369027ee16118496dcf8548f8d803a0c9699 --- /dev/null +++ b/roles/backup/molecule/default/prepare.yml @@ -0,0 +1,19 @@ +--- + +- name: Prepare + hosts: all + gather_facts: False + tasks: + - name: Install python for Ansible + raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) + become: True + changed_when: False + +- hosts: all + become: yes + tasks: + + - name: Update all caches to avoid errors due to missing remote archives + apt: + update_cache: yes + changed_when: False diff --git a/roles/backup/tests/data/gnupg/parameters-mandatory.asc b/roles/backup/molecule/default/tests/data/gnupg/parameters-mandatory.asc similarity index 100% rename from roles/backup/tests/data/gnupg/parameters-mandatory.asc rename to roles/backup/molecule/default/tests/data/gnupg/parameters-mandatory.asc diff --git a/roles/backup/tests/data/gnupg/parameters-optional.asc b/roles/backup/molecule/default/tests/data/gnupg/parameters-optional.asc similarity index 100% rename from roles/backup/tests/data/gnupg/parameters-optional.asc rename to roles/backup/molecule/default/tests/data/gnupg/parameters-optional.asc diff --git a/roles/backup/tests/data/ssh/parameters-mandatory b/roles/backup/molecule/default/tests/data/ssh/parameters-mandatory similarity index 100% rename from roles/backup/tests/data/ssh/parameters-mandatory rename to roles/backup/molecule/default/tests/data/ssh/parameters-mandatory diff --git a/roles/backup/tests/data/ssh/parameters-optional b/roles/backup/molecule/default/tests/data/ssh/parameters-optional similarity index 100% rename from roles/backup/tests/data/ssh/parameters-optional rename to roles/backup/molecule/default/tests/data/ssh/parameters-optional diff --git a/roles/backup/tests/data/ssh/server_dsa.pub b/roles/backup/molecule/default/tests/data/ssh/server_dsa.pub similarity index 100% rename from roles/backup/tests/data/ssh/server_dsa.pub rename to roles/backup/molecule/default/tests/data/ssh/server_dsa.pub diff --git a/roles/backup/tests/data/ssh/server_ecdsa.pub b/roles/backup/molecule/default/tests/data/ssh/server_ecdsa.pub similarity index 100% rename from roles/backup/tests/data/ssh/server_ecdsa.pub rename to roles/backup/molecule/default/tests/data/ssh/server_ecdsa.pub diff --git a/roles/backup/tests/data/ssh/server_ed25519.pub b/roles/backup/molecule/default/tests/data/ssh/server_ed25519.pub similarity index 100% rename from roles/backup/tests/data/ssh/server_ed25519.pub rename to roles/backup/molecule/default/tests/data/ssh/server_ed25519.pub diff --git a/roles/backup/tests/data/ssh/server_rsa.pub b/roles/backup/molecule/default/tests/data/ssh/server_rsa.pub similarity index 100% rename from roles/backup/tests/data/ssh/server_rsa.pub rename to roles/backup/molecule/default/tests/data/ssh/server_rsa.pub diff --git a/roles/backup/tests/test_default.py b/roles/backup/molecule/default/tests/test_default.py similarity index 67% rename from roles/backup/tests/test_default.py rename to roles/backup/molecule/default/tests/test_default.py index 9a5e89dc076817844e3ea267eb7c899d4533f4b0..077de93d4e69ee8dbf9eee9b66c5f11af1dfad5c 100644 --- a/roles/backup/tests/test_default.py +++ b/roles/backup/molecule/default/tests/test_default.py @@ -2,17 +2,17 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('all') + '.molecule/ansible_inventory.yml').get_hosts('all') -def test_backup_patterns_file(File, Sudo): +def test_backup_patterns_file(host): """ Tests if deployed file with backup patterns has correct permissions. """ - with Sudo(): + with host.sudo(): - backup_pattern = File('/etc/duply/main/patterns/test') + backup_pattern = host.file('/etc/duply/main/patterns/test') assert backup_pattern.is_file assert backup_pattern.user == 'root' @@ -20,14 +20,14 @@ def test_backup_patterns_file(File, Sudo): assert backup_pattern.mode == 0o600 -def test_include_file(File, Sudo): +def test_include_file(host): """ Tests if assembled include file has correct permissions. """ - with Sudo(): + with host.sudo(): - include = File('/etc/duply/main/include') + include = host.file('/etc/duply/main/include') assert include.is_file assert include.user == 'root' diff --git a/roles/backup/tests/test_parameters_mandatory.py b/roles/backup/molecule/default/tests/test_parameters_mandatory.py similarity index 55% rename from roles/backup/tests/test_parameters_mandatory.py rename to roles/backup/molecule/default/tests/test_parameters_mandatory.py index 45969ad1ffa437eba0a073591ca28b473759fc05..af520a4056450972ec6128c35f7e8458478e2bc8 100644 --- a/roles/backup/tests/test_parameters_mandatory.py +++ b/roles/backup/molecule/default/tests/test_parameters_mandatory.py @@ -2,29 +2,29 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('parameters-mandatory') + '.molecule/ansible_inventory.yml').get_hosts('parameters-mandatory') -def test_backup_patterns_content(File, Sudo): +def test_backup_patterns_content(host): """ Tests if content of file containing backup patterns is correct. """ - with Sudo(): + with host.sudo(): - backup_pattern = File('/etc/duply/main/patterns/test') + backup_pattern = host.file('/etc/duply/main/patterns/test') assert backup_pattern.content == '' -def test_include_content(File, Sudo): +def test_include_content(host): """ Tests if content of assembled include file containing backup patterns is correct. """ - with Sudo(): + with host.sudo(): - include = File('/etc/duply/main/include') + include = host.file('/etc/duply/main/include') assert include.content == '' diff --git a/roles/backup/tests/test_parameters_optional.py b/roles/backup/molecule/default/tests/test_parameters_optional.py similarity index 59% rename from roles/backup/tests/test_parameters_optional.py rename to roles/backup/molecule/default/tests/test_parameters_optional.py index 76a3be49d42cb6f64028ead5de50c42eb73bdf53..267fe39704d0f9acbddc54cc7761ca1333894e62 100644 --- a/roles/backup/tests/test_parameters_optional.py +++ b/roles/backup/molecule/default/tests/test_parameters_optional.py @@ -2,29 +2,29 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('parameters-optional') + '.molecule/ansible_inventory.yml').get_hosts('parameters-optional') -def test_backup_patterns_content(File, Sudo): +def test_backup_patterns_content(host): """ Tests if content of file containing backup patterns is correct. """ - with Sudo(): + with host.sudo(): - backup_pattern = File('/etc/duply/main/patterns/test') + backup_pattern = host.file('/etc/duply/main/patterns/test') assert backup_pattern.content == "/etc/hosts\n/etc/ethers\n/var/log" -def test_include_content(File, Sudo): +def test_include_content(host): """ Tests if content of assembled include file containing backup patterns is correct. """ - with Sudo(): + with host.sudo(): - include = File('/etc/duply/main/include') + include = host.file('/etc/duply/main/include') assert include.content == "/etc/hosts\n/etc/ethers\n/var/log"