diff options
| author | Ahmed Abdelhalim <[email protected]> | 2025-09-03 22:38:38 +0200 |
|---|---|---|
| committer | Ahmed Abdelhalim <[email protected]> | 2025-09-03 22:38:38 +0200 |
| commit | 97e4317089d29ad1e74daf6a82b05068440f0aa1 (patch) | |
| tree | bf0fe523ec45f9d6cf81eed2a4106ff712e600b1 | |
| parent | 82b90471672d36d3a88d04c181c523c701820eb9 (diff) | |
Add ansible role
| -rw-r--r-- | molecule/default/converge.yml | 1 | ||||
| -rw-r--r-- | roles/ansible/README.md | 101 | ||||
| -rw-r--r-- | roles/ansible/defaults/main.yml | 8 | ||||
| -rw-r--r-- | roles/ansible/meta/argument_specs.yml | 22 | ||||
| -rw-r--r-- | roles/ansible/meta/main.yml | 19 | ||||
| -rw-r--r-- | roles/ansible/tasks/main.yml | 65 | ||||
| -rw-r--r-- | roles/python/tasks/main.yml | 6 | ||||
| -rw-r--r-- | site.yml | 1 |
8 files changed, 223 insertions, 0 deletions
diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 039fc920..c435c56d 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -43,6 +43,7 @@ - role: "go" - role: "docker" - role: "podman" + - role: "ansible" - role: "wireguard" - role: "wireguard_gateway" - role: "pihole" diff --git a/roles/ansible/README.md b/roles/ansible/README.md new file mode 100644 index 00000000..8e6f3a6e --- /dev/null +++ b/roles/ansible/README.md @@ -0,0 +1,101 @@ +# Ansible Role + +This role manages multiple versions of ansible-core and molecule using pipx, +allowing you to install different versions side-by-side and switch between them easily. + +## Variables + +### Required Variables + +**Ansible-core:** + +- `ansible_core_versions`: List of ansible-core versions to install +- `ansible_core_default_version`: The version to use as default (must be in the versions list) + +**Molecule:** + +- `molecule_versions`: List of molecule versions to install +- `molecule_default_version`: The version to use as default (must be in the versions list) + +### Example Configuration + +```yaml +ansible_core_versions: + - "2.18.7" +ansible_core_default_version: "2.18.7" + +molecule_versions: + - "25.7.0" +molecule_default_version: "25.7.0" +``` + +## Usage + +### Installed Commands + +After running this role, you'll have: + +**Ansible version-specific commands:** + +- `ansible2.18.7`, `ansible-playbook2.18.7`, `ansible-galaxy2.18.7`, etc. + +**Molecule version-specific commands:** + +- `molecule25.7.0` (includes docker and podman drivers via molecule-plugins) + +**Default commands (symlinked to default version):** + +- `ansible` → `ansible2.18.7` +- `ansible-playbook` → `ansible-playbook2.18.7` +- `ansible-galaxy` → `ansible-galaxy2.18.7` +- `molecule` → `molecule25.7.0` +- And all other ansible commands + +### Switching Default Version + +To switch the default version: + +1. Update `ansible_core_default_version` and/or `molecule_default_version` in your variables +2. Re-run the playbook + +The symlinks will be updated to point to the new default versions. + +## Manual Cleanup + +This role does not automatically remove unused versions. To manually clean up old installations: + +### List installed versions + +```bash +pipx list --short +``` + +### Remove specific versions + +```bash +# Remove ansible-core versions +pipx uninstall ansible-core2.18.0 + +# Remove molecule versions +pipx uninstall molecule25.7.0 +``` + +## Requirements + +- pipx must be installed and available at `/home/linuxbrew/.linuxbrew/bin/pipx` +- The `community.general` Ansible collection for the pipx module + +These requirements are satisfied with the `homebrew` and `python` roles (added as dependencies) + +## Example Playbook + +```yaml +- hosts: all + roles: + - role: ansible + vars: + ansible_core_versions: + - "2.18.7" + - "2.19.1" + ansible_core_default_version: "2.19.1" +``` diff --git a/roles/ansible/defaults/main.yml b/roles/ansible/defaults/main.yml new file mode 100644 index 00000000..75b19f99 --- /dev/null +++ b/roles/ansible/defaults/main.yml @@ -0,0 +1,8 @@ +--- +ansible_core_default_version: "2.18.7" +ansible_core_versions: + - "2.18.7" + +molecule_default_version: "25.7.0" +molecule_versions: + - "25.7.0" diff --git a/roles/ansible/meta/argument_specs.yml b/roles/ansible/meta/argument_specs.yml new file mode 100644 index 00000000..6f6b6ee8 --- /dev/null +++ b/roles/ansible/meta/argument_specs.yml @@ -0,0 +1,22 @@ +--- +argument_specs: + main: + options: + ansible_core_default_version: + type: "str" + description: "The default ansible-core version to use (must be in major.minor.patch format)" + default: "2.18.7" + ansible_core_versions: + type: "list" + elements: "str" + description: "List of ansible-core versions to install (must be in major.minor.patch format)" + default: ["2.18.7"] + molecule_default_version: + type: "str" + description: "The default molecule version to use (must be in major.minor.patch format)" + default: "25.7.0" + molecule_versions: + type: "list" + elements: "str" + description: "List of molecule versions to install (must be in major.minor.patch format)" + default: ["25.7.0"] diff --git a/roles/ansible/meta/main.yml b/roles/ansible/meta/main.yml new file mode 100644 index 00000000..2b3e00f9 --- /dev/null +++ b/roles/ansible/meta/main.yml @@ -0,0 +1,19 @@ +--- +dependencies: + - role: "python" +galaxy_info: + author: "a14m" + description: "Install ansible and molecule on Linux distributions" + company: "kartoffeln.work GmbH." + license: "MIT" + min_ansible_version: "2.18" + platforms: + - name: "ArchLinux" + versions: + - "all" + - name: "Ubuntu" + versions: + - "noble" + - name: "Debian" + versions: + - "bookworm" diff --git a/roles/ansible/tasks/main.yml b/roles/ansible/tasks/main.yml new file mode 100644 index 00000000..fe7611ae --- /dev/null +++ b/roles/ansible/tasks/main.yml @@ -0,0 +1,65 @@ +--- +- name: "Validate version format" + ansible.builtin.assert: + that: + - item | regex_search('^[0-9]+\\.[0-9]+\\.[0-9]+$') + msg: "Version '{{ item }}' must be in major.minor.patch format" + with_items: + - "{{ (ansible_core_versions + [ansible_core_default_version]) | unique }}" + - "{{ (molecule_versions + [molecule_default_version]) | unique }}" + +- name: "Install ansible core versions with suffixes" + community.general.pipx: + name: "ansible-core=={{ item }}" + state: "present" + executable: "/home/linuxbrew/.linuxbrew/bin/pipx" + suffix: "{{ item }}" + with_items: "{{ (ansible_core_versions + [ansible_core_default_version]) | unique }}" + # NOTE: pipx module always report the task changed even if it's not. + tags: + - molecule-idempotence-notest + +- name: "Install molecule versions with suffixes" + community.general.pipx: + name: "molecule=={{ item }}" + state: "present" + executable: "/home/linuxbrew/.linuxbrew/bin/pipx" + suffix: "{{ item }}" + with_items: "{{ (molecule_versions + [molecule_default_version]) | unique }}" + # NOTE: pipx module always report the task changed even if it's not. + tags: + - molecule-idempotence-notest + +# NOTE: the default pipx module inject_packages doesn't work with suffix +- name: "Inject molecule-plugins with docker and podman drivers" + ansible.builtin.command: + cmd: "/home/linuxbrew/.linuxbrew/bin/pipx inject molecule{{ item }} 'molecule-plugins[docker,podman]'" + with_items: "{{ (molecule_versions + [molecule_default_version]) | unique }}" + register: pipx_inject_result + changed_when: "'injected package' in pipx_inject_result.stdout" + failed_when: pipx_inject_result.rc != 0 and 'already exists' not in pipx_inject_result.stderr + +- name: "Create symlinks for default ansible version" + ansible.builtin.file: + src: "{{ ansible_user_dir }}/.local/bin/{{ item }}{{ ansible_core_default_version }}" + dest: "{{ ansible_user_dir }}/.local/bin/{{ item }}" + state: "link" + force: true + with_items: + - "ansible" + - "ansible-playbook" + - "ansible-galaxy" + - "ansible-config" + - "ansible-console" + - "ansible-doc" + - "ansible-inventory" + - "ansible-pull" + - "ansible-test" + - "ansible-vault" + +- name: "Create symlinks for default molecule version" + ansible.builtin.file: + src: "{{ ansible_user_dir }}/.local/bin/molecule{{ molecule_default_version }}" + dest: "{{ ansible_user_dir }}/.local/bin/molecule" + state: "link" + force: true diff --git a/roles/python/tasks/main.yml b/roles/python/tasks/main.yml index 45451561..686e99a9 100644 --- a/roles/python/tasks/main.yml +++ b/roles/python/tasks/main.yml @@ -75,3 +75,9 @@ changed_when: true tags: - molecule-idempotence-notest + +- name: "Install packaging for community.general.pipx version installs" + become: true + ansible.builtin.package: + name: "python-packaging" + state: "present" @@ -35,6 +35,7 @@ - role: "go" - role: "docker" - role: "podman" + - role: "ansible" - name: "Raspberry Pi configure" gather_facts: true |
