Chapter 5. Writing and running a playbook with Ansible development tools


Learn to write and configure an Ansible playbook using the VS Code extension.

5.1. Setting up the Ansible configuration file for your playbook project

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

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 Ansible Configuration Settings in the Ansible documentation.

5.2. Writing your first playbook

Ansible development tools help you to create and run playbooks in VS Code.

Prerequisites

  • 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:
    Copy to Clipboard Toggle word wrap

    Ansible-lint runs in the background and displays errors in the Problems tab of the terminal. There are no errors in this 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.

5.3. Inspecting your playbook

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

  1. Open a playbook in VS Code.
  2. Hover your mouse over a keyword or a module name: the Ansible extension provides documentation:

  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.

5.4. Debugging your playbook

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

  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:
    Copy to Clipboard Toggle word wrap

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

  2. Hover your mouse over an error to view the details:

  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.

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

5.5. Running your playbook

The Ansible VS Code extension provides two options to run your playbook:

  • ansible-playbook runs the playbook on your local machine using Ansible Core.
  • ansible-navigator runs the playbook in an execution environment in the same manner that Ansible Automation Platform runs an automation job. You specify the base image for the execution environment in the Ansible extension settings.

5.5.1. Running your playbook with ansible-playbook

You can run your Ansible playbook locally by using the ansible-playbook command directly within the VS Code extension.

Procedure

  • To run a playbook, right-click the playbook name in the Explorer pane, then select Run Ansible Playbook via Run playbook via ansible-playbook.

    Run playbook via ansible-playbook

    The output is displayed in the Terminal tab of the VS Code terminal. The ok=2 and failed=0 messages indicate that the playbook ran successfully.

    Success message for ansible-playbook execution

5.5.2. Running your playbook with ansible-navigator

You can run an Ansible playbook through ansible-navigator by right-clicking the playbook name in the Explorer pane. This procedure explains how to view the playbook’s output and navigate the results for each play and task within the terminal.

Prerequisites

  • In the Ansible extension settings, enable the use of an execution environment in Ansible Execution Environment > Enabled.
  • Enter the path or URL for the execution environment image in Ansible > Execution Environment: Image.

Procedure

  1. To run a playbook, right-click the playbook name in the Explorer pane, then select Run Ansible Playbook via Run playbook via ansible-navigator run.
  2. View the output in the Terminal tab of the VS Code terminal. The Successful status indicates that the playbook ran successfully.

    Output for ansible-navigator execution

  3. Enter the number next to a play to step into the play results. The example playbook only contains one play. Enter 0 to view the status of the tasks executed in the play.

    Tasks in ansible-navigator output

    Type the number next to a task to review the task results.

5.5.3. Working with execution environments

You can view the automation execution environments provided by Red Hat in the Red Hat Ecosystem Catalog.

Procedure

  1. Click on an execution environment for information on how to download it.
  2. Log in to registry.redhat.io if you have not already done so.

    Note

    If you are running Ansible development tools on a container inside VS Code and you want to pull execution environments or the devcontainer to use as an execution environment, you must log in to registry.redhat.io from a terminal prompt within the devcontainer inside VS Code.

  3. Using the information in the Red Hat Ecosystem Catalog, download the execution environment you need.

    For example, to download the minimal RHEL 8 base image, run the following command:

    $ podman pull registry.redhat.io/ansible-automation-platform-25/ee-minimal-rhel9
    Copy to Clipboard Toggle word wrap

    You can build and create custom execution environments with ansible-builder. For more information about working with execution environments locally, see Creating and using execution environments.

  4. After customizing your execution environment, you can push your new image to the container registry in automation hub. See Publishing an automation execution environment in the Creating and using execution environments documentation.

5.6. Testing your playbooks

To test your playbooks in your project, run them in a non-production environment such as a lab setup or a virtual machine.

Automation content navigator (ansible-navigator) is a text-based user interface (TUI) for developing and troubleshooting Ansible content with execution environments.

Running a playbook using ansible-navigator generates verbose output that you can inspect to check whether the playbook is running the way you expected. You can specify the execution environment that you want to run your playbooks on, so that your tests replicate the production setup on Ansible Automation Platform:

Procedure

  • To run a playbook on an execution environment, run the following command from the terminal in VS Code:

    $ ansible-navigator run <playbook_name.yml> -eei <execution_environment_name>
    Copy to Clipboard Toggle word wrap

    For example, to execute a playbook called site.yml on the Ansible Automation Platform RHEL 9 minimum execution environment, run the following command from the terminal in VS Code.

    ansible-navigator run site.yml --eei ee-minimal-rhel8
    Copy to Clipboard Toggle word wrap

Verification

The output is displayed in the terminal. You can inspect the results and step into each play and task that was executed.

Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat
トップに戻る