Skip to main content

Learn any cloudAnsible LearnLabsAnsible syntax check, step by step

Ansible syntax check, step by step

WorkspacesUnknown

Run ansible-playbook --syntax-check over the playbook that configures the Hybrid Lab host itself, see it fail until the collections it needs are installed, break a play and read what the check reports, and learn what a syntax check is not — with no host in reach and nothing executed.

Introductoryabout 20 minutesToolsansible

Opening your lab workspace…

Steps

  1. Step 1: Read the layout

    The folder is the configuration management for the Hybrid Lab host. ansible.cfg points at inventory/localhost.yml and roles/; site.yml runs the roles in order; requirements.yml pins the collections they use; each role’s meta/argument_specs.yml is its variable contract. Read site.yml top to bottom before running anything.

  2. Step 2: Run the syntax check, and watch it fail

    ansible-playbook --syntax-check site.yml
    

    The image carries ansible-core and no collections, so the first role that names a module outside ansible.builtin stops the check with "couldn’t resolve module/action". A syntax check resolves every module it meets; a missing collection is a syntax error to it.

  3. Step 3: Install the pinned collections

    ansible-galaxy collection install -r requirements.yml
    ansible-playbook --syntax-check site.yml
    

    The versions are exact, so you install the same code the host runs. The check now ends with playbook: site.yml and exit code 0.

  4. Step 4: Break a play

    In site.yml, rename pre_tasks: to pre_taskz: and run the check again. It reports that pre_taskz is not a valid attribute for a Play, with the file and line. Then open any roles/*/tasks/main.yml, misspell a module name (ansible.builtin.fil), and see the same kind of message name the task instead. Put both back:

    git checkout -- .
    ansible-playbook --syntax-check site.yml
    
  5. Step 5: What a syntax check is not

    It parses and resolves; it does not connect, gather facts, evaluate conditions or run a single module. --check mode would need a host to talk to, and a lint (ansible-lint) is a separate tool this image does not carry — run it on your own machine if you want style rules. To see the runner apply the same check to a playbook of your own, send one that uses ansible.builtin modules only, since the runner installs no collections.

    Checked by the agent (ansible-check): Paste one playbook as the text payload, ansible.builtin modules only: the runner has ansible-core and no collections, so anything else fails to resolve, exactly as step 2 did here.