Write your first automation task using the VS Code extension

Learn how to write, inspect, debug, and run Ansible playbooks directly within VS Code using Ansible development tools.

Set up the Ansible configuration file

When you scaffolded your playbook project, an Ansible configuration file, ansible.cfg, was added to the root directory of your project.

Procedure

If you have configured a default Ansible configuration file in /etc/ansible/ansible.cfg, copy any settings that you want to reuse in your project from your default Ansible configuration file to the ansible.cfg file in your project’s root directory.

To learn more about the Ansible configuration file, see Reviewing your Ansible configuration with automation content navigator.

Write your first playbook

Create your first Ansible playbook within VS Code using the Ansible extension. The tools available help ensure that your syntax is correct and ready to run.

Before you begin

  • You have installed and opened the Ansible VS Code extension.
  • You have opened a terminal in VS Code.
  • You have installed ansible-devtools.

Procedure

  1. Create a new .yml file in VS Code for your playbook, for example example_playbook.yml. Put it in the same directory level as the example site.yml file.
  2. Add the following example code into the playbook file and save the file. The playbook consists of a single play that executes a ping to your local machine.
    ---
    - name: My first play
      hosts: localhost
      tasks:
        - name: Ping my hosts
          ansible.builtin.ping:

    Ansible-lint runs in the background and displays errors in the Problems tab of the terminal. There are no errors in this playbook:


    Ansible-lint showing no errors in a playbook
  3. If you want to add new content to the playbook, use the following rules:
    • Every playbook file must finish with a blank line.
    • Trailing spaces at the end of lines are not allowed.
    • Every playbook and every play require an identifier (name).
  4. Save your playbook file.

Inspect your playbook

The Ansible VS Code extension provides inline help, syntax highlighting, and assists you with indentation in .yml files.

Procedure

  1. Open a playbook in VS Code.
  2. Hover your mouse over a keyword or a module name: the Ansible extension provides documentation:
    Ansible-lint showing no errors in a playbook
  3. If you begin to type the name of a module, for example ansible.builtin.ping, the extension provides a list of suggestions.

    Select one of the suggestions to autocomplete the line.


    Ansible-lint showing no errors in a playbook

Debug your playbook

Learn how to use VS Code to identify and understand error messages in playbooks.

Procedure

  1. The following playbook contains multiple errors. Copy and paste it into a new file in VS Code.
    - name:
      hosts: localhost
      tasks:
       - name:
         ansible.builtin.ping:

    The errors are indicated with a wavy underline in VS Code.

  2. Hover your mouse over an error to view the details:
    Popup message explaining a playbook error
  3. Playbook files that contain errors are indicated with a number in the Explorer pane.
  4. Select the Problems tab of the VS Code terminal to view a list of the errors.
    Playbook errors shown in Problems tab and explorer list

    $[0].tasks[0].name None is not of type 'string' indicates that the playbook does not have a label.