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은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat
맨 위로 이동