From 865418e614ee5b00ce84970c42aca49e7c76999b Mon Sep 17 00:00:00 2001 From: lotyp Date: Wed, 29 Jun 2022 01:24:39 +0300 Subject: [PATCH 1/2] feat: refactored ci file, updates to pyproject --- .ansible-lint | 4 + .editorconfig | 2 +- .github/workflows/ci.yml | 140 +++++++----------- .later.yml | 6 +- .yamllint | 1 + Makefile | 54 +------ README.md | 33 ++--- meta/main.yml | 2 +- molecule/_partials/playbooks/converge.yml | 2 + molecule/_partials/playbooks/verify.yml | 2 + molecule/_partials/tasks/setup.yml | 12 +- .../default-macos-on-localhost}/ansible.cfg | 4 - molecule/default-macos-over-ssh/ansible.cfg | 7 + molecule/default/ansible.cfg | 7 + tests/defaults/main.yml | 6 - tests/inventory.yml | 10 -- tests/test.yml | 10 -- 17 files changed, 110 insertions(+), 192 deletions(-) rename {tests => molecule/default-macos-on-localhost}/ansible.cfg (55%) create mode 100644 molecule/default-macos-over-ssh/ansible.cfg create mode 100644 molecule/default/ansible.cfg delete mode 100644 tests/defaults/main.yml delete mode 100644 tests/inventory.yml delete mode 100644 tests/test.yml diff --git a/.ansible-lint b/.ansible-lint index 8a65fdc..d49ac24 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -3,3 +3,7 @@ # https://ansible-lint.readthedocs.io/en/latest/configuring/ skip_list: [] + +exclude_paths: + - ./contrib + - ./.venv diff --git a/.editorconfig b/.editorconfig index 6854d6e..5c558df 100755 --- a/.editorconfig +++ b/.editorconfig @@ -8,5 +8,5 @@ indent_style = space indent_size = 4 trim_trailing_whitespace = true -[*.{yml,yaml,toml}] +[*.{yml,yaml,toml,py}] indent_size = 2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3aa676e..d809b6e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,37 +24,11 @@ jobs: - name: Check out the codebase uses: actions/checkout@v3 - - name: Setup python3 + - name: Setup python uses: actions/setup-python@v4 with: python-version: '3.9' - - name: Install poetry into system - uses: gi0baro/setup-poetry-bin@v1 - - - name: Install dependencies - run: make install-deps - - - name: Run linting tasks - run: make lint - - integration-macos-native: - name: Integration on macOS (native) - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: ["macos-12"] - steps: - - name: Check out the codebase - uses: actions/checkout@v3 - - # Uncomment for debugging over SSH - # - name: Setup upterm.dev session for SSH debugging - # uses: lhotari/action-upterm@v1 - - - name: Update built-in macOS pip - run: make update-pip - - name: Install poetry into system uses: gi0baro/setup-poetry-bin@v1 with: @@ -65,49 +39,46 @@ jobs: uses: actions/cache@v3 with: path: .venv - key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }} - - - name: Install dependencies (cached) - if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' - run: poetry install --no-interaction --no-root - env: - PY_COLORS: '1' + key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-${{ hashFiles('**/requirements.yml') }} - name: Install dependencies + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' run: make install-deps - - name: Test the playbook - run: make test - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Idempotence check - run: | - IDEMPOTENCE=$(mktemp) - cd tests && poetry run ansible-playbook test.yml | tee -a ${IDEMPOTENCE} - tail ${IDEMPOTENCE} \ - | grep -q 'changed=0.*failed=0' \ - && (echo 'Idempotence test: pass' && exit 0) \ - || (echo 'Idempotence test: fail' && exit 1) - env: - PY_COLORS: '1' - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Run linting tasks + run: make lint - integration-macos-molecule: - name: Integration check on macOS (molecule) + integration-linux-molecule: + name: Integration on Linux runs-on: ${{ matrix.os }} strategy: max-parallel: 4 matrix: - os: ["macos-12"] + os: ["ubuntu-latest"] python-version: ["3.9"] - scenario: ["default-macos-on-localhost"] + scenario: ["default"] + steps: - name: Check out the codebase uses: actions/checkout@v3 + with: + submodules: true + + - name: Check out the actions/cache source + uses: actions/checkout@v3 + with: + repository: actions/cache + ref: v3 + path: .tmp/actions/cache + + - name: Check out fix - make actions/cache@v3 run always, not only when job succeeds + run: | + sed -i -e '/ post-if: /d' .tmp/actions/cache/action.yml - - name: Update built-in macOS pip - run: make update-pip + - name: Setup python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} - name: Install poetry into system uses: gi0baro/setup-poetry-bin@v1 @@ -116,45 +87,47 @@ jobs: - name: Load cached .venv id: cached-poetry-dependencies - uses: actions/cache@v3 + uses: ./.tmp/actions/cache with: path: .venv - key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }} - - - name: Install dependencies (cached) - if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' - run: poetry install --no-interaction --no-root - env: - PY_COLORS: '1' + key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-${{ hashFiles('**/requirements.yml') }} - name: Install dependencies + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' run: make install-deps - - name: Run molecule tests - run: poetry run molecule test --scenario-name "${SCENARIO}" -- -v + - name: Test the playbook with molecule + run: poetry run molecule test --scenario-name "${SCENARIO}" -- -vvv --become env: SCENARIO: ${{ matrix.scenario }} PY_COLORS: '1' - ANSIBLE_FORCE_COLOR: '1' GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - integration-linux-molecule: + integration-macos-molecule: name: Integration on Linux runs-on: ${{ matrix.os }} strategy: max-parallel: 4 matrix: - os: ["ubuntu-latest"] - python-version: ["3.9"] - scenario: ["default"] + os: ["macos-12"] + scenario: ["default-macos-on-localhost"] + steps: - name: Check out the codebase uses: actions/checkout@v3 + with: + submodules: true - - name: Setup python3 - uses: actions/setup-python@v4 + - name: Check out the actions/cache source + uses: actions/checkout@v3 with: - python-version: ${{ matrix.python-version }} + repository: actions/cache + ref: v3 + path: .tmp/actions/cache + + - name: Check out fix - make actions/cache@v3 run always, not only when job succeeds + run: | + sed -i -e '/ post-if: /d' .tmp/actions/cache/action.yml - name: Install poetry into system uses: gi0baro/setup-poetry-bin@v1 @@ -163,35 +136,28 @@ jobs: - name: Load cached .venv id: cached-poetry-dependencies - uses: actions/cache@v3 + uses: ./.tmp/actions/cache with: path: .venv - key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }} - - - name: Install dependencies (cached) - if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' - run: poetry install --no-interaction --no-root - env: - PY_COLORS: '1' + key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-${{ hashFiles('**/requirements.yml') }} - name: Install dependencies + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' run: make install-deps - - name: Run molecule tests - run: poetry run molecule test --scenario-name "${SCENARIO}" -- -v + - name: Test the playbook with molecule + run: poetry run molecule test --scenario-name "${SCENARIO}" -- -vvv --become env: SCENARIO: ${{ matrix.scenario }} PY_COLORS: '1' - ANSIBLE_FORCE_COLOR: '1' GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} publish: if: github.event.inputs.shouldPublish == 'yes' || github.ref == 'refs/heads/master' needs: - lint - - integration-macos-native - - integration-macos-molecule - integration-linux-molecule + - integration-macos-molecule runs-on: ubuntu-latest steps: - name: Publish to Ansible Galaxy diff --git a/.later.yml b/.later.yml index b4a3a4d..d8a308a 100644 --- a/.later.yml +++ b/.later.yml @@ -12,6 +12,10 @@ ansible: # Global settings for all defined rules rules: # List of files to exclude - exclude_files: [] + exclude_files: + - ./contrib + - ./.venv + + version: "1.0" ... diff --git a/.yamllint b/.yamllint index cd1d2db..6c6a84d 100644 --- a/.yamllint +++ b/.yamllint @@ -4,6 +4,7 @@ extends: default ignore: | contrib + .venv # Overwrite above default rules rules: diff --git a/Makefile b/Makefile index 0cd640f..02abc2f 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,6 @@ ### export ANSIBLE_FORCE_COLOR = 1 -export ANSIBLE_JINJA2_NATIVE = true - export PY_COLORS = 1 export PYTHONIOENCODING = UTF-8 export LC_CTYPE = en_US.UTF-8 @@ -14,10 +12,7 @@ export LANG = en_US.UTF-8 # https://stackoverflow.com/questions/50009505/ansible-stdout-formatting export ANSIBLE_STDOUT_CALLBACK = unixy -TASK_TAGS ?= "tpl-install tpl-update" -PLAYBOOK ?= test.yml -WORKDIR ?= ./tests -INVENTORY ?= inventory.yml +TASK_TAGS ?= "tpl-install,tpl-update" REQS ?= requirements.yml INSTALL_POETRY ?= true POETRY_BIN ?= poetry @@ -32,44 +27,23 @@ PY_PATH ?= $(shell which python3) # -vvv - enable connection debugging DEBUG_VERBOSITY ?= -vvv -TEST_PLAYBOOK = $(POETRY_RUNNER) ansible-playbook $(PLAYBOOK) -i $(INVENTORY) $(DEBUG_VERBOSITY) -TEST_IDEMPOTENT = $(TEST_PLAYBOOK) | tee /dev/tty | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) - ### Lint yaml files -lint: check-syntax +lint: later $(POETRY_RUNNER) yamllint . - cd $(WORKDIR) && $(POETRY_RUNNER) ansible-lint $(PLAYBOOK) -c ../.ansible-lint + $(POETRY_RUNNER) ansible-lint . --force-color .PHONY: lint ### Run tests -test: - cd $(WORKDIR) && $(TEST_PLAYBOOK) -.PHONY: test - -test-idempotent: - cd $(WORKDIR) && $(TEST_IDEMPOTENT) -.PHONY: test-idempotent - -test-install: TASK_TAGS="tpl-install" -test-install: test-tag - -test-update: TASK_TAGS="tpl-update" -test-update: test-tag - -test-tag: - cd $(WORKDIR) && $(TEST_PLAYBOOK) --tags $(TASK_TAGS) -.PHONY: test-tag - m-local: - $(POETRY_RUNNER) molecule test --scenario-name default-macos-on-localhost -- -vvv --tags $(TASK_TAGS) + $(POETRY_RUNNER) molecule test --scenario-name default-macos-on-localhost -- $(DEBUG_VERBOSITY) --tags $(TASK_TAGS) .PHONY: m-local m-remote: - $(POETRY_RUNNER) molecule test --scenario-name default-macos-over-ssh -- -vvv --tags $(TASK_TAGS) + $(POETRY_RUNNER) molecule test --scenario-name default-macos-over-ssh -- $(DEBUG_VERBOSITY) --tags $(TASK_TAGS) .PHONY: m-remote m-linux: - $(POETRY_RUNNER) molecule test --scenario-name default -- -vvv --tags $(TASK_TAGS) + $(POETRY_RUNNER) run molecule test --scenario-name default -- $(DEBUG_VERBOSITY) --tags $(TASK_TAGS) .PHONY: m-linux login-mac: @@ -88,20 +62,6 @@ debug-version: $(POETRY_RUNNER) ansible --version .PHONY: debug-version -check: - cd $(WORKDIR) && $(TEST_PLAYBOOK) --check -.PHONY: check - -### List all hostnames -ls-host: - cd $(WORKDIR) && $(POETRY_RUNNER) ansible all -i $(INVENTORY) -m shell -a "hostname;" -.PHONY: ls-host - -### Check playbook syntax -check-syntax: - cd $(WORKDIR) && $(TEST_PLAYBOOK) --syntax-check -.PHONY: check-syntax - later: $(POETRY_RUNNER) $(ANSIBLE_LATER_BIN) **/*.yml .PHONY: later @@ -138,4 +98,4 @@ endif hooks: $(POETRY_RUNNER) pre-commit install $(POETRY_RUNNER) pre-commit autoupdate -.PHONY: hooks +.PHONY: install-hooks diff --git a/README.md b/README.md index fa39d5b..b5287a8 100644 --- a/README.md +++ b/README.md @@ -92,12 +92,6 @@ Repository uses default structure, sugested by [Ansible Documentation](https://d ├── 🗂 tasks │   └── 📄 main.yml ├── 🗂 templates -├── 🗂 tests -│   ├── 📄 ansible.cfg -│   ├── 🗂 defaults -│   │   └── 📄 main.yml -│   ├── 📄 inventory.yml -│   └── 📄 test.yml └── 🗂 vars └── 📄 main.yml ``` @@ -204,28 +198,21 @@ You can check `Makefile` to get full list of commands for remote and local testi > :warning: **Notice**: By defaut all tests are ran against your local machine! ```bash -# run all tags with scenario from ./tests/test.yml -$ make test - -# or test-tag without any parameters -$ make test-tag - -# run idempotency check -$ make test-idempotent - -# run tasks that validate config file and does installation -$ export TASK_TAGS="tpl-install tpl-update" -$ make test-tag - -# run by predefined command that executes only one tag -$ make test-install -$ make test-update - # run molecule tests on localhost $ poetry run molecule test --scenario-name default-macos-on-localhost -- -vvv # or with make command $ make m-local + +# choose which tags will be included +# run tasks that validate config file and does installation +$ export TASK_TAGS="tpl-install,tpl-update"; make m-local + +# runs molecule with docker driver +$ poetry run molecule test --scenario-name default -- -vvv + +# or with make file +$ make m-linux ```
diff --git a/meta/main.yml b/meta/main.yml index 412646b..54cab5f 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -9,7 +9,7 @@ galaxy_info: license: GPL-3.0-only - min_ansible_version: "2.12" + min_ansible_version: "2.13" platforms: - name: macOS diff --git a/molecule/_partials/playbooks/converge.yml b/molecule/_partials/playbooks/converge.yml index 5fd4fd6..ac535e9 100644 --- a/molecule/_partials/playbooks/converge.yml +++ b/molecule/_partials/playbooks/converge.yml @@ -12,5 +12,7 @@ tasks: - name: Molecule | Converge | Include setup tasks ansible.builtin.include_tasks: "../tasks/setup.yml" + tags: + - always ... diff --git a/molecule/_partials/playbooks/verify.yml b/molecule/_partials/playbooks/verify.yml index b910e9d..ce75888 100644 --- a/molecule/_partials/playbooks/verify.yml +++ b/molecule/_partials/playbooks/verify.yml @@ -12,5 +12,7 @@ tasks: - name: Molecule | Verify | Include setup tasks ansible.builtin.include_tasks: "../tasks/setup.yml" + tags: + - always ... diff --git a/molecule/_partials/tasks/setup.yml b/molecule/_partials/tasks/setup.yml index 8ba7162..5bfe4bd 100644 --- a/molecule/_partials/tasks/setup.yml +++ b/molecule/_partials/tasks/setup.yml @@ -2,19 +2,27 @@ - name: Molecule | Setup | Load default variables ansible.builtin.include_vars: "defaults/main.yml" + tags: + - always - name: Molecule | Setup | Include elliotweiser.osx-command-line-tools (macOS only) ansible.builtin.include_role: name: elliotweiser.osx-command-line-tools when: ansible_os_family == 'Darwin' + tags: + - always -- name: Include geerlingguy.git (Linux only) +- name: Molecule | Setup | Include geerlingguy.git (Linux only) ansible.builtin.include_role: name: geerlingguy.git when: ansible_system == 'Linux' + tags: + - always -- name: Include ansible-role-tpl +- name: Molecule | Setup | Include ansible-role-tpl ansible.builtin.include_role: name: ansible-role-tpl + tags: + - always ... diff --git a/tests/ansible.cfg b/molecule/default-macos-on-localhost/ansible.cfg similarity index 55% rename from tests/ansible.cfg rename to molecule/default-macos-on-localhost/ansible.cfg index 482bdad..07f1055 100644 --- a/tests/ansible.cfg +++ b/molecule/default-macos-on-localhost/ansible.cfg @@ -1,11 +1,7 @@ [defaults] # collections_paths = ../../../ -inventory = ./inventory.yml interpreter_python = auto_silent ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S -# https://stackoverflow.com/questions/50009505/ansible-stdout-formatting stdout_callback = unixy -# Use the stdout_callback when running ad-hoc commands. bin_ansible_callbacks = true -jinja2_native = true force_color = true diff --git a/molecule/default-macos-over-ssh/ansible.cfg b/molecule/default-macos-over-ssh/ansible.cfg new file mode 100644 index 0000000..07f1055 --- /dev/null +++ b/molecule/default-macos-over-ssh/ansible.cfg @@ -0,0 +1,7 @@ +[defaults] +# collections_paths = ../../../ +interpreter_python = auto_silent +ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S +stdout_callback = unixy +bin_ansible_callbacks = true +force_color = true diff --git a/molecule/default/ansible.cfg b/molecule/default/ansible.cfg new file mode 100644 index 0000000..07f1055 --- /dev/null +++ b/molecule/default/ansible.cfg @@ -0,0 +1,7 @@ +[defaults] +# collections_paths = ../../../ +interpreter_python = auto_silent +ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S +stdout_callback = unixy +bin_ansible_callbacks = true +force_color = true diff --git a/tests/defaults/main.yml b/tests/defaults/main.yml deleted file mode 100644 index 072cbbf..0000000 --- a/tests/defaults/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- - -# If you need to totally override defaults, -# use this config and then in CI stage do cp with rewrite. - -... diff --git a/tests/inventory.yml b/tests/inventory.yml deleted file mode 100644 index becbd53..0000000 --- a/tests/inventory.yml +++ /dev/null @@ -1,10 +0,0 @@ -# Standards: 1.0 ---- - -local: - hosts: - localhost: - ansible_connection: local - host: 127.0.0.1 - -... diff --git a/tests/test.yml b/tests/test.yml deleted file mode 100644 index d763069..0000000 --- a/tests/test.yml +++ /dev/null @@ -1,10 +0,0 @@ -# Standards: 1.0 ---- - -- hosts: localhost - connection: local - - roles: - - ../../ - -... From 6e411a1ff4884b2a1e39cc3ca0f3c4ff94b66e45 Mon Sep 17 00:00:00 2001 From: lotyp Date: Wed, 29 Jun 2022 01:55:46 +0300 Subject: [PATCH 2/2] feat: refactored ci file, updates to pyproject --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d809b6e..2f8a4b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -104,7 +104,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} integration-macos-molecule: - name: Integration on Linux + name: Integration on macOS runs-on: ${{ matrix.os }} strategy: max-parallel: 4