Passer au contenu principal

Guide de script Ansible

Exécuter les playbooks Ansible sur les appareils gérés en utilisant Level

Introduction

Level peut exécuter des playbooks Ansible sur les appareils gérés via son moteur de script. La principale différence par rapport à une configuration Ansible typique : Level n'utilise pas de nœud de contrôle central. Chaque appareil télécharge et exécute le playbook localement, et Level diffuse la sortie vers votre console en temps réel.

Ce guide couvre le modèle d'exécution, comment créer un script Level qui appelleansible-playbook, et comment préparer les points de terminaison Windows qui nécessitent un environnement compatible POSIX pour exécuter Ansible.


⚙️ CONDITIONS PRÉALABLES

  • Ansible installé sur chaque appareil cible (pas de nœud de contrôle central — voir ci-dessous)

  • Pour les appareils Unix/macOS/Linux : ansible et ansible-playbook available in PATH

  • Pour les appareils Windows : un environnement compatible POSIX (WSL ou Cygwin/Git Bash) avec Ansible installé

  • Agent Level déployé sur tous les appareils cibles


Comment Level exécute les playbooks Ansible

Ansible standard utilise une machine de contrôle centrale pour SSH dans les hôtes cibles et exécuter les tâches à distance. Le modèle de Level est différent : chaque appareil exécute le playbook localement sur lui-même.

Lorsqu'un script Level contenant unansible-playbookla commande s'exécute sur un appareil :

  1. Le script (et tous les fichiers de playbook joints) sont téléchargés sur l'appareil.

  2. L'appareil exécuteansible-playbooklocalement en utilisant sa propre installation d'Ansible.

  3. L'agent Level capture stdout/stderr et le diffuse vers la console Level au fur et à mesure que le playbook s'exécute.

Cela signifie que chaque appareil doit avoir Ansible installé, pas seulement une machine centrale. Cela signifie également que les playbooks ciblant hosts: localhostou les types de connexion locaux fonctionnent mieux pour l'automatisation distribuée par Level.

REMARQUE : Playbooks that target remote hosts over SSH still work — the device acts as the Ansible control node for those remote connections. This is useful for cases where one managed device needs to reach others on its local network.Creating a Level Script for Ansible


The Level script is a shell wrapper that calls

. The playbook file itself is attached separately and downloaded to the device at runtime. ansible-playbookFor Linux and macOS

1. Go to

Automations → Scripts and click + New script. 2. Set the interpreter to

Shell (Bash). 3. Add the playbook invocation:

Bash

4. Attach the playbook file to the script or upload it to

#!/bin/bash
ansible-playbook /path/to/playbook.yml
if [ $? -ne 0 ]; then
echo "Playbook failed on $(hostname)" >&2
exit 1
fi

Automations → Files and use a Download File action to place it on the device before the script runs. CONSEIL :

Use during development to do a dry run that validates syntax and connectivity without making changes.For Windows (via WSL)ansible-playbook --checkSet the interpreter to

PowerShell

  1. .Call Ansible through WSL:PowerShell

  2. WSL must already be installed and configured on the device. See

Setting Up Ansible on Windows

wsl ansible-playbook /mnt/c/LevelPlaybooks/network_test.yml

below. For Windows (via Cygwin or Git Bash) PowerShell

Setting Up Ansible on Windows Endpoints

Because each device runs the playbook locally, Windows endpoints need a POSIX-compatible shell with Python and Ansible installed. Two options:

C:\cygwin64\bin\bash.exe -lc "ansible-playbook /cygdrive/c/LevelPlaybooks/playbook.yml"


Option A: Windows Subsystem for Linux (WSL)

1. In PowerShell as Administrator, run:

PowerShell

Reboot when prompted.

2. From the Ubuntu shell that opens after reboot:

wsl --install

Bash

3. Verify:

Bash

sudo apt update && sudo apt upgrade -y
sudo apt install -y python3 python3-pip
pip3 install ansible

4. In Level scripts, call Ansible via

from PowerShell.

ansible --version
ansible-playbook --version

Option B: Cygwin or Git Bash wsl ansible-playbook ...1. Install

Cygwin

or Git for Windows, selecting Python 3, pip, and OpenSSH during setup. 2. From the Cygwin or Git Bash shell: Bash

3. In Level scripts, call Ansible by invoking the Bash executable directly:

PowerShell

pip3 install ansible

AVERTISSEMENT :

Align Python and Ansible versions across all Windows endpoints. Version mismatches between endpoints cause inconsistent behavior that's hard to diagnose.

C:\cygwin64\bin\bash.exe -lc "ansible-playbook /path/to/playbook.yml"

Playbook Configuration for Local ExecutionWhen playbooks run locally on each device rather than from a central control node, a few configuration adjustments help:Use


with

for tasks that should run on the device itself:

YAMLhosts: localhost Add verbose flags connection: local in development to get detailed output in Level's console:

Bash

- name: Local configuration
hosts: localhost
connection: local
tasks:
- name: Check connectivity
ansible.builtin.ping:

Level streams all stdout/stderr output, so verbose mode shows task-by-task detail inline. Best Practices

Validate locally before deploying through Level.

ansible-playbook /path/to/playbook.yml -vv

Run


on a test device first. Syntax errors in the playbook surface immediately rather than halfway through a production run.

  • Exit non-zero on failure. Level marks a script run as failed only if the script exits with a non-zero code. The ansible-playbook playbook.yml --check check in the wrapper above does this automatically. Without it, a failed playbook can look like a success in Level's run history.

  • Keep playbooks idempotent. Ansible playbooks should produce the same result whether they run once or ten times. This makes them safe to re-run via Level without unintended side effects.if [ $? -ne 0 ] Use Level's file repository for playbooks.

  • Upload playbook files to

  • Automations → Files , then use a .ymlDownload File action before the script step to place them on each device. This keeps your playbooks version-controlled in Level and ensures every device gets the current version. FAQDo I need Ansible on every managed device, or just a central server? Every device that you want to run a playbook needs Ansible installed locally. Level doesn't SSH from a central control node — it distributes and runs the playbook on each device independently.


Can the playbook target other hosts over SSH from the managed device?

  • Yes. The managed device acts as the Ansible control node for any remote connections defined in the playbook. SSH keys and credentials need to be present on the managed device itself. My playbook runs fine locally but fails in Level. What should I check first?

  • Check the user context. Level runs scripts as on Linux/macOS and as

  • on Windows. If the playbook depends on a user home directory, PATH, or environment variable that's only set for a specific user, it won't find those resources. Use absolute paths and explicit environment setup in the playbook. How do I pass variables into the playbook at runtime?root Use the SYSTEM flag in the

  • call. Level's system variables and automation variables can be passed in using the picker in the script editor. For example: --extra-vars Can I schedule the playbook to run on a recurring basis?ansible-playbook Yes. Attach the script to an automation with a Scheduled trigger. Set the schedule, add a Download File action for the playbook if it's stored in Level's file repository, then add the Run Script action. The automation runs the playbook on every device that matches the trigger conditions.{x} picker in the script editor. For example:
    ansible-playbook /path/to/playbook.yml --extra-vars "hostname=##{{level_device_name}}"

  • Can I schedule the playbook to run on a recurring basis? Yes. Attach the script to an automation with a Scheduled trigger. Set the schedule, add a Download File action for the playbook if it's stored in Level's file repository, then add the Run Script action. The automation runs the playbook on every device that matches the trigger conditions.

Avez-vous trouvé la réponse à votre question ?