From 2902d3d4d1491bfe89d121f3f30a22951858d0f0 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Wed, 16 Nov 2016 23:23:37 +0200 Subject: [PATCH] - List testing dependencies at tests/requirements.txt. - Add pre-commit check for out-of-date dependencies. - Add flake8 pre-commit check. - List roles needed for testing (bootstraping) at tests/requirements.yml, leave other dependencies at meta/main.yml. - Update molecule.yml, ansible.cfg for newer versions of Molecule. - Update documentation, playbook accordingly. --- .pre-commit-config.yaml | 21 ++++++++++++++++---- README.rst | 43 +++++++++++++++++++++++++++++++++++++---- ansible.cfg | 6 +++++- meta/main.yml | 10 +++++----- molecule.yml | 10 +++++----- tasks/main.yml | 4 ++-- tests/playbook.yml | 9 ++++++++- tests/requirements.txt | 6 ++++++ tests/requirements.yml | 7 +++++++ 9 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 tests/requirements.txt create mode 100644 tests/requirements.yml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3016f7..83a18d2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,17 +6,30 @@ - id: check-xml - id: check-yaml - id: check-merge-conflict + - id: flake8 + - id: check-symlinks - repo: https://www.shore.co.il/git/ansible-pre-commit - sha: v0.4.0 + sha: 0fadd691465b97db8992cfc66650f630e433324b hooks: - id: ansible-syntax-check always_run: true files: tests/playbook.yml - args: ['tests/playbook.yml'] + args: + - tests/playbook.yml - repo: https://github.com/willthames/ansible-lint - sha: v3.4.4 + sha: 959ab0f525e9abb19cf75f34381015cf33695f61 hooks: - id: ansible-lint always_run: true files: tests/playbook.yml - args: ['tests/playbook.yml'] + args: + - tests/playbook.yml +- repo: local + hooks: + - id: piprot + name: piprot + description: Check up-to-date Python requirements + language: system + entry: piprot --quiet --outdated tests/requirements.txt + files: requirements.txt + always_run: true diff --git a/README.rst b/README.rst index 06fb1d6..cdbd544 100644 --- a/README.rst +++ b/README.rst @@ -8,7 +8,38 @@ more information read the `blog post Requirements ------------ -See :code:`meta/main.yml` and assertions at top of :code:`tasks/main.yml`. +See :code:`meta/main.yml`, :code:`requirements.yml` and assertions at top of +:code:`tasks/main.yml`. + +Adding the role as a dependency +------------------------------- + +Add the following to your :code:`meta/main.yml`: + +.. code:: yaml + + dependencies: + - src: https://www.shore.co.il/git/ansible-role-example + scm: git + name: example + +When :code: `ansible-galaxy` downloads your role it will also download its +dependencies, ensuring this role will be present and run everytime your role +runs. + +Adding the role to your playbooks +--------------------------------- + +Add the following to your :code:`requirements.yml`: + +.. code:: yaml + + - src: https://www.shore.co.il/git/ansible-role-example + scm: git + name: example + +and update your roles by running :code: `ansible-galaxy install -r +requirements.yml`. Role Variables -------------- @@ -28,17 +59,21 @@ See :code:`tests/playbook.yml`. Testing ------- -To install the dependencies: +Testing requires Virtualbox and Vagrant (out of scope for this documentation). +Install the Python dependencies, add pre-commit hooks by running: .. code:: shell - ansible-galaxy install git+file://$(pwd),$(git rev-parse --abbrev-ref HEAD) -p .molecule/roles + pip install -r tests/requirements.txt + pre-commit install To run the full test suite: .. code:: shell - molecule test + ansible-galaxy install git+file://$(pwd),$(git rev-parse --abbrev-ref HEAD) -p .molecule/roles + pre-commit run --all-files + molecule test --platform all License ------- diff --git a/ansible.cfg b/ansible.cfg index 10b3da5..2bc7613 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,6 +1,10 @@ [defaults] +library = library host_key_checking = False retry_files_enabled = False -roles_path = roles:../:../../:.molecule/roles +roles_path = .molecule/roles:.molecule/../roles:../:../../ command_warnings = True deprecation_warnings = True + +[ssh_connection] +pipelining = True diff --git a/meta/main.yml b/meta/main.yml index ba13102..2dea7f4 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -7,9 +7,9 @@ galaxy_info: platforms: - name: OpenBSD versions: - - 5.9 + - 6.0 + - name: Ubuntu + versions: + - xenial galaxy_tags: [ ansible ] -dependencies: - - src: https://www.shore.co.il/git/ansible-role-openbsd-bootstrap - scm: git - role: openbsd_bootstrap +dependencies: [] diff --git a/molecule.yml b/molecule.yml index a62eaa4..16a28c0 100644 --- a/molecule.yml +++ b/molecule.yml @@ -1,12 +1,10 @@ --- ansible: - verbose: v + verbose: vv playbook: tests/playbook.yml diff: True - -molecule: - testinfra_dir: tests - ansible_config_template: ansible.cfg + config_file: ../ansible.cfg + requirements_file: tests/requirements.yml vagrant: providers: @@ -15,6 +13,8 @@ vagrant: platforms: - name: openbsd box: kaorimatz/openbsd-6.0-amd64 + - name: ubuntu + box: ubuntu/xenial64 instances: - name: ansible-role-example options: diff --git a/tasks/main.yml b/tasks/main.yml index d519beb..5772d26 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -4,5 +4,5 @@ - name: Assertions assert: that: - - ansible_os_family == 'OpenBSD' - - ansible_distribution_release == '6.0' + - ansible_os_family in ['OpenBSD', 'Debian'] + - ansible_distribution_release in ['6.0', 'xenial'] diff --git a/tests/playbook.yml b/tests/playbook.yml index e739a2b..02cca1a 100644 --- a/tests/playbook.yml +++ b/tests/playbook.yml @@ -1,5 +1,12 @@ --- -- hosts: all +- hosts: ansible-role-example-openbsd + gather_facts: false + roles: [openbsd_bootstrap] + +- hosts: ansible-role-example-ubuntu gather_facts: false + roles: [debian_bootstrap] + +- hosts: all roles: - role: ansible-role-example diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..0588c35 --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,6 @@ +ansible==2.2.0.0 +testinfra==1.4.3 +molecule==1.13.0 +ansible-lint==3.4.4 +pre-commit==0.9.3 +piprot==0.9.7 diff --git a/tests/requirements.yml b/tests/requirements.yml new file mode 100644 index 0000000..06c6a6b --- /dev/null +++ b/tests/requirements.yml @@ -0,0 +1,7 @@ +--- +- src: https://www.shore.co.il/git/ansible-role-openbsd-bootstrap + scm: git + role: openbsd_bootstrap +- src: https://www.shore.co.il/git/ansible-role-debian-bootstrap + scm: git + role: debian_bootstrap -- GitLab