ericsysmin / ansible-collection-system

Collection of Roles for managing Ubuntu/Debian/CentOS systems
MIT License
33 stars 22 forks source link

Regarding Ansible Playbook Testing for Windows, Linux, Mac #17

Open mavsravikiran opened 3 days ago

mavsravikiran commented 3 days ago

Description

Scripting

For WSL

---
- name: WSL - Add Maven plugin in asdf
  shell: "bash -lic 'source {{ wsl_bashrc_path }} && asdf plugin add maven'"
  become: true
  become_user: "{{ ansible_env.SUDO_USER }}"
  become_method: su
  retries: 5
  delay: 10
  register: result
  until: result is success
  failed_when: result.rc != 0 and result.rc != 2

- name: WSL - Install Maven
  shell: "bash -lic 'source {{ wsl_bashrc_path }} && asdf install maven {{ maven_version }}'"
  become: true
  become_user: "{{ ansible_env.SUDO_USER }}"
  become_method: su
  retries: 5
  delay: 10
  register: result
  until: result is success

- name: WSL - Set global maven version
  shell: "bash -lic 'source {{ wsl_bashrc_path }} && asdf global maven {{ maven_version }}'"
  become: true
  become_user: "{{ ansible_env.SUDO_USER }}"
  become_method: su

- name: WSL - Copy Maven settings.xml file
  copy:
    src: settings.xml
    dest: "{{ maven_wsl_settings_path }}"
    mode: "644"
  become: true
  become_user: "{{ ansible_env.SUDO_USER }}"
  become_method: su

Molecule Script for Maven Role

Code for test_default.py

import os
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')

def test_maven(host):
    # Check if Maven is installed
    cmd = host.run("mvn --version")
    assert "Apache Maven" in cmd.stdout

    # Check if Maven's settings.xml file exists
    f = host.file('/usr/share/maven/conf/settings.xml')
    assert f.exists
    assert f.user == 'root'
    assert f.group == 'root'

Code for Converge.yml

- name: Converge
  hosts: all
  # become: yes
  roles:
    - role: maven

Code for Verify.yml

---
- name: Verify Maven role
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Install Maven
      include_role:
        name: maven

    - name: Verify Maven installation
      command: mvn --version
      register: result

    - name: Assert Maven is installed
      assert:
        that:
          - "'Apache Maven' in result.stdout"
          - "'Maven home' in result.stdout"
        success_msg: "Maven is installed"
        fail_msg: "Maven is not installed"

Code for Molecule.yml

---
dependency:
  name: galaxy
driver:
  name: docker
lint: |
  set -e
  yamllint .
  ansible-lint
platforms:
  - name: instance
    image: ${MOLECULE_DISTRO:-ubuntu-xenial}
    command: ${MOLECULE_COMMAND:-""}
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:ro
    privileged: true
    pre_build_image: true
provisioner:
  name: ansible
  playbooks:
    converge: ${MOLECULE_PLAYBOOK:-converge.yml}

Github Workflow Script

name: "Test"
on:
  workflow_dispatch:
    permissions:
    contents: read
    push:
      branches:
        - main
        paths:
          - "./roles/maven/**"
          - "./molecule/maven/**"
          - ".github/workflows/test.yml"
    pull_request:
    paths:
      - "./roles/maven/**"
      - "./molecule/maven/**"
      - ".github/workflows/test.yml"
jobs:
  molecule:
    runs-on: ubuntu-latest
    env:
      PY_COLORS: 1
      ANSIBLE_FORCE_COLOR: 1
      ANSIBLE_ROLES_PATH: ./roles
    strategy:
      # fail-fast: true
      matrix:
        molecule_distro:
          - { "distro": "centos-7", "command": "/usr/sbin/init" }
          - { "distro":"centos-8", "command":"/usr/sbin/init" }
          - { "distro":"fedora-32", "command":"/usr/sbin/init" }
          - { "distro":"fedora-31", "command":"/usr/sbin/init" }
          - { "distro":"fedora-30", "command":"/usr/lib/systemd/systemd" }
          - { "distro": "ubuntu-16.04", "command": "/sbin/init" }
          - { "distro": "ubuntu-24.04", "command": "/lib/systemd/systemd" }
          - { "distro":"ubuntu-20.04", "command":"/lib/systemd/systemd" }
          - { "distro":"debian-9", "command":"/lib/systemd/systemd" }
          - { "distro": "debian-10", "command": "/lib/systemd/systemd" }

        collection_role:
          - maven
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Set up Python 3.
        uses: actions/setup-python@v2
        with:
          python-version: "3.x"

      - name: Install dependencies
        run: |
          sudo apt install apt-transport-https ca-certificates curl software-properties-common
          curl -fsSL https://get.docker.com -o get-docker.sh
          sudo sh get-docker.sh
          python -m pip install --upgrade pip
          pip install ansible molecule yamllint ansible-lint molecule-plugins[docker]

      - name: Get Maven Role
        run: ls -la ./roles

      - name: Run role tests
        run: >-
          molecule --version &&
          ansible --version &&
          MOLECULE_COMMAND=${{ matrix.molecule_distro.command }}
          MOLECULE_DISTRO=${{ matrix.molecule_distro.distro }}
          molecule --debug test -s ${{ matrix.collection_role }}

Error Message

Requirement

Ask / Help

Please kindly help on this and quick support is much Appreciated 👍

mavsravikiran commented 2 days ago

@ericsysmin / @Fishy78 / @rockhowse ---- Please kindly take a look once you got some free time and quick support is Appreciated 👍