Build an Ansible playbook without the YAML pitfalls
Ansible playbooks are powerful but unforgiving about indentation and module names. A single mis-indented task or a deprecated short module name can stop a run cold with a cryptic error. This builder assembles a clean, valid playbook from simple inputs so you can focus on what to provision rather than YAML syntax.
How it works
The output is a single-play playbook — a YAML list containing one mapping with the keys Ansible expects at the play level:
- name: <your play name>
hosts: <target group>
become: true
vars:
http_port: 80
tasks:
- name: Install packages
ansible.builtin.apt:
name:
- nginx
- git
state: present
- name: Enable and start nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
Every task uses fully qualified ansible.builtin.* module names. Short names like apt or service still work but generate warnings in modern Ansible and will break if a collection with the same name is installed. The builder emits two-space indentation throughout, which is the YAML standard Ansible expects, and quotes string values only when YAML would otherwise misparse them.
What each generated task does
- Package install — uses
ansible.builtin.apt(Debian/Ubuntu) oransible.builtin.yum(RHEL/CentOS) withstate: present. Safe to re-run: if the package is already installed, Ansible does nothing. - Service management — uses
ansible.builtin.servicewithstate: startedandenabled: true. This starts the service immediately and registers it to start on boot. - File copy — uses
ansible.builtin.copywith asrc(local file on the control machine) anddest(absolute path on the remote host). Specifymodein octal if you need non-default permissions.
Practical guidance
Save the result as site.yml and run it with:
ansible-playbook -i inventory site.yml
Or target a single host for testing:
ansible-playbook -i "192.168.1.10," site.yml
Note the trailing comma — it tells Ansible the inventory is an inline host list, not a file.
Common mistakes to avoid
- Forgetting
become: truewhen installing packages. Package managers need root and Ansible will silently fail with a permissions error unless escalation is enabled. - Mixing tabs and spaces. YAML rejects tabs. If your editor inserts tabs, the playbook will fail to parse. This builder uses spaces only.
- Using the wrong package module for your distribution. Specify
aptfor Debian-family systems andyumordnffor RHEL-family; the builder lets you choose. - Hardcoding paths and ports in tasks. Put variable values in the
varsblock and reference them with{{ variable_name }}so you can override them at runtime with-eor in group vars.