Jamie's Blog

Ruby developer. CTO. Swimmer. Always trying to write more

Ansible syntax highlighting for Sublime

2014 08 05 at 17.34

If you’ve done any work in Ansible you’ll know that it uses YAML files for the bulk on the configuration. Whilst you can just make do with a standard YAML syntax highlighting, Ansible uses variables inside lines and Jinja template tags that it would be nice to visually highlight.

First, you’ll need to install the Ansible syntax highlighting package.

Now, you should be able to select the syntax highlighting as ‘Ansible’ in the editor right-hand corner

But, wait…

Why aren’t my .yml files being automatically marked as Ansible syntax files? Well, because they’re just YAML files!

To force them to be highlighted as Ansible, you’ll need to install the ApplySyntax plugin which gives you really fine-grained control over how files are highlighted based on their path or first line. Now, in your ApplySyntax user preferences, paste this:

    // If you want exceptions reraised so you can see them in the console, change this to true.
    "reraise_exceptions": false,

    // If you want to have a syntax applied when new files are created, set new_file_syntax to the name of the syntax to use.
    // The format is exactly the same as "name" in the rules below. For example, if you want to have a new file use
    // JavaScript syntax, set new_file_syntax to 'JavaScript'.
    "new_file_syntax": false,

    // Put your custom syntax rules here:
    "syntaxes": [
        "name": "Ansible/Ansible",
        "rules": [
          {"file_name": ".*/tasks/.*.yml$"},
          {"file_name": ".*/handler/.*.yml$"},
          {"file_name": ".*/vars/.*.yml$"},
          {"file_name": ".*/roles/.*.yml$"},
          {"file_name": ".*/.*ansible.*/.*.yml$"}

This should highlight any Ansible .yml files in directories with ‘ansible’ in their name, or any of the common directories ‘roles’, ‘tasks’, ‘handlers’ etc used in Ansible projects.


 Before: highlighted as YAML


 After: highlighted as Ansible