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
-
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 examplesite.ymlfile. Add the following example code into the playbook file and save the file. The playbook consists of a single play that executes a
pingto your local machine.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Ansible-lintruns in the background and displays errors in the Problems tab of the terminal. There are no errors in this playbook: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).
- 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.
- Open a playbook in VS Code.
Hover your mouse over a keyword or a module name: the Ansible extension provides documentation:
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.
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:- name: hosts: localhost tasks: - name: ansible.builtin.ping:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The errors are indicated with a wavy underline in VS Code.
Hover your mouse over an error to view the details:
- Playbook files that contain errors are indicated with a number in the Explorer pane.
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-playbookruns the playbook on your local machine using Ansible Core. -
ansible-navigatorruns 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
.
The output is displayed in the Terminal tab of the VS Code terminal. The
ok=2andfailed=0messages indicate that the playbook ran successfully.
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
- Click on an execution environment for information on how to download it.
Log in to
registry.redhat.ioif you have not already done so.NoteIf you are running Ansible development tools on a container inside VS Code and you want to pull execution environments or the
devcontainerto use as an execution environment, you must log in toregistry.redhat.iofrom a terminal prompt within thedevcontainerinside VS Code.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
$ podman pull registry.redhat.io/ansible-automation-platform-25/ee-minimal-rhel9Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.- 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>
$ ansible-navigator run <playbook_name.yml> -eei <execution_environment_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, to execute a playbook called
site.ymlon 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
ansible-navigator run site.yml --eei ee-minimal-rhel8Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
The output is displayed in the terminal. You can inspect the results and step into each play and task that was executed.

