Regarding Ansible Playbook Testing for Windows, Linux, Mac
Opened this issue ยท 5 comments
Description
- I am using ansible automation to install softwares like maven, node ,java, Terraform etc in both Windows and WSL(Linux)
- We are planning some automated testing for Windows, WSL and even for MacOS using Github Actions
Scripting
- Actually i am trying to implement for one of the sample role
maven
and below are the code blocks for Reference
For Windows
- name: Windows - Install Maven
command: "{{ powershell_path }} {{ windows_environment_variables }} choco upgrade {{ item.name }} --version {{ item.version }} --no-progress --allow-downgrade -y"
retries: 5
delay: 10
register: result
until: result is success
loop:
- { name: maven, version: "{{ maven_version }}" }
- name: Windows - Copy Maven settings.xml file
copy:
src: settings.xml
dest: "{{ maven_windows_settings_path }}"
mode: "644"
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
-
I have a defined a
maven
role underroles
folder at the root of the project. -
Below is the directory structure for the molecule code
-Below are the code blocks i am Attaching as Reference for all the yml files which i had mentioned in the directory structure -
Even Attaching the Github Workflow File as Reference
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
- While running the workflow below is the error message
TASK [Create molecule instance(s)] *********************************************
changed: [localhost] => (item=instance)
TASK [Wait for instance(s) creation to complete] *******************************
failed: [localhost] (item={'failed': 0, 'started': 1, 'finished': 0, 'ansible_job_id': 'j5861[197](https://github.com/principalinformationservices-emu/pfg-engineer-workstation-setup/actions/runs/9745717261/job/26894343520#step:6:202)84582.3634', 'results_file': '/home/runner/.ansible_async/j586119784582.3634', 'changed': True, 'item': {'command': '/sbin/init', 'image': 'ubuntu-16.04', 'name': 'instance', 'pre_build_image': True, 'privileged': True, 'volumes': ['/sys/fs/cgroup:/sys/fs/cgroup:ro']}, 'ansible_loop_var': 'item'}) => {"ansible_job_id": "j586119784582.3634", "ansible_loop_var": "item", "attempts": 2, "changed": false, "finished": 1, "item": {"ansible_job_id": "j586119784582.3634", "ansible_loop_var": "item", "changed": true, "failed": 0, "finished": 0, "item": {"command": "/sbin/init", "image": "ubuntu-16.04", "name": "instance", "pre_build_image": true, "privileged": true, "volumes": ["/sys/fs/cgroup:/sys/fs/cgroup:ro"]}, "results_file": "/home/runner/.ansible_async/j586119784582.3634", "started": 1}, "msg": "Error pulling image ubuntu-16.04:latest - 404 Client Error for http+docker://localhost/v1.46/images/create?tag=latest&fromImage=ubuntu-16.04: Not Found (\"pull access denied for ubuntu-16.04, repository does not exist or may require 'docker login': denied: requested access to the resource is denied\")", "results_file": "/home/runner/.ansible_async/j586119784582.3634", "started": 1, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
FAILED - RETRYING: [localhost]: Wait for instance(s) creation to complete (300 retries left).
Requirement
- We are planning to have some testing for role level using molecule for the Windows, WSL, Macos using Github Actions
- Using Molecule how can we have the script at role level. need script for that would be more helpful
Ask / Help
- I had tried to keep this as reference but i am facing some issue while running
workflow
file. - If that is possible need some script how to do at each role and Need to define Github Actions Workflow to include all the roles for testing the workflow
Please kindly help on this and quick support is much Appreciated ๐
@ericsysmin / @Fishy78 / @rockhowse ---- Please kindly take a look once you got some free time and quick support is Appreciated ๐
Hello all,
Quick help is much Appreciated ๐ฏ
@mavsravikiran That has nothing to do with this project, but ubuntu 16.04 is end of life since 2021 and does not exists anymore.
Thanks for the quick Revert ๐
@Fishy78 --- How Can i use this same github workflow for Windows , Linux , MacOS could you please guide me on this ?
@mavsravikiran Again, this issue is not part of this project, and you should not create issues on projects, which are not affected...
and again: ubuntu-xenial alias ubuntu 16.04 is end of life since 2021 and will not exist as docker image.